⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 timerecord.java

📁 1.用堆栈实现表达式求值 2.随机生成带括号的表达式
💻 JAVA
字号:
/**
 * 
 */
package sasa.view;

import java.awt.Graphics;
import java.awt.Color;
import javax.swing.JLabel;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @author sasa
 *
 * @version 1.0 2007-4-26 下午06:35:18
 */
public class TimeRecord extends JLabel implements Runnable{

	private static final long serialVersionUID = 1L;
	
	private volatile Thread thread = null;	//volatile关键字使thread在所有线程中同步
	
	private long startTime = 0;	//开始时间
	
	private long usedTime = 0;	//花费时间

	//绘制时间
	public void paint(Graphics g){
		SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss");
		Date timeRecord = null;
		try{
		timeRecord = sdf.parse("00:00:00");	//初始化时间格式
		}catch(Exception e){
			e.printStackTrace();
		}
		timeRecord.setTime(timeRecord.getTime()+usedTime);
		String strTime = sdf.format(timeRecord);	//Date类型数据的格式化
		//g.fill3DRect(0,0,78,28,true);
		g.setColor(Color.red);
		g.drawString(strTime,5,15);
	}
	
	//计时开始
	public void start(){
		startTime = System.currentTimeMillis();
		thread = new Thread(this);
		thread.start();
	}
	
	//实现Runnable中run()方法
	public void run(){
		Thread currentThread = Thread.currentThread();
		while(currentThread==thread){
			long time = System.currentTimeMillis();
			usedTime = time - startTime;
			try{
				Thread.sleep(1000);
			}catch(Exception e){
				e.printStackTrace();
			}
			repaint();
		}
	}
	
	//计时结束
	public void stop(){
		if(thread!=null)
			thread=null;
	}
}

⌨️ 快捷键说明

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