循环中的javax.swing.JFrame setText()?+故意拖延?

sno*_*yak 3 java swing timer jframe event-dispatch-thread

我是Java的新手(仅使用它大约一周),我正在商店模拟器上工作.基本上我只是希望现在能够完成基本的事情,比如有一段时间直到关闭以及屏幕上显示打开变量的时间.

我在我的Simulation扩展javax.swing.JFrame类中做了以下函数:

public void incOpenTime() {
    timeOpen++;
    int hours = timeOpen / 3600;
    int minutes = (timeOpen % 3600) / 60;
    int seconds = (timeOpen % 3600) % 60;
    this.openTime.setText((hours < 10 ? "0" : "") + hours
            + ":" + (minutes < 10 ? "0" : "") + minutes
            + ":" + (seconds < 10 ? "0" : "") + seconds);
    decTimeLeft();
    }

public void decTimeLeft() {
    int remainingTime = 28800 - timeOpen;
    int hours = remainingTime / 3600;
    int minutes = (remainingTime % 3600) / 60;
    int seconds = (remainingTime % 3600) % 60;
    this.timeLeft.setText((hours < 10 ? "0" : "") + hours
            + ":" + (minutes < 10 ? "0" : "") + minutes
            + ":" + (seconds < 10 ? "0" : "") + seconds);

    }
Run Code Online (Sandbox Code Playgroud)

使用setText()方法的openTime和timeLeft变量是GUI本身的一部分.

在main中,我像这样调用incOpenTime:

while(this.timeOpen < 28800) 
{
    incOpenTime();

}
Run Code Online (Sandbox Code Playgroud)

首先,当我运行它时,它基本上只是通过循环并仅输出最后一次到屏幕.如何才能让时间不断变化?

其次,我想稍微延迟一下......可能大约每秒1毫秒,因此模拟运行速度较慢,而其他数据输出到屏幕(后面)更具可读性.我试过Thread.sleep(1); 在循环中但它不会显示数字,因为它们被更改.

请帮忙.

谢谢

编辑:

这就是我为了让它发挥作用所做的一切.主要:

timer = new Timer();
for(int i=1; i<= 28800; i++)
{
        timer.schedule(new task(), 1*i);

}
Run Code Online (Sandbox Code Playgroud)

做了一个新课:

class task extends TimerTask {

    @Override
    public void run() {
        incOpenTime();
        }
}
Run Code Online (Sandbox Code Playgroud)

tra*_*god 5

javax.swing.Timer可能是一个不错的选择,作为" Timers在事件调度线程上执行的动作事件处理程序".这里有一个例子在这里.