作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在制作一个程序,它应该能够通过单击按钮来停止整个过程。
我用过sw.cancel(true);
然而,要做到这一点,SwingWorker
方法protected void done()
仍在运营。
我怎样才能按一下按钮取消整个事情?不仅仅是doInBackground()
方法?
下面是完整的相关代码,供感兴趣的人引用:
sw = new SwingWorker() {
protected Object doInBackground() throws Exception {
//Pravljenje timera
t = new Timer(0, new ActionListener() {
public void actionPerformed(ActionEvent e) {
}
});
//Provera stanja checkboxova
if(jCheckBox2.isSelected()) {
try {
int delay =(int) jSpinner2.getValue();
jCheckBox1.setSelected(false);
Thread.sleep(delay*60000);
} catch (InterruptedException ex) {
Logger.getLogger(App_Gui.class.getName()).log(Level.SEVERE, null, ex);
}
}
if(jCheckBox1.isSelected()) {
jCheckBox2.setSelected(false);
Date delay2 = (Date) jSpinner1.getValue();
userCal = Calendar.getInstance();
System.out.println("delay2: " + delay2);
userCal.setTime(delay2);
System.out.println("userCal:" + userCal);
Calendar sysCal = Calendar.getInstance();
System.out.println("sysCal: " + sysCal);
timerAtDate =
(((int) userCal.get(Calendar.HOUR_OF_DAY) - (int) sysCal.get(Calendar.HOUR_OF_DAY)) * 60 * 60 * 1000 +
((int) userCal.get(Calendar.MINUTE) - (int) sysCal.get(Calendar.MINUTE)) * 60 * 1000 +
((int) userCal.get(Calendar.SECOND) - (int) sysCal.get(Calendar.SECOND)) * 1000);
Thread.sleep(timerAtDate);
}
return null;
}
@Override
protected void done() {
System.out.println("Done!");
t.start();
t.setRepeats(false);
JDialog dialog = new JDialog();
dialog.setLocation(700, 300);
dialog.setSize(600, 400);
dialog.setVisible(true);
try {
Thread.sleep(jSlider1.getValue());
} catch (InterruptedException ex) {
Logger.getLogger(App_Gui.class.getName()).log(Level.SEVERE, null, ex);
}
System.out.println("Zapravo gotov");
dialog.getContentPane().setBackground(jLabel2.getBackground());
dialog.setModal(true);
Assignment_Tajmer_Aplikacija.f.setVisible(false);
}
};
sw.execute();
private void jSlider1StateChanged(javax.swing.event.ChangeEvent evt) {
// TODO add your handling cosadde here:
jSlider1.addChangeListener(new ChangeListener() {
public void stateChanged(ChangeEvent e) {
JSlider source = (JSlider) e.getSource();
//System.out.println(source.getValue());
}
});
}
private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
JColorChooser jcc = new JColorChooser();
Color c = jcc.showDialog(null, "Choose background color", Color.yellow);
jLabel2.setForeground(c);
jLabel2.setBackground(c);
jLabel2.setText("Color: " + c.getRGB() + "(RGB)");
}
private void jCheckBox2ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
jCheckBox1.setSelected(false);
}
private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
sw.cancel(true);
}
private void jCheckBox1ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
jCheckBox2.setSelected(false);
}
最佳答案
在 done()
开头使用 isCancelled()
方法来查看是否应该执行该操作。
关于java - 如何在 SwingWorker 线程中取消 did() ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37455569/
我是一名优秀的程序员,十分优秀!