Swing鼠标事件

让我们来看看这些接口中的方法:

・ mousePressed() 当用户按下鼠标按钮时发生.  

・ mouseReleased() 当用户松开鼠标按钮时发生.

・ mouseClicked() 当用户按下并松开鼠标按钮时发生. 用户在选择或双击图标的时候通常会点击鼠标按钮. 用户如果在松开鼠标之前移动鼠标,点击不会导致鼠标相应事件出现.  

・ 因为点击鼠标是按下鼠标和松开鼠标的结合, 在事件分配给 mouseClicked() 方法之前, mousePressed() 和 mouseReleased() 方法已同时被调用.  

・ mouseEntered() 当鼠标离开当前组件并进入你所监听的组件时激活事件.

・ mouseExited() 当鼠标离开你所监听的组件时发生.  

・ mouseDragged() 当用户按下鼠标按钮并在松开之前进行移动时发生.在mouseDragged() 后松开鼠标不会导致mouseClicked().  

・ mouseMoved() 当鼠标在组件上移动而 不时拖动时发生.

 

 

 

 一、

java的鼠标事件

MouseListener 方法接口中的mouseClicked() 方法和mousePressed() 方法有什么区别
这是一个热门的话题,让我们来深入的找寻答案,Java’s AWT 库提供了两个接口来监听和接收鼠标事件. 一个是 java.awt.event.MouseListener:
public interface MouseListener extends EventListener
   {
   public void mousePressed(MouseEvent e);
   public void mouseReleased(MouseEvent e);
   public void mouseClicked(MouseEvent e);
   public void mouseEntered(MouseEvent e);
   public void mouseExited(MouseEvent e);
  }
另一个是 java.awt.event.MouseMotionListener:

     public interface MouseMotionListener extends EventListener
   {
   public void mouseDragged(MouseEvent e);
   public void mouseMoved(MouseEvent e);
  }

Swing 提供了 MouseInputListener, 这个接口的同时扩展了MouseMotionListener 和MouseListener 接口.

让我们来看看这些接口中的方法: mousePressed() 当用户按下鼠标按钮时发生.  
mouseReleased() 当用户松开鼠标按钮时发生.
mouseClicked() 当用户按下并松开鼠标按钮时发生. 用户在选择或双击图标的时候通常会点击鼠标按钮. 用户如果在松开鼠标之前移动鼠标,点击不会导致鼠标相应事件出现.  
因为点击鼠标是按下鼠标和松开鼠标的结合, 在事件分配给 mouseClicked() 方法之前, mousePressed() 和 mouseReleased() 方法已同时被调用.  
mouseEntered() 当鼠标离开当前组件并进入你所监听的组件时激活事件.
mouseExited() 当鼠标离开你所监听的组件时发生.  
mouseDragged() 当用户按下鼠标按钮并在松开之前进行移动时发生.在mouseDragged() 后松开鼠标不会导致mouseClicked().  
mouseMoved() 当鼠标在组件上移动而 不时拖动时发生.
要监听鼠标事件就必须调用这些接口之一,或扩展一个鼠标适配器(mouse adapters) 类. AWT 提供了两种监听适配器(listener adapters): java.awt.event.MouseAdapter 和java.awt.event.MouseMotionAdapter.
Swing 为MouseInputListener提供了一个叫做javax.swing.event.MouseInputAdapter的适配器. 有了适配器你就不必调用接口中每个方法了. 取而代之, 你只需简单的扩展适配器并重写你要监听的方法就可以了.

如果你还是很迷惑, 再看看API 并写个小测试程序. 有时尝试理解不同事件的最好方法different events. 我写了一个小小的应用程序来表现两种鼠标事件的区别.这里是完整的代码:
  
   import java.awt.*;
   import java.awt.event.*;
   public class MouseTest extends Frame
   {
   public MouseTest()
   {
   Button b = new Button("JavaWorld JavaQ&A");
   b.addActionListener( new ActionListener() {
   public void actionPerformed(ActionEvent e)
   {
   System.exit(0);
   }
   }
   );
   add(b,BorderLayout.NORTH);
   addMouseListener(new MouseTest.MouseHandler());
   addMouseMotionListener(new MouseTest.MouseMotionHandler());
   }
   // MouseHandler is an inner class that implements the MouseListener.
   // Each method simply prints out a message to the command line.
   private class MouseHandler implements MouseListener
   {
   public void mousePressed(MouseEvent e)
   {
   System.out.println("mouse pressed");
   }
   public void mouseClicked(MouseEvent e)
   {
   System.out.println("moused clicked");
   }
   public void mouseReleased(MouseEvent e)
   {
   System.out.println("mouse released");
   }
   public void mouseEntered(MouseEvent e)
   {
   System.out.println("mouse entered");
   }
   public void mouseExited(MouseEvent e)
   {
   System.out.println("mouse exited");
   }
   }
   // MouseMotionHandler is an inner class that implements the MouseMotionListener.
   // Each method simply prints out a message to the command line.
   private class MouseMotionHandler implements MouseMotionListener
   {
   public void mouseMoved(MouseEvent e)
   {
   System.out.println("mouse moved");
   }
   public void mouseDragged(MouseEvent e)
   {
   System.out.println("mouse dragged");
   }
   }
   public static void main(String[] args)
   {
   new MouseTest().show();
   }
  }

