Java Swing 运行后重启定时器
Posted
技术标签:
【中文标题】Java Swing 运行后重启定时器【英文标题】:Java Swing Restart Timer After Operation 【发布时间】:2014-05-29 05:14:47 【问题描述】:我需要我的计时器重新启动,或者至少在执行某行代码后添加另一个延迟。
private static class ButtonHandler implements ActionListener
public void actionPerformed (ActionEvent e)
final JButton button = (JButton)e.getSource();
Timer timer = new Timer(1000, new ActionListener()
public void actionPerformed(ActionEvent e)
String tc = random();
them.setText("They chose: " + tc + "!");
if (button == rock)
whoWins("rock", tc);
else if (button == paper)
whoWins("paper", tc);
else if (button == scissors)
whoWins("scissors", tc);
yourWins.setText("Your wins: " + yw);
theirWins.setText("Their wins: " + tw);
);
timer.setRepeats(false);
timer.start();
我想在之后立即实现计时器的第二次延迟
them.setText("they chose: " + tc + "!");
但我不确定如何执行此操作,我应该重新启动计时器吗?如果是,我将在哪里编写那行代码?提前致谢。
【问题讨论】:
它应该在消息显示后立即延迟(他们选择:...),无需用户交互。它本质上应该是两个延迟,中间有一个暂停,无论在屏幕上显示第一条消息需要多长时间。 不再需要了,非常感谢大家的帮助,我终于可以在睡眠之外开发我的简单程序了。 【参考方案1】:我本来打算把这个发到你的previous question,但看来你已经取得了一些进展,干得好。
好的,因此,此示例为您提供了三个按钮供您选择。当您单击一个时,它会记录您选择的内容,禁用按钮并启动等待 1 秒的Timer
。
Timer
的 ActionListener
然后检查您选择的内容并更新输出,然后启动另一个 Timer
,等待 1 秒,然后重新启用按钮...
import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.Timer;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
public class Chocies
public static void main(String[] args)
new Chocies();
public Chocies()
EventQueue.invokeLater(new Runnable()
@Override
public void run()
try
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex)
JFrame frame = new JFrame("Testing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new BorderLayout());
frame.add(new TestPane());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
);
public class TestPane extends JPanel
private JButton choice1;
private JButton choice2;
private JButton choice3;
private JLabel output;
private JButton choice;
public TestPane()
setLayout(new BorderLayout());
choice1 = new JButton("Door 1");
choice2 = new JButton("Door 2");
choice3 = new JButton("Door 3");
JPanel panel = new JPanel();
panel.add(choice1);
panel.add(choice2);
panel.add(choice3);
output = new JLabel("Pick a door");
output.setHorizontalAlignment(JLabel.CENTER);
add(output, BorderLayout.NORTH);
add(panel);
ButtonHandler handler = new ButtonHandler();
choice1.addActionListener(handler);
choice2.addActionListener(handler);
choice3.addActionListener(handler);
public class ButtonHandler implements ActionListener
@Override
public void actionPerformed(ActionEvent e)
output.setText("Wait for it...");
choice = (JButton) e.getSource();
choice1.setEnabled(false);
choice2.setEnabled(false);
choice3.setEnabled(false);
Timer timer = new Timer(1000, new TimerHandler());
timer.setRepeats(false);
timer.start();
public class TimerHandler implements ActionListener
@Override
public void actionPerformed(ActionEvent e)
if (choice1 == choice)
output.setText("Door 1 selected");
else if (choice2 == choice)
output.setText("Door 2 selected");
else if (choice3 == choice)
output.setText("Door 3 selected");
Timer timer = new Timer(1000, new ActionListener()
@Override
public void actionPerformed(ActionEvent e)
choice1.setEnabled(true);
choice2.setEnabled(true);
choice3.setEnabled(true);
);
timer.setRepeats(false);
timer.start();
【讨论】:
好的,感谢您的回答,我知道了,非常感谢!我刚刚在打印第一条消息后添加了另一个计时器,它可以正常工作。再次感谢您的宝贵时间! @user3534031 抱歉,拖了这么久;)以上是关于Java Swing 运行后重启定时器的主要内容,如果未能解决你的问题,请参考以下文章
Java Swing 2D 面板数组不使用 Swing 计时器重新绘制