swing动态时间显示

:) 运行效果(当然是动态的):
[img]http://dl.iteye.com/upload/attachment/195975/46c1fdc0-b6c8-3aa9-ade0-90ad986f7c71.jpg[/img]
:) Code:

package test.gui;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.swing.JTextField;
import javax.swing.Timer;
import javax.swing.JFrame;

public class GUITimer extends JFrame{

//添加 显示时间的JTextField
private void addComponent(){
JTextField time = new JTextField();
this.getContentPane().add(time);
this.setTimer(time);
}
//显示窗口
private void showTime(){
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//this.pack();//自动适应窗口大小
this.setSize(160, 80);
this.setVisible(true);
}

//设置Timer 1000ms实现一次动作 实际是一个线程
private void setTimer(JTextField time){
final JTextField varTime = time;
Timer timeAction = new Timer(1000, new ActionListener() {

public void actionPerformed(ActionEvent e) {
long timemillis = System.currentTimeMillis();
//转换日期显示格式
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
varTime.setText(df.format(new Date(timemillis)));
}
});
timeAction.start();
}

public static void main(String[] args) {
GUITimer timeFrame = new GUITimer();
timeFrame.addComponent();
timeFrame.showTime();
}

}

你可能感兴趣的:(swing动态时间显示)