这个应用程序建立了一个窗口和一个按钮.当你移动鼠标并按下按钮时, 应用程序就会在命令行打印出文本消息, 让你看到鼠标响应和事件间的对应关系.  
象上边那样写一个小小的测试程序是对陌生的API 进行试验的好方法.

 


--------------------------------------------------------------------------------------------------------------
          MouseListener                              mouseClicked(MouseEvent e)
                                                               mouseEntered(MouseEvent e)
                                                               mouseExited(MouseEvent e)
                                                               mousePressed(MouseEvent e)
                                                               mouseReleased(MouseEvent e)
--------------------------------------------------------------------------------------------------------------
MouseDemo1.java

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
/*我们继承WindowAdapter抽象类并实现MouseListener interface,
*因此我们必须把MouseListener中的5个方法都实现,如果不想实现
*可用匿名内部类的方法编写处理程序。
*/
public class MouseDemo1 extends WindowAdapter implements MouseListener{
    JFrame f=null;
    JButton b1=null;
    JLabel label=null;
   
    public MouseDemo1(){
       f=new JFrame("MouseDemo1");
       Container contentPane=f.getContentPane();
       contentPane.setLayout(new GridLayout(2,1));
       b1=new JButton("按钮");
       label=new JLabel("起始状态,还没有鼠标事件",JLabel.CENTER);
       b1.addMouseListener(this);
       contentPane.add(label);
       contentPane.add(b1);
       f.pack();
       f.show();
       f.addWindowListener(this);
    }
    public void mousePressed(MouseEvent e){
        label.setText("你已经压下鼠标按钮");
    }
    public void mouseReleased(MouseEvent e){
        label.setText("你已经放开鼠标按钮");
    }
    public void mouseEntered(MouseEvent e){
        label.setText("鼠标光标进入按钮");
    }
    public void mouseExited(MouseEvent e){
        label.setText("鼠标光标离开按钮");
    }
    public void mouseClicked(MouseEvent e){
        label.setText("你已经按下按钮");
    }
    public void windowClosing(WindowEvent e){
        System.exit(0);
    }
    public static void main(String[] args){
       new MouseDemo1();       
    }
}


2-1-2:MouseMotionListener的使用:
         下面是讨论MouseMotionListener的使用时机,它提供的下面的两个方法,可让你随时掌握鼠标的坐标,并处理拖曳鼠标
的操作。
--------------------------------------------------------------------------------------------------------------
          MouseMotionListener                   mouseDragged(MouseEvent e)
                                                               mouseMoved(MouseEvent e)
--------------------------------------------------------------------------------------------------------------
下面的范例让你知道鼠标在JFrame上的坐标,并拖曳出直线来。
例子:MouseDemo3.java

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
/*为了达到画线的功能,我们分别implements MouseListener与MouseMotionListener.
*/
public class MouseDemo3 extends JFrame implements MouseListener,MouseMotionListener{
     int flag;//flag=1代表Mouse Moved,flag=2代表Mouse Dragged
     int x=0;
     int y=0;
     int startx,starty,endx,endy;//起始坐标与终点坐标
     public MouseDemo3(){
        Container contentPane=getContentPane();
        contentPane.addMouseListener(this);
        contentPane.addMouseMotionListener(this);
        setSize(300,300);
        show();
        addWindowListener(
                  new WindowAdapter(){
                     public void windowClosing(WindowEvent e){
                        System.exit(0);
                     }
              }
        );       
     }       
     /*由mousePressed(),mouseReleased()取得示拖曳的开始与结束坐标*/
     public void mousePressed(MouseEvent e){
        startx=e.getX();
        starty=e.getY();
     }
     public void mouseReleased(MouseEvent e){
        endx=e.getX();
        endy=e.getY();
     }
     public void mouseEntered(MouseEvent e){
     }    
     public void mouseExited(MouseEvent e){
     }
     public void mouseClicked(MouseEvent e){
     }
     /*mouseMoved(),mouseDragged()取得鼠标移动的每一个坐标,并调用repaint()方法*/
     public void mouseMoved(MouseEvent e){
               flag=1;
               x=e.getX();
               y=e.getY();
               repaint();
     }
     public void mouseDragged(MouseEvent e){
               flag=2;
               x=e.getX();
               y=e.getY();
               repaint();
     }
     public void update(Graphics g){
          g.setColor(this.getBackground());
          g.fillRect(0,0,getWidth(),getHeight());
          paint(g);
     }
     public void paint(Graphics g){
          g.setColor(Color.black);
          if (flag==1){
             g.drawString("鼠标坐标:("+x+","+y+")",10,50);
             g.drawLine(startx,starty,endx,endy);
          }
          if (flag==2){
             g.drawString("拖曳鼠标价坐标:("+x+","+y+")",10,50);
             g.drawLine(startx,starty,x,y);
          }
     }
     public static void main(String[] args){
          new MouseDemo3();
     }
}

你可能感兴趣的:(String,swing,command,Class,import,interface)