Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package ui;
- import java.awt.BorderLayout;
- import java.awt.event.ActionEvent;
- import java.awt.event.ActionListener;
- import javax.swing.JButton;
- import javax.swing.JFrame;
- import javax.swing.JLabel;
- import javax.swing.JOptionPane;
- public class CountdownFrame extends JFrame {
- private JLabel label;
- public CountdownFrame() {
- label = new JLabel();
- add(label);
- JButton button = new JButton("Click me, now!");
- button.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent e) {
- (new Thread(new CountDown())).start();
- }
- });
- add(button, BorderLayout.SOUTH);
- setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- setSize(160, 160);
- setVisible(true);
- }
- class CountDown implements Runnable {
- public void run() {
- int hours = 0, minutes = 1, seconds = 5;
- boolean kt=true;
- while (kt) {
- try {
- Thread.sleep(1000);
- } catch (InterruptedException x) {
- }
- seconds -= 1;
- if (seconds == -1) {
- minutes -= 1;
- seconds = 59;
- }
- if (minutes == -1) {
- minutes = 59;
- hours -= 1;
- }
- String h = hours<10 ? "0"+hours : ""+hours;
- String m = minutes<10 ? "0"+minutes : ""+minutes;
- String s = seconds<10 ? "0"+seconds : ""+seconds;
- label.setText(h+":"+m+":"+s);
- if(hours==0 && minutes==0 && seconds==0){
- JOptionPane.showMessageDialog(null, "Hết con mẹ nó giờ rồi");
- kt = false;
- }
- }
- }
- }
- public static void main(String[] args) {
- CountdownFrame frame = new CountdownFrame();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement