WhatTheHacker
WhatTheHacker

Reputation: 1

Running Applet Out of JButton

I have a Applet game, and it consists of a Applet class for the game, and a regular drawpanel class for the graphics. I wanted to use a third class to have a main menu, that had several JButtons to direct to other class I want to make, but more importantly to open the Applet game class. Does the Menu Class have to be an Applet? I wouldn't think it would, but if not, how could I use a line of code similar to

Pacman g = new Pacman();

To open a Applet class?

Thank You!

(I don't have many years of programming under my belt, as I am just a tenth grade student in AP Computer Science)

Upvotes: 0

Views: 122

Answers (1)

Paul Samsotha
Paul Samsotha

Reputation: 208944

"Does the Menu Class have to be an Applet?"

No, just use a CardLayout. Put another JPanel class in the Applet class. So you have your DrawPanel and MenuPanel. See How to Use CardLayout

public class MyApplet extends JApplet {
    DrawPanel drawPanel = new DrawPanel();
    MenuPanel menuPanel = new MenuPanel();

    class DrawPanel extends JPanel {
    }

    class MenuPanel extends JPanel {
    }
}

See the running example from How do I Switch JPanels inside a JFrame for a simple implementation of CardLayout. The basic concept is to layer the JPanel on top of one another. And you can use a button click to show a different panel. So you would start with with MenuPanel, then a button on that panel can cause the CardLayout to show the DrawPanel


Side Note

Upvotes: 2

Related Questions