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实现的可定时到秒的秒表详解编程语言

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

原创文章,作者:奋斗,如若转载,请注明出处:https://blog.ytso.com/10620.html

(0)
上一篇 2021年7月19日
下一篇 2021年7月19日

相关推荐

发表回复

登录后才能评论