package gui;
import java.awt.Button;
import java.awt.FlowLayout;
import java.awt.Frame;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
public class MouseListenerDemo {
private Frame f;
private Button bt;
MouseListenerDemo(){
init();
}
public void init(){
f = new Frame("MyFrame");
f.setBounds(200, 150, 400, 350);
f.setLayout(new FlowLayout());
bt = new Button("good");
f.add(bt);
myevent();
f.setVisible(true);
}
void myevent(){
f.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
System.exit(0);
}
});
bt.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e) {
System.out.println("Action");
}
});
bt.addMouseListener(new MouseAdapter(){
private int count=0;
public void mouseEntered(MouseEvent e){
System.out.println("enter:"+count++);
}
public void mouseClicked(MouseEvent e){
System.out.println("click!");
}
});
}
public static void main(String[] args) {
new MouseListenerDemo();
}
}
就这个程序,我在激活“good”button的情况下,回车没打印“Action”。然后我执行毕老师的源代码执行,情况也是一样。
import java.awt.*;
import java.awt.event.*;
class MouseDemo
{
private Frame f;
private Button but;
MouseDemo()
{
init();
}
public void init()
{
f = new Frame("mouse demo");
f.setBounds(400,100,500,500);
f.setLayout(new FlowLayout());
but = new Button("mouse demo");
f.add(but);
myEvent();
f.setVisible(true);
}
public void myEvent()
{
//窗体关闭事件。
f.addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
});
but.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
System.out.println("action");
}
});
//给按钮添加一个鼠标事件。
but.addMouseListener(new MouseAdapter()
{
private int count = 1;
public void mouseEntered(MouseEvent e)
{
System.out.println("enter :"+count++);
}
public void mouseClicked(MouseEvent e)
{
if(e.getClickCount()==2)
System.out.println("double click :");
}
});
}
}
class MouseListenerDemo
{
public static void main(String[] args)
{
new MouseDemo();
}
}
毕老师的代码。
|
|