//添加鼠标监听器
import java.awt.*;
import java.awt.event.*;
class AwtDemo3
{
private Frame f;
private Button b;
AwtDemo3()
{
init();
}
public void init()
{
f=new Frame("魔兽世界");
b=new Button("退出游戏");
f.setSize(300,500);
f.setLocation(500,600);
f.setLayout(new FlowLayout());
f.add(b);
myEvent();
f.setVisible(true);
}
private void myEvent()
{
f.addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
System.out.println("窗体退出");
System.exit(0);
}
});
b.addMouseListener(new MouseAdapter()
{
public void mouseEntered(MouseEvent e)
{
System.out.println("鼠标进入组件!!!");
}
public void mouseExited(MouseEvent e)
{
System.out.println("鼠标离开组件");
}
public void mouseClicked(MouseEvent e)
{
if (e.getClickCount()==2)
{
System.out.println("双击");
}
System.out.println("鼠标点击按钮!!");
}
});
b.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
//System.out.println("按按钮退出");
//System.exit(0);
}
});
}
public static void main(String[] args)
{
new AwtDemo3();
}
}
4.加入鼠标监听
最新推荐文章于 2022-12-03 22:07:24 发布