java

位置:IT落伍者 >> java >> 浏览文章

Java 鼠标事件


发布日期:2023年02月02日
 
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);

}

}

               

上一篇:java正则表达式匹配

下一篇:Java编程语言程序的认识误区