java.swing.Timer没有停止



你能帮我编码一下吗?它是一个计时器,运行一个特定的任务,但它不会停止!我正在使用java.swing.Timer和WebLookAndFeel(你会注意到NotificationManager:它属于WebLaF)。我试着调用自身内部的<timer>.stop();,但它说它没有实例化。我还尝试过添加<timer>.setRepeats(false),这导致了NPE。<timer>为每个唯一定时器。

没有计时器的编码工作得很好,但最近添加到应用程序中需要使用计时器。

我在编程中使用的大部分东西都是自学的,所以我不太擅长调试计时器。请耐心听我说。

我的代码:

Timer updateEB;
try {
    updateEB = new Timer(2500, new ActionListener() 
    @Override
    public void actionPerformed(ActionEvent evt) {
        System.out.println("Update process started");
        try {
            // <editor-fold defaultstate="collapsed" desc="Get first message via SQL query">
            try {
                // Initiate SQL connection and execute query
                String sql = "Select * from APP.EBULLETINS ORDER BY msgid DESC FETCH FIRST 2 ROWS ONLY";
                Class.forName("org.apache.derby.jdbc.EmbeddedDriver");
                Class.forName("org.apache.derby.jdbc.ClientDriver");
                Connection con = (Connection) DriverManager.getConnection("jdbc:derby:C:\Program Files\AmalgaIMS\AIMSDB", "xxxxxxx", "xxxxxxxx");
                Statement stmt = con.createStatement();
                ResultSet rs = stmt.executeQuery(sql);
                //  Get message details
                if (rs.next()) {
                    eBul1_Title = rs.getString("Title");
                    eBul1_Msg = rs.getString("Content");
                    eBul1_Type = rs.getString("MSGTYPE");
                }
                // Set message fields
                eBul1T.setText(eBul1_Title);
                eBul1M.setText(eBul1_Msg);
                // Debugging purposes
                System.out.println("Setting Icons...");
                System.out.print("1. ");
                // <editor-fold defaultstate="collapsed" desc="Set Message 1 Icon">
                if (eBul1_Type.equals("INFORMATION")) {
                    TypeImage1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/main/MainMenu/Information.png")));
                    System.out.println("Info");
                } else if (eBul1_Type.equals("ANNOUNCEMENT")) {
                    TypeImage1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/main/MainMenu/announcement.png")));
                    System.out.println("Announce");
                } else if (eBul1_Type.equals("WARNING")) {
                    TypeImage1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/main/MainMenu/Warning.png")));
                    System.out.println("Warning");
                } else {
                    TypeImage1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/main/MainMenu/misc 16x16.png")));
                    System.out.println("Other");
                }
                // </editor-fold>
                } catch (Exception eB1Exc) {
                    JOptionPane.showMessageDialog(null, eB1Exc);
                }
                // </editor-fold>
                // <editor-fold defaultstate="collapsed" desc="Get second message via SQL query">
                try {
                    // Initiate SQL connection and execute query
                    String sql = "Select * from app.EBULLETINS ORDER BY msgid DESC FETCH FIRST 2 ROWS ONLY";
                    Class.forName("org.apache.derby.jdbc.EmbeddedDriver");
                    Class.forName("org.apache.derby.jdbc.ClientDriver");
                    Connection con = (Connection) DriverManager.getConnection("jdbc:derby:C:\Program Files\AmalgaIMS\AIMSDB", "xxxxxxxx", "xxxxxxxxx");
                    Statement stmt = con.createStatement();
                    ResultSet rs = stmt.executeQuery(sql);
                    //  Get message details
                    while (rs.next()) {
                        eBul2_Title = rs.getString("Title");
                        eBul2_Msg = rs.getString("Content");
                        eBul2_Type = rs.getString("MSGTYPE");
                    }
                    // Debugging purposes
                    System.out.print("2. ");
                    // Set message fields
                    eBul2T.setText(eBul2_Title);
                    eBul2M.setText(eBul2_Msg);
                    // <editor-fold defaultstate="collapsed" desc="Set Message 2 Icon">
                    if (eBul2_Type.equals("INFORMATION")) {
                        TypeImage2.setIcon(new javax.swing.ImageIcon(getClass().getResource("/main/MainMenu/Information.png")));
                        System.out.println("Info");
                    } else if (eBul2_Type.equals("ANNOUNCEMENT")) {
                        TypeImage2.setIcon(new javax.swing.ImageIcon(getClass().getResource("/main/MainMenu/announcement.png")));
                        System.out.println("Announce");
                    } else if (eBul2_Type.equals("WARNING")) {
                        TypeImage2.setIcon(new javax.swing.ImageIcon(getClass().getResource("/main/MainMenu/Warning.png")));
                        System.out.println("Warning");
                    } else {
                        TypeImage2.setIcon(new javax.swing.ImageIcon(getClass().getResource("/main/MainMenu/misc 16x16.png")));
                        System.out.println("Other");
                    }
                    // </editor-fold>
                } catch (Exception eB2Exc) {
                    eB2Exc.printStackTrace();
                }
                // </editor-fold>
                //<editor-fold defaultstate="collapsed" desc="Update Successful">
                //<editor-fold defaultstate="collapsed" desc="Set eBulletin Notification Sound">
                try {
                    // Open an audio input stream.
                    URL url = this.getClass().getResource("/Resources/Sounds/Notifications/NotifB.wav");
                    AudioInputStream audioIn = AudioSystem.getAudioInputStream(url);
                    // Get a sound clip resource.
                    Clip clip = AudioSystem.getClip();
                    // Open audio clip from the audio input stream.
                    clip.open(audioIn);
                    clip.start();
                } catch (UnsupportedAudioFileException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                } catch (LineUnavailableException e) {
                    e.printStackTrace();
                }
                //</editor-fold>
                NotificationManager.showNotification("eBulletins updated!", NotificationIcon.mail.getIcon());
                Timer hideNotifs;
                try {
                    hideNotifs = new Timer(3000, new ActionListener() {
                    @Override
                    public void actionPerformed(ActionEvent evt) {
                        NotificationManager.hideAllNotifications();
                    }
                });
                hideNotifs.start();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            } catch (Exception exc) {
                exc.printStackTrace();
            }
            //</editor-fold>
            //<editor-fold defaultstate="collapsed" desc="Set Refresh Icon">
            try {
                lbleBulUpdate.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Resources/refresh 16x16.png")));
            } catch (Exception e) {
                System.out.println(e);
            }
            //</editor-fold>
            lblEBulStatus.setForeground(new java.awt.Color(51, 255, 0));
            lblEBulStatus.setText("ACTIVE");
        }
    });
    updateEB.start();
} catch (Exception e) {
}

您在事件调度线程(或EDT)上进行了大量长时间运行的调用,这将使其无能为力。相反,在后台线程中处理数据库代码和任何其他长时间运行的代码,比如SwingWorker中可用的代码。

说句题外话,这个空catch块让我很害怕:

} catch (Exception e) {
}

注意,如果你想让Timer只运行一次,那么你可以在actionPerformed方法中通过:

((Timer) evt.getSource()).stop();

你甚至可以把它作为actionPerformed方法的第一行

相关内容

  • 没有找到相关文章

最新更新