操作后Java Swing重启计时器 [英] Java Swing Restart Timer After Operation

查看:162
本文介绍了操作后Java Swing重启计时器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要我的计时器重启或至少在执行一行代码后添加另一个延迟。

I need my timer to restart or at least add another delay after a certain line of code is performed.

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();     
    }
} 

我想实现定时器的第二次延迟紧接着

I would like to implement the second delay of the timer right after

them.setText("they chose: " + tc + "!");

但我不知道怎么做,我应该重启计时器,如果有的话我会写那行代码吗?在此先感谢。

yet I am not sure how to do this, am I supposed to restart the timer and if so where would I write that line of code? Thanks in advance.

推荐答案

我打算将此发布到您的上一个问题,但似乎你已经取得了一些进展,做得很好。

I was going to post this into your previous question, but it seems you've some progress, well done.

好的,这个例子给你三个按钮可供选择。单击一个时,它会记录您选择的内容,禁用按钮并启动等待1秒的计时器

Okay, so, this example gives you three buttons to choose from. When you click one, it records what you choose, disables the buttons and starts a Timer which waits 1 second.

计时器 ActionListener 然后检查看你选择什么并更新输出,然后启动另一个计时器等待1秒,然后重新启用按钮......

The Timer's ActionListener then checks to see what you choose and updates the output, then starts another Timer which waits 1 second, which then re-enables the buttons...

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();
            }

        }

    }

}

这篇关于操作后Java Swing重启计时器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