java學習筆記--------顯示時間

顯示時間的方法很多,這裏的方法比較簡單,代碼如下:

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();
	}

}

效果:


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章