Java監聽鼠標事件 – JAVA編程語言程序開發技術文章

[java]
package com.han; 
 
import java.awt.Container; 
import java.awt.event.MouseEvent; 
import java.awt.event.MouseListener; 
 
import javax.swing.JFrame; 
 
/**
 * 監聽鼠標事件
 * 可以看出,當雙擊鼠標時,第一次的點擊會觸發一次單擊事件
 * @author HAN
 *
 */ 
public class MouseEvent_1 extends JFrame { 
 
    /**
     * 
     */ 
    private static final long serialVersionUID = 7554087008285696671L; 
 
    public MouseEvent_1() { 
        // TODO Auto-generated constructor stub  
        Container container = getContentPane(); 
        container.addMouseListener(new MouseListener() { 
 
            @Override 
            public void mouseClicked(MouseEvent e) { 
                // TODO Auto-generated method stub  
                System.out.print("單擊瞭鼠標按鍵,"); 
                int i = e.getButton(); 
                if (i == MouseEvent.BUTTON1) 
                    System.out.print("單擊的是鼠標左鍵,"); 
                if (i == MouseEvent.BUTTON2) 
                    System.out.print("單擊的是鼠標中鍵,"); 
                if (i == MouseEvent.BUTTON3) 
                    System.out.print("單擊的是鼠標右鍵,"); 
                int clickCount = e.getClickCount(); 
                System.out.println("單擊次數為" + clickCount + "下"); 
            } 
 
            @Override 
            public void mousePressed(MouseEvent e) { 
                // TODO Auto-generated method stub  
                System.out.print("鼠標按鍵被按下,"); 
                int i = e.getButton(); 
                if (i == MouseEvent.BUTTON1) 
                    System.out.println("按下的是鼠標左鍵"); 
                if (i == MouseEvent.BUTTON2) 
                    System.out.println("按下的是鼠標中鍵"); 
                if (i == MouseEvent.BUTTON3) 
                    System.out.println("按下的是鼠標右鍵"); 
            } 
 
            @Override 
            public void mouseReleased(MouseEvent e) { 
                // TODO Auto-generated method stub  
                System.out.print("鼠標按鍵被釋放,"); 
                int i = e.getButton(); 
                if (i == MouseEvent.BUTTON1) 
                    System.out.println("釋放的是鼠標左鍵"); 
                if (i == MouseEvent.BUTTON2) 
                    System.out.println("釋放的是鼠標中鍵"); 
                if (i == MouseEvent.BUTTON3) 
                    System.out.println("釋放的是鼠標右鍵"); 
            } 
 
            @Override 
            public void mouseEntered(MouseEvent e) { 
                // TODO Auto-generated method stub  
                System.out.println("光標移入組件"); 
            } 
 
            @Override 
            public void mouseExited(MouseEvent e) { 
                // TODO Auto-generated method stub  
                System.out.println("光標移出組件"); 
            } 
             
        }); 
    } 
 
    /**
     * @param args
     */ 
    public static void main(String[] args) { 
        // TODO Auto-generated method stub  
        MouseEvent_1 frame = new MouseEvent_1(); 
        frame.setTitle("MouseEvent Test"); 
        frame.setVisible(true); 
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
        frame.setBounds(0, 0, 300, 100); 
    } 
 

package com.han;

import java.awt.Container;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

import javax.swing.JFrame;

/**
 * 監聽鼠標事件
 * 可以看出,當雙擊鼠標時,第一次的點擊會觸發一次單擊事件
 * @author HAN
 *
 */
public class MouseEvent_1 extends JFrame {

 /**
  *
  */
 private static final long serialVersionUID = 7554087008285696671L;

 public MouseEvent_1() {
  // TODO Auto-generated constructor stub
  Container container = getContentPane();
  container.addMouseListener(new MouseListener() {

   @Override
   public void mouseClicked(MouseEvent e) {
    // TODO Auto-generated method stub
    System.out.print("單擊瞭鼠標按鍵,");
    int i = e.getButton();
    if (i == MouseEvent.BUTTON1)
     System.out.print("單擊的是鼠標左鍵,");
    if (i == MouseEvent.BUTTON2)
     System.out.print("單擊的是鼠標中鍵,");
    if (i == MouseEvent.BUTTON3)
     System.out.print("單擊的是鼠標右鍵,");
    int clickCount = e.getClickCount();
    System.out.println("單擊次數為" + clickCount + "下");
   }

   @Override
   public void mousePressed(MouseEvent e) {
    // TODO Auto-generated method stub
    System.out.print("鼠標按鍵被按下,");
    int i = e.getButton();
    if (i == MouseEvent.BUTTON1)
     System.out.println("按下的是鼠標左鍵");
    if (i == MouseEvent.BUTTON2)
     System.out.println("按下的是鼠標中鍵");
    if (i == MouseEvent.BUTTON3)
     System.out.println("按下的是鼠標右鍵");
   }

   @Override
   public void mouseReleased(MouseEvent e) {
    // TODO Auto-generated method stub
    System.out.print("鼠標按鍵被釋放,");
    int i = e.getButton();
    if (i == MouseEvent.BUTTON1)
     System.out.println("釋放的是鼠標左鍵");
    if (i == MouseEvent.BUTTON2)
     System.out.println("釋放的是鼠標中鍵");
    if (i == MouseEvent.BUTTON3)
     System.out.println("釋放的是鼠標右鍵");
   }

   @Override
   public void mouseEntered(MouseEvent e) {
    // TODO Auto-generated method stub
    System.out.println("光標移入組件");
   }

   @Override
   public void mouseExited(MouseEvent e) {
    // TODO Auto-generated method stub
    System.out.println("光標移出組件");
   }
   
  });
 }

 /**
  * @param args
  */
 public static void main(String[] args) {
  // TODO Auto-generated method stub
  MouseEvent_1 frame = new MouseEvent_1();
  frame.setTitle("MouseEvent Test");
  frame.setVisible(true);
  frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  frame.setBounds(0, 0, 300, 100);
 }

}

 

作者:Gaowen_HAN

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。