简体   繁体   中英

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.

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").

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.

EG The Swing Timer , this is an easy way to loop & pause rendering in a GUI - it offers methods like 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.

You are in the area of GUI-programming which is basically using events ( event driven )

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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