1

我想访问t.isStopped()被覆盖的run()方法,但出现以下错误。

不能在不同方法中定义的内部类中引用非最终变量 t

如果我将 t 设置为最终变量

局部变量 t 可能尚未初始化

<!-- language: java -->

jHibernateCheckBox = new JCheckBox("Sleep / Hibernate after ");
jHibernateTextField = new JTextField("240");

jHibernateCheckBox.addActionListener(new ActionListener() {

  @Override
  public void actionPerformed(ActionEvent e) {


     StoppableThread t = new StoppableThread() {

        @Override
        public void run() {
           t.setIsRunning(true);
           long timestamp = new Date().getTime();
           int minutes = Integer.parseInt(jHibernateTextField.getText());
           long secounds = minutes * 60;
           long millisecounds = secounds * 1000;
           long elapsedTime = new Date().getTime();

           do {
              try {
                 Thread.sleep(1000);
                 millisecounds -= new Date().getTime() - elapsedTime;
                 secounds = millisecounds / 1000;
                 if(secounds < 60) {
                    jHibernateTextField.setText("" + secounds);
                    jRemainingLabel.setText(" secounds remaining");
                 }
                 else {
                    jHibernateTextField.setText("" + secounds / 60);
                    jRemainingLabel.setText(" minutes remaining");
                 }
              } catch (InterruptedException e) {
                 // TODO Auto-generated catch block
                 e.printStackTrace();
              }
              elapsedTime = new Date().getTime();
           }while(!t.isStopped() && timestamp + minutes * 60 * 1000 > new Date().getTime());

           jHibernateTextField.setText("" + minutes);

           if(t.isStopped()) return;
           else {
              console.out("Computer wird schlafen gelegt =)");
              //Hibernate (siehe LAPTOP)
           }

        }
     };

     Thread thread = new Thread(t);


     if(jHibernateCheckBox.isSelected()) {
        jHibernateTextField.setEnabled(false);
        thread.start();
     }
     else {
        jHibernateTextField.setEnabled(true);
        t.setIsStopped(true);
     }
  }

});

是否可以访问内部类中的变量 t ,或者如果再次单击复选框,是否有人知道如何停止线程?

我只是希望当我再次单击 jHibernateCheckBox 时计时器停止工作......也许还有另一种方法?

4

4 回答 4

2

与您的问题无关,但在您发布的代码片段中,您违反了 Swing 线程规则。所有 Swing 组件只能从 Event Dispatch Thread 访问/修改。StoppableThread不允许修改您的 Swing 组件,并且可能会导致运行时出现奇怪的行为。

快速浏览一下您的代码后,您似乎可以使用 aSwingWorker并触发进度事件。或多或少与JProgressBar教程中说明的机制相同(他们使用 a 的部分SwingWorker

有关更多信息,请参阅Swing 并发教程

于 2012-11-19T17:36:02.443 回答
1

您可以尝试在实现中使用“this”关键字吗?

于 2012-11-19T17:05:18.847 回答
1
                  StoppableThread t = new StoppableThread() {

                    @Override
                    public void run() {
                        t.setIsRunning(true);
                        long timestamp = new Date().getTime();
                        int minutes = Integer.parseInt(jHibernateTextField.getText());
                        long secounds = minutes * 60;
                        long millisecounds = secounds * 1000;
                        long elapsedTime = new Date().getTime();

                        do {
                            try {
                                Thread.sleep(1000);
                                millisecounds -= new Date().getTime() - elapsedTime;
                                secounds = millisecounds / 1000;
                                if(secounds < 60) {
                                    jHibernateTextField.setText("" + secounds);
                                    jRemainingLabel.setText(" secounds remaining");
                                }
                                else {
                                    jHibernateTextField.setText("" + secounds / 60);
                                    jRemainingLabel.setText(" minutes remaining");
                                }
                            } catch (InterruptedException e) {
                                // TODO Auto-generated catch block
                                e.printStackTrace();
                            }
                            elapsedTime = new Date().getTime();
                        }while(!t.isStopped() && timestamp + minutes * 60 * 1000 > new Date().getTime());

                        jHibernateTextField.setText("" + minutes);

                        if(t.isStopped()) return;
                        else {
                            console.out("Computer wird schlafen gelegt =)");
                            //Hibernate (siehe LAPTOP)
                        }

                    }
                };

在 run 方法中将 t 替换为this

于 2012-11-19T17:06:17.233 回答
-1

我尝试了一些新的东西,现在它可以工作了。我不知道这是否是一个错误,但
只是看看。

private StoppableThread t;

public void initComponents() {
...
...
t = new StoppableThread() {
        @Override
        public void run() {
            setIsRunning(true);
            long timestamp = new Date().getTime();
            int minutes = Integer.parseInt(jHibernateTextField.getText());
            long secounds = minutes * 60;
            long millisecounds = secounds * 1000;
            long elapsedTime = new Date().getTime();

            do {
                try {
                    Thread.sleep(1000);
                    millisecounds -= new Date().getTime() - elapsedTime;
                    secounds = millisecounds / 1000;
                    if(secounds < 60) {
                        jHibernateTextField.setText("" + secounds);
                        jRemainingLabel.setText(" secounds remaining");
                    }
                    else {
                        jHibernateTextField.setText("" + secounds / 60);
                        jRemainingLabel.setText(" minutes remaining");
                    }
                } catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                elapsedTime = new Date().getTime();
            }while(!isStopped() && timestamp + minutes * 60 * 1000 > new Date().getTime());

            jHibernateTextField.setText("" + minutes);

            if(isStopped()) return;
            else {
                console.out("Computer wird schlafen gelegt =)");
                //Hibernate (siehe LAPTOP)
            }

        }
    };

    jHibernateCheckBox.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent e) {


            Thread thread = new Thread(AutoBarbarPanel.this.t);


            if(jHibernateCheckBox.isSelected()) {
                jHibernateTextField.setEnabled(false);
                thread.start();
            }
            else {
                jHibernateTextField.setEnabled(true);
                t.setIsStopped(true);
            }
        }

    });
}

感谢所有答案

于 2012-11-20T07:42:25.180 回答