testmouseevent.java

来自「事件的处理」· Java 代码 · 共 75 行

JAVA
75
字号
import java.awt.*;
import java.awt.event.*;
public class TestMouseEvent extends Frame implements MouseListener,MouseMotionListener{

  private TextArea testTA;
  private Label testLabel;

  public static void main(String args[]){

    TestMouseEvent test=new TestMouseEvent();
    test.setSize(300,200);
    test.setVisible(true);

  }

  public TestMouseEvent(){

    testTA=new TextArea();
    testTA.addMouseListener(this);
    testTA.addMouseMotionListener(this);
    testLabel=new Label();
    add(testTA,BorderLayout.CENTER);
    add(testLabel,BorderLayout.SOUTH);
  
  }

  public void mouseClicked(MouseEvent e){

    if ((e.getModifiers() & MouseEvent.META_MASK)==0)
      testTA.append("left");
    else
      testTA.append("right");
    testTA.append(" click "+e.getClickCount()+" times\n");

  }

  public void mouseEntered(MouseEvent e){

    testTA.append("enter\n");
    
  }

  public void mouseExited(MouseEvent e){

    testTA.append("exit\n");
    
  }

  public void mousePressed(MouseEvent e){

    testTA.append("press\n");
    
  }


  public void mouseReleased(MouseEvent e){

    testTA.append("release\n");
    
  }

  public void mouseDragged(MouseEvent e){

    testTA.append("drag\n");
    
  }

  public void mouseMoved(MouseEvent e){

    testLabel.setText(e.getX()+","+e.getY());
    
  }

}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?