2016-03-04 43 views
1

問題是我沒有得到任何錯誤,但計時器沒有被添加到框架。反覆「越來越」變化int

比方說我有三大類:InFrameClock,鐘錶和TwoPlayer

TimeKeeper的包含增加一個int 「計數器」 每秒擺動計時器:

import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 
import javax.swing.JPanel; 
import javax.swing.Timer; 

public class TimeKeeper extends JPanel { 
private Timer timer; 
private int delay = 1000; // every 1 second 
private ActionListener action; 
private int counter = 0; 

public TimeKeeper() { 

    action = new ActionListener() { 
     @Override 
     public void actionPerformed(ActionEvent event) { 
        counter++; 

     }; 
     }; 
} 

public boolean isTimerRunning() { 
    return timer != null && timer.isRunning(); 
} 

public void startTimer() { 
    timer = new Timer(delay, action); 
    timer.setInitialDelay(0); 
    timer.start(); 
} 

public void stopTimer() { 
    if (timer != null && timer.isRunning()) { 
     timer.stop(); 
     timer = null; 
     counter = 0; 
    } 
} 

public int getCounter() { 
    return counter; 
} 

public void setCounter(int counter) { 
    this.counter = counter; 
} 
} 

^顯然,這些都是其他地方使用,但我會到達那個有點

的TwoPlayer類啓動和停止計時器:

import java.awt.FlowLayout; 
import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 
import java.awt.event.WindowEvent; 
import javax.swing.JButton; 
import javax.swing.JFrame; 
import javax.swing.JLabel; 
import javax.swing.JPanel; 

public class TwoPlayer { 
private JFrame mainFrame; 
private TimeKeeper myTimeKeeper; 

public TwoPlayer() { 
    mainFrame = new JFrame("Two Player"); 
    mainFrame.setSize(325, 135); 
    mainFrame.setLocation(600, 300); 
    mainFrame.setLayout(new FlowLayout()); 

    myTimeKeeper = new TimeKeeper(); 

    JButton button1 = new JButton("Start/Stop"); 
    button1.addActionListener(new ActionListener() { 
     public void actionPerformed(ActionEvent e) { 
      if (myTimeKeeper.isTimerRunning()) { 
       myTimeKeeper.stopTimer(); 
      } 
      else { 
        myTimeKeeper.startTimer(); 
       } 
     } 
      }); 

    JPanel panel = new JPanel(); 
    panel.setLayout(new FlowLayout()); 
    InFrameClock clock = new InFrameClock(myTimeKeeper); 
    mainFrame.setVisible(true); 
    mainFrame.add(button1); 
    mainFrame.add(clock); 
} 
} 

上述被添加到幀的對象「時鐘」來自下面的類:

import java.awt.GridBagLayout; 
import javax.swing.JLabel; 
import javax.swing.JPanel; 

public class InFrameClock extends JPanel{ 
private JLabel clock = new JLabel(); 
private int timeSec; 
private int timeMin; 

TimeKeeper myTimeKeeper; 

public InFrameClock(TimeKeeper timeKeeper){ 
    this.myTimeKeeper = timeKeeper; 
    int clockVal = timeKeeper.getCounter(); 
    clock.setText(String.valueOf(clockVal)); 

    while (clockVal <= 0){ 
     clockVal = timeKeeper.getCounter(); 

    if(clockVal >= 60){ 
     timeSec = clockVal % 60; 
     timeMin = (clockVal - timeSec)/60; 
     clock.setText(String.valueOf(timeMin) + 
     ":" + String.valueOf(timeSec)); 
    } 
    else{ 
     clock.setText(String.valueOf(clockVal)); 
    } 

    setLayout(new GridBagLayout()); 
    add(clock); 

    } 
} 
} 

我覺得我在做什麼:在TimeKeeper的初始化計數器爲0,開始TimeKeeper的與TwoPlayer啓動按鈕,並獲得計數器的值,只要它小於或等於0.

這裏的想法是,我在窗口中創建了一個「時鐘」,它顯示了從幾分鐘後開始的時間值以及秒格式。

+1

您能否澄清一下您的問題? – BRasmussen

+0

哦!抱歉,問題在於我沒有收到任何錯誤,但計時器未被添加到框架中 – CodeBoy

+0

您的代碼中存在錯誤,導致我們無法複製並粘貼它並運行它。如果你可以將代碼合併成一個有效的[mcve],並且讓我們省去嘗試調試一堆錯誤的麻煩,那更好。 –

回答

4

您已經在Swing事件線程上運行了一個while循環並將其凍結。從事件線索中獲取此信息。更好的是,改變你的代碼,這樣你就不需要while循環。相反,使用Swing Timer的actionPerformed作爲事件來改變視圖的狀態,或者使用觀察者模式來通知視圖對定時器計數器的更改。

+0

謝謝!有意義的是while循環將所有東西都保存起來,而不是提到壞的位置。 – CodeBoy

4

作爲clockVal是計數器從0增加1,while條件將在開始時僅一次fullfilled

while (clockVal <= 0){ 

的位進一步

if(clockVal >= 60){ 

不能變得容易成爲真正的,作爲毫秒之間通過<= 0<= 60

另外一個構造函數中的忙循環沒有意義;不是基於預期的基於事件的GUI模型。

+0

............很好的眼睛。 1+ –