clstimertime.java

来自「Hi, this program is for my Java assignme」· Java 代码 · 共 39 行

JAVA
39
字号
/**
* Used to get the system runing time and display it in a JLabel
*/
import java.util.Date;
import java.util.Timer;
import java.util.Calendar;
import javax.swing.JLabel;
import java.util.TimerTask;
import java.text.DateFormat;

public class clsTimerTime {
//-------------------------------------------- TimerTime Constructor
	public clsTimerTime(JLabel label, int seconds) {
		timer = new Timer();
		timer.scheduleAtFixedRate(new clsTime(label), 0, seconds * 1000);
	}

//-------------------------------------------- TimerTime inner class
	public class clsTime extends TimerTask {
		private JLabel label    = new JLabel();
		private boolean visible = false;

		public clsTime(JLabel label) { this.label = label; }
		public void run() {
			cal  = Calendar.getInstance();
			date = cal.getTime();
			dateFormatter = DateFormat.getTimeInstance();
			label.setText(dateFormatter.format(date) + " ");
		}
	}

	//<!-- DECLARE_VARIABLES
	private Date date;
	private Timer timer;
	private Calendar cal;
	private DateFormat dateFormatter;
	//-->
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?