import java.awt.Color;
import java.awt.EventQueue;
import java.awt.Font;
import java.util.Date;
import java.util.Random;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.border.LineBorder;
import javax.swing.border.TitledBorder;

public class ZuiDaiMaColock extends JFrame {

    /**
     * 
     */
    private static final long serialVersionUID = 1L;
    //候选颜色
    private final Color colors[] = { Color.BLACK, Color.BLUE, Color.CYAN,
            Color.DARK_GRAY, Color.GRAY, Color.YELLOW, Color.RED, Color.GREEN ,
            Color.MAGENTA, Color.PINK };

    public ZuiDaiMaColock() {
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setResizable(false);
        this.setBounds(100, 100, 400, 130);
        //this.setTitle("JAVA\\u8ff7\\u4f60\\u7535\\u5b50\\u8868");
        this.setTitle("JAVA Swing彩色时钟");
        this.getContentPane().setLayout(null);

        JPanel panel = new JPanel();
        panel.setBounds(7, 10, 380, 73);
        //panel.setBorder(new TitledBorder(new LineBorder(Color.BLACK), "\\u65f6\\u95f4(T)"));
        panel.setBorder(new TitledBorder(new LineBorder(Color.BLACK), "当前时间:"));

        final TTimerLable clock = new TTimerLable();
//      clock.setForeground(Color.BLUE);
        Runnable color = new Runnable(){
            private boolean flag = true;
            private Random r = new Random();
            @Override
            public void run() {
                // TODO Auto-generated method stub
                while(flag){
                    try {
                        int index = r.nextInt(10);
                        Thread.sleep(1000);
//                      System.out.print(index + " ");
                        clock.setForeground(colors[index]);
                    } catch (InterruptedException e) {}
                }
            }

        };
        Thread colorTh = new Thread(color);
        colorTh.start();

        clock.setFont(new Font("隶书", Font.BOLD, 24));
        panel.setLayout(null);
        clock.setBounds(80, 10, 380, 60);
        panel.add(clock);
        getContentPane().add(panel);

        JLabel lblCkiller = new JLabel("来自:http://outofmemory.cn");
        lblCkiller.setBounds(169, 90, 215, 15);
        getContentPane().add(lblCkiller);

        clock.start();

    }

    public static void main(String [] args){
        EventQueue.invokeLater(new Runnable(){

            @Override
            public void run() {
                // TODO Auto-generated method stub
            	ZuiDaiMaColock zdmC = new ZuiDaiMaColock();
            	zdmC.setVisible(true);
            }});
    }

    // 显示时间的 Lable
    class TTimerLable extends JLabel implements Runnable {
        /**
         * 
         */
        private static final long serialVersionUID = 1L;
        private Date date = null;
        private boolean flag = true;
        private Thread thread = new Thread(this);

        public TTimerLable() {
            super();
        }

        public void start(){
            thread.start();
        }

        @Override
        public void run() {
            // TODO Auto-generated method stub
            while(flag) {
                date = new Date();
                //String now = date.getHours() + " \\u70b9 " + date.getMinutes() + " \\u5206 " + date.getSeconds() + "\\u79d2";
                String now = date.getHours() + " 时 " + date.getMinutes() + " 分 " + date.getSeconds() + " 秒";
                this.setText(now);
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                }
            }
        }

    }
}
//该片段来自于http://outofmemory.cn
最近下载更多
459232078  LV2 2021年4月23日
15966848095  LV22 2020年1月12日
anzhenhao  LV1 2019年9月19日
dtldxl  LV2 2019年9月4日
haha19961113  LV1 2019年8月10日
1848954545  LV1 2019年1月1日
蒋水艳  LV1 2018年9月13日
张锦徳  LV3 2018年7月12日
Weipeng_  LV14 2018年6月29日
378002843  LV9 2018年6月5日
最近浏览更多
星空Starry-Sky  LV6 3月1日
drawing 2023年11月6日
暂无贡献等级
卡布琪洛  LV4 2023年5月28日
另类清晨  LV8 2023年5月15日
微信网友_6425728903319552  LV1 2023年4月9日
cksndh  LV4 2023年2月21日
Willson88888  LV3 2022年12月7日
无敌大富富富翁  LV2 2022年11月26日
3233497907  LV1 2022年10月21日
AngelW  LV10 2022年4月11日
顶部 客服 微信二维码 底部
>扫描二维码关注最代码为好友扫描二维码关注最代码为好友