我正在使用Netbeans 8.0,我很难停止在GUI中制作的计时器。这是我的代码。
public class Task1 implements Runnable{
public void run()
{
long start = System.currentTimeMillis();
while (!Thread.interrupted())
{
long time = System.currentTimeMillis() - start;
final long seconds = time / 1000;
final long min = seconds / 60;
final long remainingSec = seconds % 60;
final long hr = min / 60;
SwingUtilities.invokeLater(new Runnable() {
public void run()
{
jLabel3.setText("Time Passed: " + hr + ":" + min + ":" + remainingSec);
}
});
try { Thread.sleep(100); } catch(Exception e) { }
}
}
}
Thread t1 = new Thread(new Task1());
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
t1.start();
}
private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
t1.interrupt(); // this one doesnt stop my timer.
}
我制作了两个按钮,jButton1启动计时器,jButton2停止计时器。请指教。