Liny_@NotePad

沉迷ACG中

AWT事件监听作业学习例子(3) - MouseEvent + MouseWheelEvent

YOYO posted @ 2008年8月12日 04:05 in 【Java SE】 with tags java AWT ActionEvent 监听 , 3695 阅读
【事件执行情况】MouseEvent - 与鼠标事件相关联的操作时执行 ; MouseWheelEvent - 鼠标滚轮在组件中滚动时执行
【传递的接口名】MouseListener,MouseMotionListener + MouseWheelListener
【需要实现方法】MouseListener:mouseClicked - 鼠标按键在组件上单击(按下并释放)时调用; mouseEntered - 鼠标进入到组件上时调用;mouseExited - 鼠标离开组件时调用;mousePressed - 鼠标按键在组件上按下时调用;mouseReleased - 鼠标按钮在组件上释放时调用;
         MouseMotionListener:mouseDragged - 鼠标按键在组件上按下并拖动时调用;mouseMoved - 鼠标光标移动到组件上但无按键按下时调用
         MouseWheelListener:mouseWheelMoved - 鼠标滚轮旋转时调用
【样例】鼠标在panel上动作/当前鼠标位置/滚轮状态

【代码】

  1. import javax.swing.*;
  2.  
  3. import java.awt.*;
  4. import java.awt.event.*;
  5.  
  6. public class CallByMouse {
  7.  
  8.         /**
  9.          * @author Linyq.
  10.          */
  11.         public static void main(String[] args) {
  12.                 JFrame.setDefaultLookAndFeelDecorated(true);
  13.                 MyFrame frm = new MyFrame();
  14.                 frm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  15.         }
  16.  
  17. }
  18.  
  19. class MyFrame extends JFrame{
  20.        
  21.         public MyFrame(){
  22.                 this.setTitle("MouseEvent 测试");
  23.                 this.setSize(600,300);
  24.                 this.setBackground(Color.DARK_GRAY);
  25.                 this.setLayout(new FlowLayout(FlowLayout.CENTER,20,10));
  26.                
  27.                 final JLabel label = new JLabel("鼠标没有任何动作");
  28.                 this.getContentPane().add(label);
  29.                 //      此文本标签用来记录鼠标动作
  30.                
  31.                 final JLabel xy = new JLabel("鼠标不在panel容器范围中");
  32.                 this.getContentPane().add(xy);
  33.                 //      此文本标签用来显示当前鼠标坐标
  34.                
  35.                 final JLabel wheel = new JLabel("滚轮等待动作..");
  36.                 this.getContentPane().add(wheel);
  37.                 //      此文本标签用来显示滚轮状态
  38.                
  39.                 //      panel
  40.                 Panel panel = new Panel();
  41.                 panel.setLayout(null);
  42.                 panel.setSize(500,200);
  43.                 panel.setBackground(Color.black);
  44.                 this.getContentPane().add(panel);
  45.                
  46.                 //      鼠标事件(按下、释放、单击、进入或离开)的侦听器接口
  47.                 panel.addMouseListener(new MouseListener(){
  48.  
  49.                         public void mouseClicked(MouseEvent e) {
  50.                                 //      鼠标按键在组件上单击(按下并释放)时调用
  51.                                 label.setText("鼠标在panel容器上单击(按下并释放)");
  52.                         }
  53.  
  54.                         public void mouseEntered(MouseEvent e) {
  55.                                 //      鼠标进入到组件上时调用
  56.                                 label.setText("鼠标进入panel容器");
  57.                         }
  58.  
  59.                         public void mouseExited(MouseEvent e) {
  60.                                 //      鼠标离开组件时调用
  61.                                 label.setText("鼠标离开panel容器");
  62.                         }
  63.  
  64.                         public void mousePressed(MouseEvent e) {
  65.                                 //      鼠标按键在组件上按下时调用
  66.                                 if(e.getClickCount()==2){
  67.                                         JOptionPane.showMessageDialog(null,"鼠标在panel容器上双击!");
  68.                                 }
  69.                                 if(e.getButton()==e.BUTTON3){
  70.                                         label.setText("鼠标右键在panel容器上按下");
  71.                                 }
  72.                                 if(e.getButton()==e.BUTTON1){
  73.                                         label.setText("鼠标左键在panel容器上按下");
  74.                                 }
  75.                                 if(e.getButton()==e.BUTTON2){
  76.                                         label.setText("鼠标中键在panel容器上按下");
  77.                                 }
  78.                         }
  79.  
  80.                         public void mouseReleased(MouseEvent e) {
  81.                                 //      鼠标按钮在组件上释放时调用
  82.                                 label.setText("鼠标在panel容器上释放");
  83.                         }
  84.                        
  85.                 });
  86.                 //      鼠标移动事件的侦听器接口
  87.                 panel.addMouseMotionListener(new MouseMotionListener(){
  88.  
  89.                         @Override
  90.                         public void mouseDragged(MouseEvent e) {
  91.                                 //      鼠标按键在组件上按下并拖动时调用
  92.                                 label.setText("鼠标在panel上按下并拖动");
  93.                         }
  94.  
  95.                         @Override
  96.                         public void mouseMoved(MouseEvent e) {
  97.                                 //      鼠标光标移动到组件上但无按键按下时调用
  98.                                 xy.setText("当前位置  (x:" + e.getX() + ",y:" + e.getY() + ") ");
  99.                         }
  100.                        
  101.                 });
  102.                
  103.                 //      鼠标滚轮事件的侦听器接口
  104.                 panel.addMouseWheelListener(new MouseWheelListener(){
  105.  
  106.                         @Override
  107.                         public void mouseWheelMoved(MouseWheelEvent e) {
  108.                                 //      鼠标滚轮旋转时调用
  109.                                 wheel.setText("滚轮向" + (e.getWheelRotation()>0?"下":"上") + "滚动");
  110.                         }
  111.                        
  112.                 });          
  113.                
  114.                 this.setVisible(true);
  115.         }
  116.        
  117. }

 


登录 *


loading captcha image...
(输入验证码)
or Ctrl+Enter