2012-12-10 57 views
1

我需要一個計時器在我的應用程序中執行,它將從10秒到0秒進行倒計時。 ,並在JLabel中顯示倒計時。顯示倒計時

這是我的實現;

... 
Timer t = new Timer(1000, new List()); 
     t.start(); 

} 

class List implements ActionListener{ 

    @Override 
    public void actionPerformed(ActionEvent e) { 
     int sec = 0; 
     label.setText(""+sec); 
     // Do a if- condition check to see if the clock has reached to, and then stop 

    } 

} 

我期待着JLabel從0-10開始計數然後停下來。但事實並非如此。 JLabel設置值0並且它不會遞增。

UPDATE 1

t = new Timer(1000, new Listner()); 
    t.start(); 


} 

class Listner implements ActionListener{ 
    private int counter = 0; 
    @Override 
    public void actionPerformed(ActionEvent e) { 
     lable.setText(""+ (counter++)); 

     if (counter == 10) 
       t.removeActionListener(this); 

    } 

} 

回答

4

你是不是存儲也不遞增secs任何地方,所以我不知道應該如何得到更新,以

Timer timer; 

void start() { 
    timer = new Timer(1000,new List()); 
} 

class List implements ActionListener { 
    private counter = 0; 
    @Override 
    public void actionPerformed(ActionEvent e) { 
     label.setText(""+counter++); 

     if (counter == 10) 
      timer.removeActionListener(this); 
    } 
} 

心靈嘗試,你需要存儲到計時器參考的地方是能夠在倒計時完成後從中刪除監聽者。

+0

不會從每次調用actionPerformed時計數器從0開始? – Fyre

+0

@Fyre編號'私人計數器= 0;'創建類時創建。在'actionPerformed'中'counter'只是增加了。 –

+0

噢耶我的壞沒有看到它的類變量....謝謝 – Fyre

4

那麼每個計時器被稱爲它的時間聲明int變量秒到0。因此標籤犯規得到更新。

您應該將sec變量聲明爲全局變量,然後在actionPerformed方法中每次調用時都會增加其值。

public int sec = 0; 
class List implements ActionListener{ 

    @Override 
    public void actionPerformed(ActionEvent e) { 
     sec++; 
     label.setText(""+sec); 
     // Do a if- condition check to see if the clock has reached to, and then stop 

    } 

} 
+0

我推薦交換順序並使'sec'成爲監聽器類的一個屬性。例如。 'class List實現ActionListener {private int sec = 0; ..' –

+0

計數器不從'0'開始。相反,它從'8'開始,然後結束於9.這是因爲我停止計時器在'10' – user1315906

3

一個完整的例子

public class ATimerExample { 

    Timer timer; 
    int counter = 0; 

    public ATimerExample() { 
     final JFrame frame = new JFrame("somethgi"); 
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     final JLabel label = new JLabel("0"); 
     JPanel panel = new JPanel(); 
     panel.add(label, BorderLayout.SOUTH); 
     frame.getContentPane().add(panel); 
     frame.pack(); 
     frame.setVisible(true); 

     timer = new Timer(1000, new ActionListener() { 
      @Override 
      public void actionPerformed(ActionEvent e) { 
       label.setText(String.valueOf(counter)); 
       counter++; 
       if (counter == 10) { 
        //timer.removeActionListener(this); 
         timer.stop(); 
       } 
      } 
     }); 
     timer.start(); 
    } 

    public static void main(String[] args) { 
     SwingUtilities.invokeLater(new Runnable() { 

      @Override 
      public void run() { 
       new ATimerExample(); 
      } 
     }); 
    } 
} 
+0

@mKorbel :-)謝謝。更新。 – vels4j

1

由於Java讀取以毫秒爲單位的時間,應該是10000,而不是1000嘗試你的代碼,看看是否能工程。當我想要30秒時,我遇到了同樣的問題。而不是寫定時器t =新的定時器(30000,新的列表()); t.start();

我寫了Timer t = new Timer(3000,new List()); t.start();

這使得我的程序在3秒後停止。我建議,你使用10000而不是1000.

記得要做:t.stop()在你的List類中。謝謝