试图组成Java Swing应用程序的一部分,该应用程序应该执行以下操作:
我在代码的某些点上放置了打印件,似乎代码从textField输入了几分钟,然后将其传递给Countdown类中的Constructor,但随后仅执行了done(),而无需先运行doInBackground()。
如果有人暗示为什么会这样,我将非常感激,我已经尝试了所有想法,但没有任何效果。
这是第一部分,来自Button的代码:
private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
userCountTime = Integer.parseInt(jTextField2.getText());
userCountTime = userCountTime * 60;
System.out.println("Preuzeto iz dugmeta " + userCountTime);
if (jRadioButton2.isSelected()){
Countdown countDown= new Countdown (userCountTime);
}
这是Countdown类:
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.SwingWorker;
import javax.swing.Timer;
/**
*
* @author Glupko
*/
public class Countdown extends SwingWorker{
public Timer timer;
static int userCountTime;
static ActionListener timerListener;
//Constructor
public Countdown (int userCountTime){
Countdown.userCountTime = userCountTime;
System.out.println("In constructor: " + Countdown.userCountTime);
this.execute();
}
@Override
protected Object doInBackground() throws Exception {
//defining listener
timerListener = new ActionListener() {
@Override
public void actionPerformed(ActionEvent actionEvent) {
System.out.println("In action performed value is: " + userCountTime);
//for each tick we subtract 1, every second
while (userCountTime >= 0){
userCountTime--;
System.out.println("In actionPerformed there is number of seconds: " + userCountTime);}
timer = new Timer(1000, timerListener);
//start timer
timer.start();
}};}
@Override
protected void done(){
System.out.println("From done: timer elapsed");
ChangingWindow window = new ChangingWindow();
}
}
提前谢谢了!
doInBackground
方法中启动Swing计时器。这不能实现所有目的,因为应该在Swing事件线程或EDT上运行Swing计时器。timer.start()
是非阻塞的。取而代之的是摆脱SwingWorker,而直接使用Timer。
有关代码的更完整帮助,请考虑创建并发布您的最小代码示例程序,以供我们检查,测试和修复。
例如,。
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Arrays;
import javax.swing.*;
public class CountDownTest extends JPanel {
private static final Integer[] SECONDS = {1, 2, 3, 4, 5, 6, 7, 8, 9};
private JSpinner secondsSpinner = new JSpinner(new SpinnerListModel(Arrays.asList(SECONDS)));
public CountDownTest() {
add(secondsSpinner);
add(new JButton(new StartCountDownAction(this, "Start Count Down")));
}
public int getSecondsSpinnerValue() {
return (Integer) secondsSpinner.getValue();
}
private static void createAndShowGui() {
CountDownTest mainPanel = new CountDownTest();
JFrame frame = new JFrame("CountDownTest");
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.getContentPane().add(mainPanel);
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGui();
}
});
}
}
class StartCountDownAction extends AbstractAction {
private CountDownTest countDownTest;
private Timer timer;
public StartCountDownAction(CountDownTest countDownTest, String name) {
super(name);
this.countDownTest = countDownTest;
int mnemonic = (int) name.charAt(0);
putValue(MNEMONIC_KEY, mnemonic);
}
@Override
public void actionPerformed(ActionEvent e) {
if (timer != null && timer.isRunning()) {
// don't start a new Timer when one is running
return;
}
int seconds = countDownTest.getSecondsSpinnerValue();
timer = new Timer(seconds * 1000, new TimerListener(countDownTest));
timer.setRepeats(false);
timer.start();
}
}
class TimerListener implements ActionListener {
private CountDownTest countDownTest;
public TimerListener(CountDownTest countDownTest) {
this.countDownTest = countDownTest;
}
@Override
public void actionPerformed(ActionEvent e) {
JOptionPane.showMessageDialog(countDownTest, "Timer Done!");
}
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句