简体   繁体   中英

A main menu for class A extends Applet

Hi i have made a game in java that extends applet. The game works perfectly fine but one of the requirements of this assignment is that there should be a menu. For example: As the program is run a screen with "Play" and "Quit" Options should appear and if user clicks "Play", this should lead on to the game, etc...

Q) Is there a way to do this specifically for applets?

I have attempted to make aa menu using the following code but it doesn't work (I think this is only for extends JPanel or JFrame not extends Applet):

MainMenu.java

public class MainMenu extends JFrame {

    int screenWidth = 200;
    int screenHeight = 150;

    int buttonWidth = 100;
    int buttonHeight = 40;

    JButton Play;
    JButton Quit;

    public MainMenu() {
        addButtons();
        addActions();

        Play.setBounds((screenWidth - buttonWidth)/2, 5 , buttonWidth, buttonHeight); // Positions the play button
        Quit.setBounds((screenWidth - buttonWidth)/2, 10 , buttonWidth, buttonHeight);

        //Adding buttons
        getContentPane().add(Play); //add the button to the Frame
        getContentPane().add(Quit);

        pack();
        setVisible(true);
        setLocationRelativeTo(null);
        setSize(screenWidth , screenHeight);
        setTitle("Drop");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setResizable(false);

    }

    private void addButtons() {
        Play = new JButton ("Play");
        Quit = new JButton ("Quit");

    }

    private void addActions() {
        Play.addActionListener(new ActionListener() {   // takes play button, adds new actionlistener

            public void actionPerformed(ActionEvent e) { // Turn actionPerformed into variable for usage
                dispose();                               // wipes out Jframe

                Board game = new Board();

                game.run();
            }
        }); //Play Button

        Quit.addActionListener(new ActionListener() {   // takes quit button, adds new actionlistener

            public void actionPerformed(ActionEvent e) { // Turn actionPerformed into variable for usage
                System.exit(0); 
            }
        }); //Quit Button
    }
}

Launcher.java (Where menu is run from)

public class Launcher {

    public static void main (String[] args){

        new MainMenu();
    }
}

Any help is much appreciated (Ideas, tutorials...)

对于一个空间中的许多组件,请使用CardLayout如本简短示例所示

I just ended up making a text based menu.

Initially set a variable to true, for eg: menu = true and make the paint method paint whatever you want on the menu, like start...

    if(menu) {

      paint what's on the menu

    }

then when when user clicks on a certain option within the menu turn menu variable to false ie menu = false.

You need to get mouse input to get the user's input so use either the mouse pressed or mouse clicked methods that come with the mouselistener.

After turning it false, get the paint method to paint your game. ie

  if(!menu) {

  paint the game

  }

Pretty much a bunch of if statements.

Hope this helps someone.

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