2015-05-21 21 views
0

我已經搜索,但我找不到任何專門爲我需要的東西。我正在制定計時器,而且我是Java的初學者。我製作了計時器並且工作正常,但我想要一個窗口說剩下的時間。 這是我有:在Java中的JOptionPane自動更新計時器

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); 
    } 
} 

回答

0

我解決了我自己的問題。我通過使用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); 
    }