无法弄清楚为什么Timer无法按预期工作

时间:2019-03-03 17:43:15

标签: java swing methods timer jtable

我正在尝试开发一种程序,一旦满足条件,该程序就可以从JTable中删除条目。为此,我选择使用计时器。我计划的方式是,当删除或添加条目时,我有两个几乎相同的Timer方法在彼此之间切换。为了阻止Timer()自身重叠,我创建了一个'stop'布尔变量,该变量可以在调用Timer()之前使其停止,以便一次仅存在一个实例。但是,这是行不通的,正如系统在错误检查中每秒打印2 x而不是1所证明的那样。请在下面的伪代码中找到:

public class Main extends javax.swing.JFrame{

boolean stop;

public void Timer(){

stop = false;

Timer timer = new Timer();

long interval = (1000) ;

timer.schedule(new TimerTask(){

    public void run() {

        if(<condition is met>){
        <remove entry>;                                         

            if(<no more entries>){
            timer.cancel();
            }

            else{
            Timer2(); // identical to Timer(), but with this line swapped to < Timer(); >
            timer.cancel();
            }
        }

        else if(stop == true){
        timer.cancel();
        }

        else{
        System.out.println("x") // used to check if Timer is working          
        }

   }
}, 0, interval); 
}    

private void AddButtonActionPerformed(java.awt.event.ActionEvent evt) {
<insert an entry into the table>
stop = true;
Timer();
}
}

任何帮助将不胜感激。

0 个答案:

没有答案
相关问题