顯示時間的方法很多,這裏的方法比較簡單,代碼如下:
package Testing3;
import javax.swing.*;
import javax.swing.Timer;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.*;
public class Testing3 {
JFrame jf = new JFrame();
JLabel jl ;
//注意使用的是javax.swing的Timer
//該Timer可以定時處理Action事件
Timer t;
//定義Timer要處理的事件
ActionListener timeActionListener = new ActionListener(){
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
jl.setText(Calendar.getInstance().getTime().toString());
}
};
public Testing3(){
jl = new JLabel(Calendar.getInstance().getTime().toString());
//定義完整 並啓動
t = new Timer(1000,timeActionListener);
t.start();
jf.add(jl);
jf.pack();
jf.setVisible(true);
}
public static void main(String[] args) {
// TODO Auto-generated method stub
Testing3 test = new Testing3();
}
}
效果: