Java swing计时器问题

时间:2012-04-07 01:52:53

标签: java swing timer

我遇到了(我认为)摇摆计时器的问题。我写了一些工作正常的代码,然后把它全部移到我的新计算机上,它很快就无法工作。我用这个方法编写了一个GUI类(基于JFrame):

public void Splash(){
    mainPanel.add(Empous.splash, BorderLayout.CENTER);
    while(Empous.splash.GetCount() < 3){
              //System.out.println(Empous.splash.GetCount());
    }
}

从另一个类调用该方法。那个Empous.splash家伙是一个JPanel类,只运行启动动画。它使用摆动计时器。在splash类中,侦听器通过以下代码调整帧的内容:

private class TimerListener implements ActionListener{
    public void actionPerformed(ActionEvent evt) {
        counter+=1;
        if (counter==1){
            title2.setText("In Association With");
            title1.setText("El Pollo Diablo Productions");
        }
        if (counter==2){
            remove(title2);
            remove(title1);
            repaint();
        }
        if (counter==3){
            timer.stop();
        }
    }
}

现在,如果第一个块中的print语句被注释掉,我的程序将在计时器停止后冻结。如果我取消注释,程序将打印出计数器的值,然后在下雨时继续。我想取出print语句并让while循环运行而不做任何事情,但我不能按照当前的行为方式这样做。任何帮助表示赞赏。

1 个答案:

答案 0 :(得分:4)

你希望用这个while循环打破Swing的线程规则:

while(Empous.splash.GetCount() < 3){
   //System.out.println(Empous.splash.GetCount());
}

为什么你甚至有这个,因为你已经有一个Swing Timer来处理这类事情而不会释放事件线程?

有关更具体的建议,是的,发布sscce