反复“获得”改变的int

时间:2016-03-04 21:07:46

标签: java swing timer

问题是我没有收到任何错误,但计时器没有被添加到框架中。

假设我有三个类:InFrameClock,TimeKeeper和TwoPlayer

TimeKeeper包含一个swing计时器,每秒递增一个int“counter”:

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,使用TwoPlayer中的开始按钮启动TimeKeeper,并且只要它小于或等于0,就获得计数器的值。

这里的想法是我在窗口中创建一个“时钟”,显示自分钟和秒格式开始以来的时间值。

2 个答案:

答案 0 :(得分:4)

由于clockVal是从0增加1的计数器,while条件将在开头只填充一次

while (clockVal <= 0){

再远一点

if(clockVal >= 60){

不能轻易成为现实,因为<= 0<= 60之间经过了几毫秒。

构造函数中的繁忙循环也没有意义;不是基于事件的GUI模型。

答案 1 :(得分:4)

你在Swing事件线程上运行了一个while循环并冻结它。从事件线程中获取此信息。更好的是,改变你的代码,这样你就不需要循环了。而是使用您的Swing Timer的actionPerformed作为事件来更改视图的状态,或者使用观察者模式让视图通知计时器计数器的更改。