day22/MouseAndKey.java

来源:互联网 发布:写轮眼隐形眼镜淘宝 编辑:程序博客网 时间:2024/06/06 07:26
/*鼠标和键盘事件鼠标和键盘在 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,右键为3System.out.println(whichKey);}});}public static void main(String[] args) {new MouseAndKey();}}

0 0
原创粉丝点击