java电子时钟显示屏_java 变色的Java电子时钟

[java]代码库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 ColockDemo 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 ColockDemo() {

this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

this.setResizable(false);

this.setBounds(100, 100, 400, 130);

this.setTitle("JAVA\\u8ff7\\u4f60\\u7535\\u5b50\\u8868");

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

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("C2killer ---- UNemployment 130 Day");

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

ColockDemo fd = new ColockDemo();

fd.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";

this.setText(now);

try {

Thread.sleep(1000);

} catch (InterruptedException e) {

}

}

}

}

}

//源代码片段来自云代码http://yuncode.net

694748ed64b9390909c0d88230893790.png

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值