JAVA秒錶,可定時到秒!

來源:互聯網
上載者:User

標籤: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 stubstartJButton=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);        //初始化時設為falsesecondButton=new JButton("以秒計時");secondButton.setEnabled(false);       //初始化時設為falsestopJButton=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);             //添加目前時間Labelthis.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);}@Overridepublic void run() {// TODO Auto-generated method stubtry {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 exceptione.printStackTrace();}}@SuppressWarnings("deprecation")@Overridepublic void actionPerformed(ActionEvent e) {// TODO Auto-generated method stubif(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;//quanJButtonif(e.getSource()==quanJButton){resultTimeText.append("第"+(i++)+"圈:"+bHour+":"+bMin+":"+bSec+"\n");scrollPane.add(resultTimeText);secondButton.setEnabled(false);}//quanJButton//secondButtonif(e.getSource()==secondButton){resultTimeText.append(bHour+":"+bMin+":"+bSec+"\n");scrollPane.add(resultTimeText);quanJButton.setEnabled(false);}//secondButton//stopJButtonif(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秒錶,可定時到秒!

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.