gpt4 book ai didi

java等待swing.timer任务完成后再执行下一个任务

转载 作者:行者123 更新时间:2023-12-01 09:01:05 25 4
gpt4 key购买 nike

所以标题确实说明了一切。我需要线程等待计时器完成,然后再执行方法中的下一个命令。

代码:

import javax.swing.Timer;

public class doesntMatter
{
Timer timer;
ActionListener timerTask;

public doesntMatter
{
timerTask = new ActionListener()
{
@Override
public void actionPerformed(ActionEvent e)
{
//taskSomethingIdk
}
}
}

private void whatever
{
timer = new Timer(1000, timerTask);
timer.setInitialDelay(0);
timer.start();

// here i need to wait for the timer to finish

if(timerhasfrigginfinished)
continueOrSomethingIdk
}

}

最佳答案

假设您的计时器重复,在计时器的 ActionListener 中检查它是否正在运行最后一次重复,如果是,则调用 continueOrSomethingIdk() 方法.

否则,您将必须设置自己的通知机制(回调),以便计时器通知任何监听器它已完成运行。

例如:

import java.awt.Color;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.*;

@SuppressWarnings("serial")
public class WhenTimerDone extends JPanel {
private static final Color[] COLORS = {
Color.RED, Color.ORANGE,
Color.YELLOW, Color.GREEN,
Color.BLUE, Color.CYAN };
private static final String START = "Start";
private static final String DONE = "Done";
private static final int PREF_W = 400;
private static final int PREF_H = PREF_W;
public static final int TIMER_DELAY = 1000;
private JLabel statusLabel = new JLabel(START);
private StartAction startAction = new StartAction("Start!");

public WhenTimerDone() {
add(statusLabel);
add(new JButton(startAction));
}

@Override
public Dimension getPreferredSize() {
if (isPreferredSizeSet()) {
return super.getPreferredSize();
}
return new Dimension(PREF_W, PREF_H);
}

// this is the method called by the Timer's ActionListener when it is done
public void done() {
// reset all to baseline state
statusLabel.setText(DONE);
startAction.setEnabled(true);
setBackground(null);
}

// ActionListener for the start button
private class StartAction extends AbstractAction {

public StartAction(String name) {
super(name);
int mnemonic = (int) name.charAt(0);
putValue(MNEMONIC_KEY, mnemonic);
}

@Override
public void actionPerformed(ActionEvent e) {
// disables itself
setEnabled(false);
statusLabel.setText(START); // updates the status label

// create and start a timer
Timer timer = new Timer(TIMER_DELAY, new TimerListener());
timer.setInitialDelay(0);
timer.start();
}
}

// action listener for the timer
private class TimerListener implements ActionListener {
private int colorsIndex = 0;

@Override
public void actionPerformed(ActionEvent e) {
// simply loops through a colors array, changing background color
if (colorsIndex < COLORS.length) {
setBackground(COLORS[colorsIndex]);
colorsIndex++;
} else {
// when all colors shown -- stop the timer
((Timer) e.getSource()).stop();

// and call the done method -- ******* here's the key!
done(); // called when Timer is done!
}
}
}

private static void createAndShowGui() {
WhenTimerDone mainPanel = new WhenTimerDone();

JFrame frame = new JFrame("WhenTimerDone");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(mainPanel);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}

public static void main(String[] args) {
SwingUtilities.invokeLater(() -> createAndShowGui());
}
}

关于java等待swing.timer任务完成后再执行下一个任务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41657010/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com