Advertisement
huyhung94

CountDownSwing.java

Mar 19th, 2015
233
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.99 KB | None | 0 0
  1. package ui;
  2.  
  3. import java.awt.BorderLayout;
  4. import java.awt.event.ActionEvent;
  5. import java.awt.event.ActionListener;
  6. import javax.swing.JButton;
  7. import javax.swing.JFrame;
  8. import javax.swing.JLabel;
  9. import javax.swing.JOptionPane;
  10.  
  11. public class CountdownFrame extends JFrame {
  12.  
  13.     private JLabel label;
  14.  
  15.     public CountdownFrame() {
  16.         label = new JLabel();
  17.         add(label);
  18.         JButton button = new JButton("Click me, now!");
  19.         button.addActionListener(new ActionListener() {
  20.  
  21.             public void actionPerformed(ActionEvent e) {
  22.                 (new Thread(new CountDown())).start();
  23.             }
  24.         });
  25.         add(button, BorderLayout.SOUTH);
  26.  
  27.         setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  28.         setSize(160, 160);
  29.         setVisible(true);
  30.     }
  31.  
  32.     class CountDown implements Runnable {
  33.  
  34.         public void run() {
  35.             int hours = 0, minutes = 1, seconds = 5;
  36.             boolean kt=true;
  37.             while (kt) {
  38.                 try {
  39.                     Thread.sleep(1000);
  40.                 } catch (InterruptedException x) {
  41.                 }
  42.                 seconds -= 1;
  43.                 if (seconds == -1) {
  44.                     minutes -= 1;
  45.                     seconds = 59;
  46.                 }
  47.                 if (minutes == -1) {
  48.                     minutes = 59;
  49.                     hours -= 1;
  50.                 }
  51.                 String h = hours<10 ? "0"+hours : ""+hours;
  52.                 String m = minutes<10 ? "0"+minutes : ""+minutes;
  53.                 String s = seconds<10 ? "0"+seconds : ""+seconds;
  54.                
  55.                 label.setText(h+":"+m+":"+s);
  56.                 if(hours==0 && minutes==0 && seconds==0){
  57.                     JOptionPane.showMessageDialog(null, "Hết con mẹ nó giờ rồi");
  58.                     kt = false;
  59.                 }
  60.             }
  61.  
  62.         }
  63.     }
  64.  
  65.     public static void main(String[] args) {
  66.         CountdownFrame frame = new CountdownFrame();
  67.     }
  68. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement