码迷,mamicode.com
首页 > 编程语言 > 详细

JAVA秒表,可定时到秒!

时间:2015-06-16 09:24:44      阅读:178      评论:0      收藏:0      [点我收藏+]

标签:java   thread   源码   界面   

         Java秒表,实现了以秒计时和以圈计时!

话不多说,上源码!

  

package timerDemo;

import java.awt.BorderLayout;

public class TimerCountDemo extends JPanel implements Runnable,ActionListener{
	private ScrollPane scrollPane;
	private TextArea resultTimeText;
	private JLabel currentTimeLabel;
	private Calendar currentCalendar;
	private Date currentDate;
	private JButton startJButton;
	private JButton quanJButton;
	private JButton secondButton;
	private JButton stopJButton;
	private JButton showTimeJButton;
	private Thread t;
	private static int i=1;                          //圈数
	private static int hour,min,sec,msec,totalTime;
	private static boolean flag=true;
	
	
	public TimerCountDemo() {
		// TODO Auto-generated constructor stub
		startJButton=new JButton("开始");
		showTimeJButton=new JButton("00-00-00");
		showTimeJButton.setBackground(SystemColor.activeCaption);
		showTimeJButton.setFont(new Font("叶根友钢笔行书升级版", Font.PLAIN, 14));
		quanJButton=new JButton("以圈计时");
		quanJButton.setEnabled(false);        //初始化时设为false
		secondButton=new JButton("以秒计时");
		secondButton.setEnabled(false);       //初始化时设为false
		stopJButton=new JButton("停止");
		stopJButton.setBackground(Color.red);  //设置背景色为红色
		scrollPane=new ScrollPane();
		resultTimeText=new TextArea(20,20);
		resultTimeText.setEditable(false);
		scrollPane.setEnabled(true);
		currentCalendar=Calendar.getInstance();
		scrollPane.setBackground(Color.WHITE);
		currentTimeLabel=new JLabel();
		currentDate=new Date();
		currentTimeLabel.setText("当前时间:");
		
		this.setLayout(new BorderLayout());
		JPanel pNorth=new JPanel(new GridLayout(2,1));
		pNorth.add(currentTimeLabel);
		pNorth.add(showTimeJButton);
		this.add(pNorth,BorderLayout.NORTH);             //添加当前时间Label
		this.add(scrollPane,BorderLayout.CENTER);                   //添加
		JPanel panel=new JPanel(new GridLayout(2,2));
		panel.add(quanJButton);
		panel.add(secondButton);
		panel.add(startJButton);
		panel.add(stopJButton);
		this.add(panel,BorderLayout.SOUTH);
		t=new Thread(this);
		t.start();
		startJButton.addActionListener(this);
		quanJButton.addActionListener(this);
		secondButton.addActionListener(this);
		stopJButton.addActionListener(this);
	}
	@Override
	public void run() {
		// TODO Auto-generated method stub
		try {
			while(true){
				int mHour=new Date().getHours();
				int mMin=new Date().getMinutes();
				int mSec=new Date().getSeconds();
				int mTotalTime=mHour*3600+mMin*60+mSec;
				int diffHour=(mTotalTime-totalTime)/3600;
				int diffMin=(mTotalTime-totalTime)%3600/60;
				int diffSec=(mTotalTime-totalTime)%3600%60;
				Thread.sleep(10);
				currentTimeLabel.setText("当前时间:"+mHour+":"+mMin+":"+mSec);
				if(!flag){
					showTimeJButton.setText(""+diffHour+"-"+diffMin+"-"+diffSec);
				}//if(!flag)
			}//while
			
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
		
	}
	@SuppressWarnings("deprecation")
	@Override
	public void actionPerformed(ActionEvent e) {
		// TODO Auto-generated method stub
		if(e.getSource()==startJButton){
			hour= (new Date()).getHours();
			min=(new Date()).getMinutes();
		    sec=(new Date()).getSeconds();
		    totalTime=hour*3600+min*60+sec;
		    startJButton.setEnabled(false);
		    quanJButton.setEnabled(true);
		    secondButton.setEnabled(true);
		    flag=false;
		}
		//if(!flag){
			int xhour=new Date().getHours();           //获得此刻小时
			int xmin=new Date().getMinutes();          //获得此刻分钟
			int xSec=new Date().getSeconds();          //获得此刻秒
			int xTotalTime=xhour*3600+xmin*60+xSec;    //获取此刻总时间
			
			int bHour=(xTotalTime-totalTime)/3600;
			int bMin=(xTotalTime-totalTime)%3600/60;
			int bSec=(xTotalTime-totalTime)%3600%60;
			
			//quanJButton
			if(e.getSource()==quanJButton){
				resultTimeText.append("第"+(i++)+"圈:"+bHour+":"+bMin+":"+bSec+"\n");
				scrollPane.add(resultTimeText);
				secondButton.setEnabled(false);
			}//quanJButton
			
			//secondButton
			if(e.getSource()==secondButton){
				resultTimeText.append(bHour+":"+bMin+":"+bSec+"\n");
				scrollPane.add(resultTimeText);
				quanJButton.setEnabled(false);
			}//secondButton
			
			//stopJButton
			if(e.getSource()==stopJButton){
				if(stopJButton.getText()=="停止"){
					//System.out.println("Test停止");
					hour=0;
					sec=0;
					min=0;
					stopJButton.setText("清除");
					quanJButton.setEnabled(false);
					secondButton.setEnabled(false);
					flag=true;
				}//stopJButton.getText()=="停止"
				else if(stopJButton.getText()=="清除"){
					i=1;
					stopJButton.setText("停止");
					resultTimeText.setText("");
					startJButton.setEnabled(true);
					showTimeJButton.setText("00-00-00");
				}//stopJButton.setText("清除");
			}//stopJButton
		}//if(!flag)
	//	flag=false;
	//}//if(!flag)
	public static void main(String args[]){
		JFrame jf=new JFrame("Demo");
		try{
			UIManager.setLookAndFeel("com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel");
		}catch(Exception e){
			e.printStackTrace();
		}
		TimerCountDemo xCountDemo=new TimerCountDemo();
		jf.setBounds(200,200,250,300);
		jf.getContentPane().add(xCountDemo);
		jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		jf.setVisible(true);
	}
}

初始化界面

技术分享


开始

技术分享


以圈计时

技术分享


以圈计时

技术分享


以秒计时

技术分享


源码地址

JAVA秒表,可定时到秒!

标签:java   thread   源码   界面   

原文地址:http://blog.csdn.net/carvin_zh/article/details/46509087

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!