编写跟随鼠标跳动的文字的代码
package test19;import java.awt.Color;
import java.awt.EventQueue;
import java.awt.Point;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionAdapter;
import java.awt.event.MouseMotionListener;
import java.util.Random;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.border.EmptyBorder;
public class MouseDemo extends JFrame{
private Point mousePoint;
private int value1;
private JLabel label;
private JPanel contentPane;
private static final long serialVersionUID =-8501438415481947988l;
public static void main(String[]args){
try{
UIManager.setLookAndFeel("com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel");
}catch(Throwable e){
e.printStackTrace();
}
EventQueue.invokeLater(new Runnable(){
@Override
public void run() {
try{
MouseDemo mousedemo=new MouseDemo();
mousedemo.setVisible(true);
}catch(Exception e){
e.printStackTrace();
}
}
});
}
public MouseDemo(){
setTitle("跟随鼠标跳动的文字");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100,100,300,200);
contentPane=new JPanel();
contentPane.addMouseMotionListener(new MouseMotionAdapter() {
/* (非 Javadoc)
* @see java.awt.event.MouseMotionAdapter#mouseMoved(java.awt.event.MouseEvent)
*/
@Override
public void mouseMoved(MouseEvent e) {
label.setText("ganggang");
mousePoint=e.getPoint();
label.setLocation(mousePoint.x, mousePoint.y);
}
});
contentPane.setBorder(new EmptyBorder(5,5,5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
label=new JLabel("");
label.setBounds(6,6,60,18);
contentPane.add(label);
new Thread(new DynamicThread()).start();
}
private class DynamicThread implements Runnable {// 使用内部类完成标签移动操作
@Override
public void run() {
while (true) {
for (int i = 0; i < 400; i++) {
try {
Thread.sleep(10);// 线程休眠0.01秒
} catch (InterruptedException e) {
e.printStackTrace();
}
if (i % 20 == 0) {
int r = new Random().nextInt(256);
int g = new Random().nextInt(256);
int b = new Random().nextInt(256);
label.setForeground(new Color(r, g, b));
}
}
}
}
}
}
请各位学长帮我修改一下