1

What happens after the execution of actionPerformed? Is there any way I can go back to the main class of the program after actionPerformed executes?

Here is the sample of the code:

public final class JavaGame extends JFrame {

    public JavaGame(){
        int x = cache();
        JButton butt = new JButton("NEW");

        newG gameX = new newG();
        butt.addActionListener(gameX);
    }

    public class gameFunction implements ActionListener{
        @Override
        public void actionPerformed(ActionEvent e){
            if(e.getSource() == butt){
                //do something logic here. Like go to another stage of the game
                JOptionPane.showMessageDialog(null, Success!");
            }
        }
    }

    public cache(){
        return 1;
    }

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

}

After actionPerformed runs, what happens next?

In my real code, after actionPerformed runs, the stage of my game is still the same. How can I go back to main so that I can start a new stage without closing and opening the program again?

Jason C
  • 38,729
  • 14
  • 126
  • 182
Port 8080
  • 858
  • 3
  • 12
  • 24
  • I hate to be immature but if(e.getSource() == butt) lol. – Omaha Aug 12 '14 at 15:18
  • 1
    This is not how a GUI-application works. You just start it and keep handling events that are dispatched to your listeners. So when actionperformed terminates nothing more happens beside your application waiting for the next event. – A4L Aug 12 '14 at 15:20
  • I think you would benefit greatly from working through the [official Swing trail](http://docs.oracle.com/javase/tutorial/uiswing/), or at least the [Concurrency in Swing](http://docs.oracle.com/javase/tutorial/uiswing/concurrency/index.html) and [Writing Event Listeners](http://docs.oracle.com/javase/tutorial/uiswing/events/index.html) sections. – Jason C Aug 12 '14 at 16:49
  • By the way, I think this is a decent question on its own, but I think you should delete the *"In my real code, after actionPerformed runs, the stage of my game is still the same. How can I go back to main so that I can start a new stage without closing and opening the program again?"* part and let the question stand alone. There isn't enough info here to diagnose a problem with your actual game, which could have a different cause, and the presence of that risks your question being closed. I suggest creating a new question about that if you're still having issues after reading the answers here. – Jason C Aug 12 '14 at 16:55

2 Answers2

2

In the main you show the JFrame:

new JavaGame().setVisible(true);

and then you return from main.

In the background there is a thread running, called EDT, event dispatching thread. On that thread button clicks, repainting and so on are handled.

That should be done in a short period, to keep the user interface responsive, not freezed.

So you need to work event driven: you can often add a listener for some specific event, like closing the JFrame, menu items, buttons. Those listeners are called with an event object specifying what happened. You called addActionListener.

The control flow is to keep the button click responsive, and doing hard work a bit later:

@Override
public void actionPerformed(ActionEvent evt) {
    EventQueue.invokeLater(new Runnable() {
        @Override
        public void run() {
            JavaGame.this.createANewAvatar();
        }
    });
}

And then somewhere have a

private void createNeewAvatar() { ... }

This hard to follow control-flow (because fragmentary) needs a good naming, and organized sources.

Joop Eggen
  • 107,315
  • 7
  • 83
  • 138
  • what does the `JavaGame.this` refer to? gameFunction? – Honinbo Shusaku Aug 12 '14 at 16:05
  • 1
    There are many nested classes here: `JavaGame.this` is like `this` an implicit field of the object. It also has a `Runnable.this` for instance. With `JavaGame.this` you have access to the members (methods, fields) of the JavaGame object. In general you can leave it out and simple call `createANewAvatar()`. – Joop Eggen Aug 12 '14 at 16:11
2

As a concrete example, this simple game includes a Reset button that invokes reset() on the game's model. The listening view see a null value of arg as a signal to update the display accordingly.

image

Community
  • 1
  • 1
trashgod
  • 203,806
  • 29
  • 246
  • 1,045