TimerTask只运行一次

Bar*_*rak 1 java swing javax.swing.timer timertask

我有一个名为的任务timer:

timer.schedule(new task1(), 1000*minutes);
Run Code Online (Sandbox Code Playgroud)

任务:

class task1 extends TimerTask {
        @Override

    public void run()
    {
            try {
                task();
            } catch (SAXException ex) {
                Logger.getLogger(task1.class.getName()).log(Level.SEVERE, null, ex);
            } catch (ParserConfigurationException ex) {
                Logger.getLogger(task1.class.getName()).log(Level.SEVERE, null, ex);
            } catch (IOException ex) {
                Logger.getLogger(task1.class.getName()).log(Level.SEVERE, null, ex);
            } catch (URISyntaxException ex) {
                Logger.getLogger(task1.class.getName()).log(Level.SEVERE, null, ex);
            } catch (InterruptedException ex) {
                Logger.getLogger(task1.class.getName()).log(Level.SEVERE, null, ex);
            }
    }
    public void task() throws SAXException, ParserConfigurationException, IOException, URISyntaxException, InterruptedException {
        Pinner_xml t = new Pinner_xml();
        t.xml(frame.t1.getText());

        frame.output.append("task 1 \n");
        System.out.println("task 1 is running");
    }
}
Run Code Online (Sandbox Code Playgroud)

frame是我的框架名称,output是一个文本框.为什么我在Netbeans的输出对话框中运行任务是"任务1正在运行",任务只运行一次.

nIc*_*cOw 9

使用javax.swing.Timer进行Swing,因为一切都是在EDT上完成的(默认情况下),使用Timer Class,这是先决条件,请参阅如何使用Timer.另一个用于在Timer上更新JButton以及另一个用于使用Swing TimerScrolling Text的相关示例


Evg*_*eev 5

用于重复执行任务Timer.schedule(TimerTask task, long delay, long period)或使用scheduleAtFixedRate方法