簡體   English   中英

Java JOptionPane中的自動更新計時器

[英]Auto Updating Timer in JOptionPane in Java

我已經搜索過,但是找不到適合我需要的任何東西。 我正在做一個計時器,並且是Java的初學者。 我已經制作了計時器,並且可以使用,但是我想在窗戶上說剩余時間。 這是我所擁有的:

    package simpletimer;
import javax.swing.JOptionPane;
public class SimpleTimer {
    public static void main(String[] args) throws InterruptedException {
        JOptionPane.showMessageDialog(null, "Simple Timer By - -","Simple Timer", JOptionPane.INFORMATION_MESSAGE);
        double h = Integer.parseInt(JOptionPane.showInputDialog(null,"Hours:","Simple Timer",JOptionPane.PLAIN_MESSAGE));
        double m = Integer.parseInt(JOptionPane.showInputDialog(null,"Minutes:","Simple Timer",JOptionPane.PLAIN_MESSAGE));
        double s = Integer.parseInt(JOptionPane.showInputDialog(null,"Seconds:","Simple Timer",JOptionPane.PLAIN_MESSAGE));
        String Time = "Ok! The Simple Timer will go off in " + h + "h " + m + "m " + s + "s.";
        String[] Option1 = {"Start Timer"};
        JOptionPane.showOptionDialog(null, Time,"Simple Timer",JOptionPane.DEFAULT_OPTION,JOptionPane.INFORMATION_MESSAGE,null,Option1,"Start Timer");
        while(s > 0){
            Thread.sleep(1000);
            s = s - 1;
        }
        while(m > 0){
            m = m - 1;
            s = s + 59;
            Thread.sleep(60000);
        }
        while(h > 0){
            h = h - 1;
            m = m + 59;
            s = s + 59;
            Thread.sleep(3600000);
        }
        do{
                    JOptionPane.showMessageDialog(null, "Simple Timer Done!", "Simple Timer", JOptionPane.INFORMATION_MESSAGE);
        }while(h < 1 && m < 1 && s < 1);
    }
}

我解決了自己的問題。 我通過使用JFrame做到了這一點。 這是一個示例:

JFrame Main = new JFrame();
    JLabel Label = new JLabel();
    Main.add(Label);
    Main.setVisible(true);
    Main.setResizable(false);
    Main.setBounds(0, 0, 480, 200);
    Main.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    Label.setText("Time Left: " + h + ":" + m + ":" + s + ".");
    Label.setFont(Label.getFont().deriveFont(48f));
    while(s > 0){
        s = (short) (s - 1);
        Label.setText("Time Left: " + h + ":" + m + ":" + s + ".");
        Thread.sleep(1000);
    }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM