简体   繁体   English

如何使Java程序暂停直到按下特定按钮?

[英]How can I make my Java program pause until a specific button has been pressed?

I am currently writing a game similar to brick breaker and need to know how to make my program wait for a specific function to be called. 我目前正在编写类似于打砖块的游戏,并且需要知道如何使我的程序等待调用特定功能。

I need to pause the program until a decision has been made between buttons. 我需要暂停程序,直到在按钮之间做出决定为止。 For example, the game starts up, and the user is given a choice of which colors he wants in his game. 例如,游戏启动,并且向用户提供他想要在游戏中选择哪种颜色的选择。 Thus, the game pauses until his selection has been made and one he is happy then the program needs to resume to a different stage. 因此,游戏会暂停直到做出选择为止,然后他会很高兴,然后程序需要恢复到另一个阶段。

If you're using Swing components, you can add an ActionListener to the button and include the logic that needs to be executed within the overridden actionPerformed method. 如果使用的是Swing组件,则可以将ActionListener添加到按钮上,并将需要执行的逻辑包含在重写的actionPerformed方法中。

See also How to Write an Action Listener . 另请参见如何编写动作侦听器

Edit 编辑

I need to pause the program until a decision has been made between buttons. 我需要暂停程序,直到在按钮之间做出决定为止。 For example, the game starts up, and the user is given a choice of which colors he wants in his game. 例如,游戏启动,并且向用户提供他想要在游戏中选择哪种颜色的选择。 Thus, the game pauses until his selection has been made and one he is happy then the program needs to resume to a different stage. 因此,游戏会暂停直到做出选择为止,然后他会很高兴,然后程序需要恢复到另一个阶段。

There are several ways of going about this, but I'm not about to delve into the intricacies of concurrent programming here; 有几种方法可以解决此问题,但是我不想在这里深入探讨并发编程的复杂性。 you can find other resources for that. 您可以找到其他资源。 That being said, one comment I would like to make is that you should neither execute long-running tasks, nor sleep in the Event Dispatch Thread (EDT) because doing so will cause the UI to become unresponsive (ie "freeze"). 话虽如此,我想说的一句话是,您既不应该执行长时间运行的任务,也不应该在事件分发线程(EDT)中休眠,因为这样做会导致UI变得无响应(即“冻结”)。

I need to pause the program.. 我需要暂停程序。

For games such as brick-breaker, you would normally have an animation loop. 对于诸如打砖块之类的游戏,通常会有一个动画循环。 On button press, act as advised by user132.. As to what to do when that happens, it depends on how the code is organized to loop in the first place. 按下按钮,以行动为user132建议..至于做什么 ,这时,它取决于代码是如何在第一时间组织循环。

EG The Swing Timer , this is an easy way to loop & pause rendering in a GUI - it offers methods like start() & stop() . EG Swing Timer ,这是在GUI中循环和暂停渲染的一种简便方法-它提供了start()stop() If you had multiple timers running (e..g one for the rendering loop, another for a 'count-down timer' for the level) you'd obviously need to pause them all. 如果您有多个计时器在运行(例如,一个用于渲染循环,另一个用于关卡“倒数计时器”),您显然需要将其全部暂停。

More details might be forthcoming with an SSCCE of your current code. 当前代码的SSCCE可能会提供更多详细信息。

You are in the area of GUI-programming which is basically using events ( event driven ) 您位于GUI编程领域,该领域基本上是使用事件事件驱动

In some way you asked the wrong question. 您以某种方式提出了错误的问题。

In an event driving system you are always waiting. 事件驱动系统中,您始终在等待。 You continue with work when some event happens. 当某些事件发生时,您将继续工作。

In your case you should think what to do when the button is pressed. 在您的情况下,您应该考虑按下按钮时该怎么办。

Here is a basic outline of the kind of structure you will need. 这是您将需要的结构的基本概述。

    import java.swing.*
    public class Breaker
    {
        private 
        Listener listener;
        JButton b1;

        public Breaker()
        {
          listener = new Listener();
          b1 = new JButton("b1");
          b1.addActionListener(listener);

        }

        class Listener implements ActionListener
        {
            public void actionPerformed(ActionEvent e)
            Component theEventer = (Component) e.getSource();

            if (theEventer == b1)
            {
                You're stuff here
            }

        }
        public static void main (String[] args)
        {
            new Breaker();
        }
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM