标题:编写跟随鼠标跳动的文字的代码
只看楼主
蔡包小童鞋
Rank: 1
等 级:新手上路
帖 子:1
专家分:0
注 册:2014-10-26
 问题点数:0 回复次数:0 
编写跟随鼠标跳动的文字的代码
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));
                        }
                    }
                }
            }
        }

}
请各位学长帮我修改一下
搜索更多相关主题的帖子: private package public import 
2014-10-26 08:02



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-437702-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.157126 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved