public class GUIDemo {
public static void main(String[] args){
Frame f=new Frame("my.awt");
f.setBounds(200,200, 400, 300);
f.setLayout(new FlowLayout());
Button but=new Button("my button");
f.add(but);
but.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
System.out.println("action ok");
//System.exit(0);
}
});
but.addMouseListener(new MouseAdapter()
{
private int count=1;
private int clickCount=1;
public void mouseEntered(MouseEvent e)
{
System.out.println("鼠标进入到该组件"+count++);
}
public void mouseClicked(MouseEvent e)
{
System.out.println("点击动作"+clickCount++);
}
});
f.setVisible(true);
}
}
我把你的代码复制了一遍,你看看一样吗, 当执行but.addActionListener(new ActionListener()时,你点击鼠标左键或者按键盘上的键时都会输出action ok |