Stopwatch

From , 5 Years ago, written in Java, viewed 200 times.
URL https://pastebin.vip/view/28fc2782
  1. package Day0822;
  2.  
  3. import java.awt.event.*;
  4. import javax.swing.*;
  5. import java.awt.*;
  6.  
  7. public class HomeWork implements ActionListener,Runnable{
  8.         private int x = 0;
  9.         private int y = 0;
  10.         private int z = 0;
  11.         private int num = 0;
  12.         private JFrame jf;
  13.         private JPanel jp;
  14.         private JButton jb;
  15.         private JButton jb1;
  16.         private JButton jb2;
  17.         private JTextField jtf;
  18.         private boolean flag = true;
  19.         private Thread thread = new Thread(this);
  20.         public void init(){
  21.                 jf = new JFrame("*********秒表*********");
  22.                 jp = new JPanel();
  23.                 jb = new JButton("开始");
  24.                 jb1 = new JButton("暂停");
  25.                 jb2 = new JButton("重置");
  26.                 jtf = new JTextField(9);
  27.                
  28.                 jf.setSize(300,150);
  29.                 jf.setLocationRelativeTo(null);
  30.                 jf.setLayout(new FlowLayout());
  31.                 jf.setResizable(false);
  32.                
  33.                 jtf.setFont(new Font("楷体",1,50));
  34.                 jtf.setHorizontalAlignment(JTextField.CENTER);
  35.                 jtf.setText(setTime());
  36.                
  37.                 jb.addActionListener(this);
  38.                 jb1.addActionListener(this);
  39.                 jb2.addActionListener(this);
  40.                
  41.                 jf.add(jtf);
  42.                 jf.add(jb);
  43.                 jf.add(jb1);
  44.                 jf.add(jb2);
  45.                 jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  46.                 jf.setVisible(true);
  47.         }
  48.        
  49.         public void actionPerformed(ActionEvent e){
  50.                 if(e.getSource().equals(jb)){
  51.                         flag  = true;
  52.                         new Thread(this).start();
  53.                 }
  54.                 if(e.getSource().equals(jb1)){
  55.                         flag = false;
  56.                 }
  57.                 if(e.getSource().equals(jb2)){
  58.                         flag = false;  //线程终止
  59.                         num = 0;
  60.                         x = 0;
  61.                         y = 0;
  62.                         z = 0;
  63.                         jtf.setText("0:0:0");
  64.                        
  65.                 }
  66.         }
  67.         public  void run(){
  68.                 while(flag){
  69.                         try {
  70.                                 Thread.sleep(1000);
  71.                                 num = num+1;
  72.                                 z=(num)%60;
  73.                                 y = (num/60)%60;
  74.                                 x = (num/3600)%24;
  75.                         } catch (InterruptedException e) {
  76.                                 e.printStackTrace();
  77.                         }finally{
  78.                                 jtf.setText(setTime());
  79.                         }
  80.                 }
  81.         }
  82.        
  83.         public String setTime(){
  84.                 return x+":"+y+":"+z;
  85.         }
  86.         public String setTime1(){
  87.                 return "0"+x+":0"+y+":0"+z;
  88.         }
  89.  
  90.         public static void main(String[] arge){
  91.                 HomeWork hw = new HomeWork();
  92.                 hw.init();
  93.         }
  94. }
  95.  
  96.  

Reply to "Stopwatch"

Here you can reply to the paste above

captcha

https://burned.cc - Burn After Reading Website