public static void main(String[] args) {
new MouseListenerDemo();
}
public MouseListenerDemo() {
init();
}
private void init() {
f = new Frame("myFrame");
f.setBounds(300, 250, 400, 250);
f.setLayout(new FlowLayout());
tf = new TextField(25);
but = new Button("button");
myEvent_1();
myEvent_2();
myEvent_3();
f.add(tf);
f.add(but);
f.setVisible(true);
}
private void myEvent_3() {
f.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}
private void myEvent_2() {
but.addMouseListener(new MouseAdapter() {
int count = 1;
@Override
public void mouseClicked(MouseEvent e) {
//鼠标双击
if (e.getClickCount() == 2)
tf.setText(String.valueOf(count++));
}
});
}
private void myEvent_1() {
but.addMouseListener(new MouseAdapter() {
private int count = 1;
@Override
public void mouseEntered(MouseEvent e) {
tf.setText(String.valueOf(count++));
}
});
}