Java 反复地;获得;变化的整数

Java 反复地;获得;变化的整数,java,swing,timer,Java,Swing,Timer,问题是我没有收到任何错误,但是计时器没有被添加到帧中 假设我有三门课:次元锁、计时员和两名球员 计时器包含一个摆动计时器,每秒递增一个整数“计数器”: import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JPanel; import javax.swing.Timer; public class TimeKeeper extends JPanel { privat

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

假设我有三门课:次元锁、计时员和两名球员

计时器包含一个摆动计时器,每秒递增一个整数“计数器”:

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

    }
}   
}
导入java.awt.GridBagLayout;
导入javax.swing.JLabel;
导入javax.swing.JPanel;
公共类InFrameClock扩展了JPanel{
专用JLabel时钟=新JLabel();
私人时间;
私用int-timeMin;
计时员我的计时员;
公共基础设施锁(计时器计时器){
this.myTimeKeeper=计时器;
int clockVal=timeKeeper.getCounter();
clock.setText(String.valueOf(clockVal));
while(clockVal=60){
timeSec=clockVal%60;
timeMin=(clockVal-timeSec)/60;
clock.setText(String.valueOf(timeMin)+
“:”+String.valueOf(timeSec));
}   
否则{
clock.setText(String.valueOf(clockVal));
}
setLayout(新的GridBagLayout());
添加(时钟);
}
}   
}
我想我正在做的是:在TimeKeeper中将计数器初始化为0,在TwoPlayer中使用start按钮启动TimeKeeper,只要计数器的值小于或等于0,就获取计数器的值


这里的想法是,我在我的窗口中创建一个“时钟”,它以分秒格式显示启动后的时间值

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

while (clockVal <= 0){

无法轻松变为真,因为在
之间经过了毫秒,因为clockVal是计数器,从0增加了1,while条件将在开始时仅满填充一次

while (clockVal <= 0){

不能轻易变为真,因为在
之间经过了毫秒,Swing事件线程上运行了一个while循环并将其冻结。把它从事件线程中删除。更好的是,更改代码,这样您就不需要while循环了。相反,可以使用Swing Timer的actionPerformed作为事件来更改视图的状态,或者使用observer模式来通知视图计时器计数器的更改。

您已经在Swing事件线程上运行了一个while循环并将其冻结。把它从事件线程中删除。更好的是,更改代码,这样您就不需要while循环了。相反,可以使用Swing Timer的actionPerformed作为事件来更改视图的状态,或者使用observer模式来通知视图计时器计数器的更改。

请澄清您的问题是什么?哦!很抱歉,问题是我没有收到任何错误,但是计时器没有添加到框架中。您的代码中有错误,阻止我们复制、粘贴和运行它。如果你能将代码合并成一个有效的代码,并为我们省去调试一堆错误的麻烦,那就更好了。将修复代码,在编辑器中无法分辨。@HovercraftFullOfEels现在应该没有bug了。你能澄清一下你的问题吗?哦!很抱歉,问题是我没有收到任何错误,但是计时器没有添加到框架中。您的代码中有错误,阻止我们复制、粘贴和运行它。如果您能将代码合并成一个有效的代码,并为我们省去调试一堆错误的麻烦,那就更好了。将修复代码,在编辑器中无法分辨。@HovercraftFullOfEels现在应该没有bug了,谢谢!有意义的是,while循环将所有内容都保留了下来,更不用说错误的位置了。谢谢!有道理,while循环将所有内容都保留了下来,更不用说错误的位置了。