当前位置: 代码迷 >> J2SE >> 一个很简单的事件监听有关问题,拜托各位帮上忙
  详细解决方案

一个很简单的事件监听有关问题,拜托各位帮上忙

热度:82   发布时间:2016-04-24 00:27:48.0
一个很简单的事件监听问题,拜托各位帮下忙
[code=Java][/code]

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.lang.invoke.SwitchPoint;
import java.util.Random;

import javax.swing.*;

import org.omg.CORBA.PRIVATE_MEMBER;

public class Game {

public static void main(String[] args) {

EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
Graph graph = new Graph();
graph.setVisible(true);
}
});
}

}
 class Graph extends JFrame {
public Graph() {
setSize(DEFAULT_WIDTH, DEFAULT_HEIGTH);
setLayout(new GridLayout(2, 2));

myPanel = new MyPanel();
JPanel jPane2 = new JPanel();
add(jPane2);
jPane2.add(myPanel);
jPane2.setLayout(null);//
myPanel.setSize(100, 100);
myPanel.setLocation(0, 0);

textArea = new JTextArea();
add(textArea);

JPanel jPanel = new JPanel();
jPanel.setLayout(new GridLayout(2, 1));
add(jPanel);

jButton1 = new JButton("√");
jButton1.addActionListener(listener);
jButton1.setFont(new Font("黑体", Font.BOLD, 45));
jButton2 = new JButton("×");
jButton2.setFont(new Font("宋体", 2, 45));
jButton2.addActionListener(listener);
jPanel.add(jButton1);
jPanel.add(jButton2);
}

private ActionListener listener = new ActionListener() {

@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
rm_previous = myPanel.getRnm();
myPanel.repaint();
rm_now = myPanel.getRnm();
Object sourceObject = e.getSource();// get the source of events

if (sourceObject == jButton1) {
if (rm_previous == rm_now)
score += 5;
else {
score -= 3;
}
} else if (sourceObject == jButton2) {
if (rm_previous == rm_now)
score -= 3;
else {
score += 5;
}
}
textArea.setText(Integer.toString(score));
}

};
private int score = 0, rm_previous, rm_now;
private JButton jButton1, jButton2;
private MyPanel myPanel;
private JTextArea textArea;
public static final int DEFAULT_WIDTH = 400;
public static final int DEFAULT_HEIGTH = 300;
}
 class MyPanel extends JPanel {
public void paint(Graphics graphics) {
// super.paint(graphics);
Graphics g2d = (Graphics2D) graphics;
g2d.setColor(Color.black);
Rectangle rectangle = new Rectangle(0, 0, GRA_HEIGTH, GRA_HEIGTH);
g2d.setColor(Color.white);
g2d.fillRect(0, 0, GRA_HEIGTH, GRA_HEIGTH);

rdm = new Random().nextInt(3);
switch (rdm) {
case 0 :
g2d.setColor(Color.blue);
g2d.fillRect(0, 0, GRA_HEIGTH, GRA_HEIGTH);
break;
case 1 :
g2d.setColor(Color.yellow);
g2d.fillOval(0, 0, GRA_HEIGTH, GRA_HEIGTH);
break;
default :
g2d.setColor(Color.green);
g2d.drawLine(0, GRA_HEIGTH, GRA_HEIGTH / 2, 0);
g2d.drawLine(GRA_HEIGTH, GRA_HEIGTH, GRA_HEIGTH / 2, 0);
g2d.drawLine(0, GRA_HEIGTH, GRA_HEIGTH, GRA_HEIGTH);
break;
}
}
public int getRnm() {
return rdm;
}
private int rdm;
public static final int GRA_HEIGTH = 100;
}



我想知道为什么每次按下√按钮就+5,按下×就-3???真没找出哪里有问题
还有这两个按钮用了一个监听器然后判断事件源,是不是这样不太好呀?求各位指点一下吧

------解决方案--------------------
没记错的话repaint不立即触发paint(),另外也不是每次repaint都触发paint();

repaint只是增加了一个GUI事件,至于事件什么时候被处理,必然是要等到当前事件处理完毕之后了。

所以在你下面这句话中:
rm_previous = myPanel.getRnm();
myPanel.repaint();
rm_now = myPanel.getRnm();

rm_previous === rm_now
  相关解决方案