/*
鼠标和键盘事件
鼠标和键盘在 component 中查找
*/
import java.awt.*;
import java.awt.event.*;
class MouseAndKey
{
private Frame f;
private Button b;
MouseAndKey()
{
init();
}
public void init()
{
f=new Frame("my frame");
f.setBounds(200,100,500,400);
f.setLayout(new FlowLayout());
b=new Button("button");
f.add(b);
myEvent();
f.setVisible(true);
}
private void myEvent()
{
f.addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
});
//鼠标监听按钮
b.addMouseListener(new MouseAdapter()
{
int count=1;
int clickCount=1;
int clickCountDouble=1;
public void mouseEntered(MouseEvent e)
{
System.out.println("鼠标进入到该组件"+count++);
}
public void mouseClicked(MouseEvent e)
{
if(e.getClickCount()==2)
System.out.println("鼠标双击"+clickCountDouble++);
System.out.println("鼠标点击该组件"+clickCount++);
int whichKey = e.getButton();//左键为1,中键为2,右键为3
System.out.println(whichKey);
}
});
}
public static void main(String[] args)
{
new MouseAndKey();
}
}
day22/MouseAndKey.java
最新推荐文章于 2022-07-22 17:20:02 发布