下面的例子中,button事件源被添加了两个事件监听ActionListener和MouseListener,当点击这个按钮时,哪个事件会先被触发呢?
import java.awt.*;
import java.awt.event.*;
public class MouseKeyEventDemo {
private Frame frame;
private Button btn;
public MouseKeyEventDemo() {
init();
}
public void init() {
frame = new Frame("my frame");
frame.setBounds(300, 200, 500, 400);
frame.setLayout(new FlowLayout());
btn = new Button("按钮");
frame.add(btn);
frame.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
btn.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) {
System.out.println("actionPerformed被执行了"); }
});
btn.addMouseListener(new MouseAdapter() { private int count = 1;
public void mouseEntered(MouseEvent e) {
System.out.println("鼠标第" + count + "次进入到按键上了!!");
count++;
}
public void mouseClicked(MouseEvent e) {
if(e.getClickCount()==2)
System.out.println("双击动作!");
}
});
btn.addKeyListener(new KeyAdapter(){
public void keyPressed(KeyEvent k){
System.out.println(k.getKeyChar()+":"+k.getKeyCode()+k.getKeyText(k.getKeyCode()));
}
});
frame.setVisible(true);
}
public static void main(String[] args) {
new MouseKeyEventDemo();
}
}
|
|