你能帮我解释这个编码吗?它是一个运行某项任务的计时器,但它不会停止!我使用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) {
}
答案 0 :(得分:3)
你在事件调度线程(或EDT)上进行了很多长时间运行的调用,这将使它无助。相反,请在后台线程中执行数据库代码和任何其他长时间运行的代码,例如SwingWorker提供的代码。
顺便说一句,这个空的挡块让我害怕:
} catch (Exception e) {
}
注意,如果你想让Timer只运行一次,那么你可以通过actionPerformed方法将它杀死:
((Timer) evt.getSource()).stop();
您甚至可以将其作为actionPerformed方法的第一行。