捕捉鼠标拖动和鼠标移动事件

import java.awt.*;
import java.awt.event.*;
import java.awt.FlowLayout;
import javax.swing.JFrame;
import javax.swing.JButton;
public class duozhongListener implements
                              MouseMotionListener,MouseListener,ActionListener{
 
 
 
  JFrame f;        //JFrame通常默认使用BorderLayout布局管理器的
     TextArea tf;
     JButton bt;
     int number = 1;
     public duozhongListener(){
      Label label = new Label("click and drag the mouse");
      f = new JFrame("duozhongListener");
      tf = new TextArea();
      bt = new JButton("退出");
     
      tf.addMouseMotionListener(this);
      tf.addMouseListener(this);
      bt.addActionListener(this);
     
      f.add(label,BorderLayout.NORTH);
      f.add(tf,BorderLayout.CENTER);
      f.add(bt,BorderLayout.SOUTH);
      f.setSize(300,200);
      f.setVisible(true);
     
     }
 
 public static void main(String[] args) {
  // TODO Auto-generated method stub
  duozhongListener duozhong = new duozhongListener();
       
 }
 @Override    //负责处理鼠标拖动事件
 public void mouseDragged(MouseEvent e) {
  // TODO Auto-generated method stub
  String s = number+++""+"the mouse dragger:x="+e.getX()+"y="+e.getY()+"\n";
  tf.append(s);
 }
 @Override   //负责处理鼠标移动事件
 public void mouseMoved(MouseEvent e) {
  // TODO Auto-generated method stub
  String s = number+++""+"the mouse entered"+"\n";
  tf.append(s);
  
 }
 @Override
 public void mouseClicked(MouseEvent arg0) {
  // TODO Auto-generated method stub
  
 }
 @Override
 public void mouseEntered(MouseEvent arg0) {
  // TODO Auto-generated method stub
  
 }
 @Override
 public void mouseExited(MouseEvent arg0) {
  // TODO Auto-generated method stub
  
 }
 @Override
 public void mousePressed(MouseEvent arg0) {
  // TODO Auto-generated method stub
  
 }
 @Override
 public void mouseReleased(MouseEvent arg0) {
  // TODO Auto-generated method stub
  
 }
 @Override
 public void actionPerformed(ActionEvent arg0) {
  // TODO Auto-generated method stub
  System.exit(0);
 }
}

你可能感兴趣的:(捕捉鼠标拖动和鼠标移动事件)