import java.awt.*;
import java.awt.event.*;
class MouseAndKeyEventDemo
{
private Frame f;
private Button but;
MouseAndKeyEventDemo()
{
init();
}
public void init()
{
f=new Frame("my awt");
//对Frame进行基本设置
f.setBounds(300,100,600,500);
f.setLayout(new FlowLayout());
but=new Button("my button");
//将组件添加到frame
f.add(but);
//显示窗体之前加载事件监听器
myEvent();
//显示窗体
f.setVisible(true);
}
private 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("点击按钮");
}
});
but.addMouseListener(new MouseAdapter()
{
private int count=1;
private int clickcount=1;
public void mouseEntered(MouseEvent e)
{
System.out.println("鼠标进入该组件"+count++);
}
/*
public void mouseClicked(MouseEvent e)
{
System.out.println("点击动作"+clickcount++);
}
*/
//演示双击动作
public void mouseClicked(MouseEvent e)
{
if(e.getClickCount()==2) //获取点击次数
System.out.println("双击动作");
}
});
}
public static void main(String[] args)
{
new MouseAndKeyEventDemo();
}
}
演示鼠标双击的时候,判断当鼠标点击两次就执行鼠标双击,
运行时鼠标调集按钮,迅速点两次执行双击语句,但是两次间隔
时间点击就不会执行双击的语句,是不是getClickCount()这个
方法中封装了什么功能,但是查阅API查不到?求解
|
|