Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/image-processing/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何在计时器倒计时到0后禁用JButton_Java_Timer - Fatal编程技术网

Java 如何在计时器倒计时到0后禁用JButton

Java 如何在计时器倒计时到0后禁用JButton,java,timer,Java,Timer,我正在做一个点击游戏程序。用户必须输入时间限制(以秒为单位)。如果该值为正值,则将启用单击按钮。当时间用完时,单击按钮将被禁用。这是我当前代码的一部分: 我似乎无法使用setInterval()返回的值并在计时器点击0后禁用我的单击按钮 public void actionPerformed(ActionEvent a) { if (a.getSource()==startButton){ try{ String sec

我正在做一个点击游戏程序。用户必须输入时间限制(以秒为单位)。如果该值为正值,则将启用单击按钮。当时间用完时,单击按钮将被禁用。这是我当前代码的一部分: 我似乎无法使用setInterval()返回的值并在计时器点击0后禁用我的单击按钮

public void actionPerformed(ActionEvent a) {
     if (a.getSource()==startButton){
            try{
                    String sec = timeField.getText();
                    int delay = 1000;
                    int period = 1000;
                    timer = new Timer();
                    interval = Integer.parseInt(sec);

                    if(interval > 0){
                        timeLeft.setText("Time left: " + sec);
                          timeLeft.setText("Start!");
                          clickButton.setEnabled(false);



                        if(setInterval() > 0){
                            timer.scheduleAtFixedRate(new TimerTask() {
                                public void run() {
                                      timeLeft.setText("Time left: " + String.valueOf(setInterval()));
                                    }
                                }, delay, period);

                            clickButton.setEnabled(true);
                        }else{
                            System.out.print(String.valueOf(setInterval()));
                            clickButton.setEnabled(false);  
                            }

                    } else {
                        JOptionPane.showMessageDialog(null, "Error! Please enter postivie Interger! ", "Error", JOptionPane.ERROR_MESSAGE);
                    }
            }
            catch(NumberFormatException e){
                JOptionPane.showMessageDialog(null, "Error! Please enter Integer! ", "Error", JOptionPane.ERROR_MESSAGE);
            }
    }
     else if(a.getSource()==clickButton)
     {
         clickCOunter++;
         clickLabel.setText("Clicks: " + clickCOunter);
     }
}

 private static final int setInterval() {
        if (interval == 1)
            timer.cancel();
        return --interval;
    }

在您的情况下,我将放置一个System.out.println(setInterval());就在
if(setInterval()>0){
检查的正上方。这样,您可以更好地观察值的变化

作为一般提示,您不必使用
String.valueOf(…)
打印数字。删除它也会使代码更易于阅读。 另外,确保将花括号({})对齐。这也会使代码更易于阅读和调试

最后,如果我可以提出一个建议的话,我想对你的问题采取一个稍微简单一点的方法。如果你使用Swing定时器,你可以在
actionPerformed()
方法之外创建它,并将它设置为定期调用
actionPerformed()
。这样
actionPerformed()中的逻辑
可以简化很多。
请查看Swing timer的文档。

这一行是否按预期重复执行?timeLeft.setText(“Time left:”+String.valueOf(setInterval());是,它作为计时器倒计时