下面代码是鼠标进入按钮时记录进入次数:
package awtTest;
import java.awt.*;
import java.awt.event.*;
public class ListenerTest{
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
new Frames();
}
}
class Frames{
private Frame f;
private Button b;
public Frames(){
f=new Frame("My Frame");
f.setBounds(333, 444, 555, 666);
b=new Button("关闭");
f.add(b);
f.setLayout(new FlowLayout());
f.setVisible(true);
Events();
}
public void Events(){
f.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
System.exit(0);
}
});
b.addMouseListener(new MouseAdapter(){
int count=1;//放在这里运行正确。
public void mouseEntered(MouseEvent e){
//当把int count=1;放到此处时,运行代码后不管鼠标进入按钮多少次都打印出:进入1次,
System.out.println("进入"+(count++)+"次");
}
});
}
}
为什么会这样? |