我已经实现了一个计时器来调用我的 alert() 方法。从数据库中检索计时器的持续时间。当我将持续时间设置为 1 分钟时,计时器每隔一分钟调用一次 alert()。当我再次将持续时间设置为 5 分钟时,1 分钟计时器不会停止。所以现在我有 2 个正在运行的计时器。如何删除以前的计时器?谢谢。
private void getDuration()
{
durationTimer = new javax.swing.Timer(durationDB, new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
alert();
}
});
durationTimer.stop();
try
{
// Connection to the database
Class.forName("com.mysql.jdbc.Driver");
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/smas","root","root");
Statement stmt = con.createStatement();
ResultSet rs = stmt.executeQuery("SELECT * FROM alertduration");
while (rs.next())
{
durationDB = rs.getInt("duration");
}
con.close();
}
catch(Exception ea)
{
JOptionPane.showMessageDialog(watchlist, "Please ensure Internet Connectivity ", "Error!", JOptionPane.ERROR_MESSAGE);
}
durationTimer = new javax.swing.Timer(durationDB, new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
alert();
}
});
durationTimer.start();