- 0
- 0
- 约1.47千字
- 约 5页
- 2026-02-03 发布于陕西
- 举报
鼠标事件主讲教师:杨家娥
鼠标事件指的是使用鼠标所进行的操作,如单击鼠标、双击鼠标、松开鼠标等。Java使用MouseEvent类描述鼠标事件。可以通过实现MouseListener接口定义监听器或通过继承适配器MouseAdapter类定义监听器,然后调用addMouseListener()方法将监听器绑定到事件源对象。MouseEvent类、MouseAdapter类和MouseListener接口均位于java.awt.event包。
importjavax.swing.*;importjava.awt.*;importjava.awt.event.*;publicclassMouseEventdemo{publicstaticvoidmain(String[]args){JFrameframe=newJFrame(JFrame窗口);frame.setLayout(newFlowLayout());frame.setSize(300,200);frame.setLocation(300,200);JButtonbutton=newJButton(button);frame.add(button);frame.setVisible(true);button.addMouseListener(newMouseListener(){publicvoidmousePressed(MouseEvente){System.out.println(鼠标按下);}
publicvoidmouseReleased(MouseEvente){System.out.println(鼠标放开);}publicvoidmouseExited(MouseEvente){System.out.println(鼠标移出按钮区域);}publicvoidmouseEntered(MouseEvente){System.out.println(鼠标进入按钮区域);}publicvoidmouseClicked(MouseEvente){if(e.getClickCount()==1){System.out.println(鼠标完成单击);}if(e.getClickCount()==2){System.out.println(鼠标完成双击);}}});}}
谢谢观看Thanksforwatching
原创力文档

文档评论(0)