循环 - 定时

时间:2015-10-15 23:28:52

标签: java swing while-loop sleep

我正在重新创建一个非常糟糕的cookie点击器版本,只是为了测试我的技能。好吧,我遇到了一个问题,我无法弄清楚如何每3秒重复一次循环。 主类代码:

import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.JFrame;

public class CookieClickerGame {
    private static final int FRAME_WIDTH = 200;
    private static final int FRAME_HEIGHT = 200;

    public static void main(String[] args) {
        JPanel panel = new JPanel();

        JFrame frame = new JFrame();
        JButton cookie = new JButton("Cookie");
        JButton grandma = new JButton("Grandma");
        JButton robot = new JButton("Robot");
        panel.add(cookie);
        panel.add(grandma);
        panel.add(robot);
        frame.add(panel);

          /*----------------------------------------/
         /              Buttons                    /
        /----------------------------------------*/
        ActionListener listener = new CookieClicker();
        cookie.addActionListener(listener);

        ActionListener listener2 = new CookieClicker();
        grandma.addActionListener(listener2);

        ActionListener listener3 = new CookieClicker();
        robot.addActionListener(listener3);

          /*---------------------------------------------------------/
         /                       Frame                              /
        /---------------------------------------------------------*/
        frame.setSize(FRAME_WIDTH, FRAME_HEIGHT);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);
    }
}

听众代码:

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JOptionPane;

public class CookieClicker implements ActionListener {
    static int cookies;
    public void actionPerformed(ActionEvent event) {
        if (event.getActionCommand().equals("Cookie")) {
            cookies++;
            System.out.print('\f');
            System.out.println(cookies + " Cookies");
        } else if (event.getActionCommand().equals("Grandma") && cookies >= 10) {
            System.out.println("+1 Grandma | -10 Cookies");
            cookies = cookies - 10;
            System.out.println(cookies + " Cookies");
            while (true) {
                cookies = cookies + 1;
                System.out.print('\f');
                System.out.println(cookies + " Cookies");
                Thread.sleep(3000);
               }
        } else if (event.getActionCommand().equals("Robot") && cookies >= 50) {
            System.out.println("+1 Robot | -50 Cookies");
            cookies = cookies - 50;
            System.out.println(cookies + " Cookies");
            cookies = cookies + 1;
            System.out.print('\f');
            System.out.println(cookies + " Cookies");
        } else {
            JOptionPane.showMessageDialog (null, "Not Enough Cookies", "You're Poor", JOptionPane.INFORMATION_MESSAGE);
           }
       }
    }

Thread.sleep(3000)无效,有什么问题?

1 个答案:

答案 0 :(得分:1)

Swing是一个单线程框架,您不应该尝试在事件调度线程的上下文中执行任何长时间运行或阻止操作。

有关详细信息,请查看Concurrency in Swing

虽然您可以使用Thread来解决问题,但更简单的解决方案是使用Swing Timer,因为它的事件回调是在事件调度线程的上下文中触发的,可以安全地从

中更新UI

有关详细信息,请参阅How to use Swing Timers

像...一样的东西。

} else if (event.getActionCommand().equals("Grandma") && cookies >= 10) {
    System.out.println("+1 Grandma | -10 Cookies");
    cookies = cookies - 10;
    System.out.println(cookies + " Cookies");
    if (timer == null || !timer.isRunning()) {
        timer = new Timer(3000, new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                cookies = cookies + 1;
                System.out.print('\f');
                System.out.println(cookies + " Cookies");
                // I don't know what your exit condition is, but you can use
                // ((Timer)e.getSource()).stop();
                // to stop the timer from repeating
            }

        });
        timer.start();
    }
}

例如