Hi!

I just had a quick question that I can't seem to find the answer to anywhere. My book isn't helping me much and I have no one to turn to for help.

Anyways, here's my question:

I want to make it so that when you click a Button in Java it changes the program to another class.

I have a GUI made up right now it looks something like this:

|----|----|
| 21| HM|
|----|----|
| M | M |
|----|----|

They're all games. 21 means the card game 21, HM means Hangman, M means memory, and the other M means matching.

Anyways, I have each little game in their own class and I want to make it so that when you click one of the buttons it loads that class. How can I achieve this? I've looked everywhere and I can't seem to find the answer.

Thanks!

public void actionPerformed(ActionEvent ae)
{
    if(ae.getActionCommand().equals("CardGame")
    {
        CardGame cg = new CardGame();
        cg.startCardGame();
      }
     else if(ae.getActionCommand().equals("Hangman")
      {
          Hangman hg = new Hangman();
          hg.startHangmanGame();
        }
        else
        {
            AnotherGame ag = new AnotherGame();
             ag.startAnotherGame();
          }
}

Also, with the basic structure that Peter showed above, if your game classes extend JPanel (or a similar container class), you can swap that game panel in to replace your menu panel and then swap it back if they exit the game.

Hmm, it keeps telling me that it "cannot find symbol - Method Blackjack() and it's highlighting the " cg.startCardGame();" (In my case: "cg.Blackjack();") part of the code.

What am I doing wrong? Do I have to make a separate method to run the class? Bleh!

You will want something like:

Blackjack bj = new Blackjack();
bj.start();

and have a start() method in your blackjack class that basically runs the game (ie swaps to the Blackjack panel as Ezz suggested earlier).

You will want something like:

Blackjack bj = new Blackjack();
bj.start();

and have a start() method in your blackjack class that basically runs the game (ie swaps to the Blackjack panel as Ezz suggested earlier).

That's the trouble I am having, I don't know how to run another class (Besides double clicking on it). Is it basic Java knowledge?

You will want something like:

Blackjack bj = new Blackjack();
bj.start();

and have a start() method in your blackjack class that basically runs the game (ie swaps to the Blackjack panel as Ezz suggested earlier).

Isn't this exactly as my code, I wonder :?:

That's the trouble I am having, I don't know how to run another class (Besides double clicking on it). Is it basic Java knowledge?

I presume that each of your games has its own main method, if so just swap public void main() for public void start() plus any parameters you wish to pass and receive

1) If you wish to attach any images in the future please use facilities available on this forum, when you press "Go Advanced" button bellow post editing area you will find another button "Manage Attachments". Reason - links to any external reasorces provided by poster stop working/content get removed etc
2) it is exactly what I said provide any parameters for the method in Blackjack.java method should be called public void start() not public void start(String[] args) you not passing any string array from GridWindow.java

Ha! It works but now I'm having a new problem. It freezes when I click on the button that loads the game. It shows the game but you can't put any inputs in. You can click "X" on the new frame but you can't click "X" on the original button frame.

Any thoughts?

Hmm, I just realized why. Because the 2nd program doesn't use JOptionPane. It just uses the regular text input.

Is there anyway I could still load the program after I click the button? or does each program have to be JOption Panes?

Isn't this exactly as my code, I wonder :?:

Apologies peter, but I was trying to reiterate what you had said. From someguy's post I thought that he/she didn't understand, because cg.Blackjack() looked to me like they were trying to call a constructor method or something. I wasn't trying to steal credit for what you had suggested and am sorry if it appeared this way.

This article has been dead for over six months. Start a new discussion instead.