- 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://www.codesnippet.cn/detail/011120136880.html
来源: http://www.codesnippet.cn/detail/011120136880.html