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);
}
}