加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Java > 正文

JAVA实现的可定时到秒的秒表

发布时间:2020-12-14 23:16:56 所属栏目:Java 来源:网络整理
导读:今天PHP站长网 52php.cn把收集自互联网的代码分享给大家,仅供参考。 package timerDemo; import java.awt.BorderLayout; public class TimerCountDemo extends JPanel implements Runnable,ActionListener{ private Scro

以下代码由PHP站长网 52php.cn收集自互联网

现在PHP站长网小编把它分享给大家,仅供参考

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



来自:http://blog.csdn.net/carvin_zh/article/details/46509087

以上内容由PHP站长网【52php.cn】收集整理供大家参考研究

如果以上内容对您有帮助,欢迎收藏、点赞、推荐、分享。

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读