-
Notifications
You must be signed in to change notification settings - Fork 1
/
TestMoveString.java
49 lines (44 loc) · 1.09 KB
/
TestMoveString.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
package test;
/**
* 第四题
* 在主窗体中显示随鼠标移动的helloworld
*/
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionListener;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class TestMoveString {
private static int x=200;
private static int y=300;
public static void main(String[] args) {
JFrame jf=new JFrame("移动的文字");
final JPanel jp=new JPanel(){
public void paint(Graphics g){
Graphics2D g2=(Graphics2D) g;
g2.setColor(Color.white);
g2.fillRect(0, 0, 500, 400);
g2.setColor(Color.black);
g2.drawString("helloWorld", x, y);
}
};
jp.addMouseMotionListener(new MouseMotionListener() {
@Override
public void mouseMoved(MouseEvent e) {
}
@Override
public void mouseDragged(MouseEvent e) {
// TODO Auto-generated method stub
x=e.getX();
y=e.getY();
jp.repaint();
}
});
jf.add(jp);
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jf.setSize(500, 400);
jf.setVisible(true);
}
}