熱點推薦:
您现在的位置: 電腦知識網 >> 編程 >> Java編程 >> Java核心技術 >> 正文

Java 鼠標事件

2013-11-23 18:44:08  來源: Java核心技術 

  import javaawt*;

  import javaawtevent*;

  import javaxswing*;

  public class MoveMessageDemo extends JFrame {

  public MoveMessageDemo() {

  MoveMessagePanelNew p = new MoveMessagePanelNew(Welcome to Java);

  getContentPane()setLayout(new BorderLayout());

  getContentPane()add(p);

  }

  /** Main method */

  public static void main(String[] args) {

  MoveMessageDemo frame = new MoveMessageDemo();

  framesetTitle(MoveMessageDemo);

  framesetDefaultCloseOperation(JFrameEXIT_ON_CLOSE);

  framesetSize( );

  framesetVisible(true);

  }

  }

  // MoveMessagePanel draws a message

  class MoveMessagePanel implements MouseMotionListener {

  public void mouseMoved(MouseEvent e) {

  }

  /** Handle mouse dragged event */

  public void mouseDragged(MouseEvent e) { //鼠標按鍵在組件上按下並拖動時調用

  // Get the new location and repaint the screen

  Systemoutprintln(egetX());

  Systemoutprintln(egetY());

  }

  }

  // MoveMessagePanelNew draws a message

  class MoveMessagePanelNew extends JPanel implements MouseMotionListener {

  private String message = Welcome to Java;

  private int x = ;

  private int y = ;

  public MoveMessagePanelNew(String s) {

  message = s;

  thisaddMouseMotionListener(this);

  }

  public void mouseMoved(MouseEvent e) {

  }

  public void mouseDragged(MouseEvent e) {

  // Get the new location and repaint the screen

  x = egetX();

  y = egetY();

  repaint();

  }

  protected void paintComponent(Graphics g) {

  superpaintComponent(g);

  gdrawString(message x y);

  }

  }


From:http://tw.wingwit.com/Article/program/Java/hx/201311/25637.html
    推薦文章
    Copyright © 2005-2013 電腦知識網 Computer Knowledge   All rights reserved.