Skip to main content
gamedev.net
🔒 Locked

[java] Changing Menus

Started by BlackReign 3 replies 700+ views
Original Post
BlackReign
BlackReign
I'm using swing in an applet, and I can't seem to figure out how to swith menus for my game(I get the error : can't find symbol varable singleplayerMenu). Also the command mainMenu.setLayout(new BoxLayout( mainMenu, BoxLayout.Y_AXIS ) ); doesn't seem to work for aligning the buttons vetrically, can someone enlighten me?


import javax.swing.*;
import java.awt.*;
import java.awt.event.*;


public class Pearson extends JApplet implements ActionListener {

  protected JButton singleplayerButton, multiplayerButton, creditsButton,playsingleButton;
  
 

  
    public Pearson() {
     
    }

    public void init() {         
        
        //main menu button set-up
        singleplayerButton = new JButton("Play");
        singleplayerButton.setVerticalTextPosition(AbstractButton.CENTER);
        singleplayerButton.setHorizontalTextPosition(AbstractButton.LEFT);
        singleplayerButton.setActionCommand("play");

        multiplayerButton = new JButton("Multiplayer");
        multiplayerButton.setVerticalTextPosition(AbstractButton.BOTTOM);
        multiplayerButton.setHorizontalTextPosition(AbstractButton.CENTER);
        multiplayerButton.setActionCommand("multiplayer");

        creditsButton = new JButton("Credits");
        creditsButton.setVerticalTextPosition(AbstractButton.BOTTOM);
        creditsButton.setHorizontalTextPosition(AbstractButton.CENTER);
        creditsButton.setActionCommand("credits");
        
        //single player menu button set-up
        playsingleButton = new JButton("Play");
        playsingleButton.setVerticalTextPosition(AbstractButton.CENTER);
        playsingleButton.setHorizontalTextPosition(AbstractButton.LEFT);
        playsingleButton.setActionCommand("playsingle");
       
        //Listeners for buttons in main menu panel
        singleplayerButton.addActionListener(this);
        multiplayerButton.addActionListener(this);
        creditsButton.addActionListener(this);
        
        //Listeners for buttons in single player menu panel
        
        singleplayerButton.addActionListener(this);
        
        //Panel set-up
        
        JPanel mainMenu = new JPanel();
        JPanel singleplayerMenu = new JPanel();
        JPanel multiplayerMenu = new JPanel();
        
        mainMenu.setLayout(new BoxLayout( mainMenu, BoxLayout.Y_AXIS ) );
        
        mainMenu.add(singleplayerButton);
        mainMenu.add(multiplayerButton);
        mainMenu.add(creditsButton);
        mainMenu.setBackground(new Color(255,30,60));
        mainMenu.setBorder(BorderFactory.createMatteBorder(1,1,2,2,Color.black));

        singleplayerMenu.add(playsingleButton);
        singleplayerMenu.setBackground(new Color(255,30,60));
        singleplayerMenu.setBorder(BorderFactory.createMatteBorder(1,1,2,2,Color.black));
        
        singleplayerButton.addActionListener( this );
        
        setContentPane(mainMenu);
    }

    public void actionPerformed(ActionEvent evt) {
      
      
        setContentPane(singleplayerMenu);
        
    }
    
    
    
    public static void main(String[] args) {
        
      Pearson applet = new Pearson();

   
        
      
    }
}



5MinuteGaming
5MinuteGaming

I believe that your problem with the compile error is that singleplayMenu, mainMenu, and multiplayerMenu are not defined as members of the class.

Also, whenever you change the contents of a container you need to call the validate() method on that container in order for the newly added components to be drawn on the screen.

import javax.swing.*;import java.awt.*;import java.awt.event.*;public class Pearson extends JApplet implements ActionListener {	private JButton singleplayerButton, multiplayerButton, creditsButton,playsingleButton;  	private JPanel mainMenu = new JPanel();    private JPanel singleplayerMenu = new JPanel();    private JPanel multiplayerMenu = new JPanel();        public Pearson() {         }    public void init() {                         //main menu button set-up        singleplayerButton = new JButton("Play");        singleplayerButton.setVerticalTextPosition(AbstractButton.CENTER);        singleplayerButton.setHorizontalTextPosition(AbstractButton.LEFT);        singleplayerButton.setActionCommand("play");        multiplayerButton = new JButton("Multiplayer");        multiplayerButton.setVerticalTextPosition(AbstractButton.BOTTOM);        multiplayerButton.setHorizontalTextPosition(AbstractButton.CENTER);        multiplayerButton.setActionCommand("multiplayer");        creditsButton = new JButton("Credits");        creditsButton.setVerticalTextPosition(AbstractButton.BOTTOM);        creditsButton.setHorizontalTextPosition(AbstractButton.CENTER);        creditsButton.setActionCommand("credits");                //single player menu button set-up        playsingleButton = new JButton("Play");        playsingleButton.setVerticalTextPosition(AbstractButton.CENTER);        playsingleButton.setHorizontalTextPosition(AbstractButton.LEFT);        playsingleButton.setActionCommand("playsingle");               //Listeners for buttons in main menu panel        singleplayerButton.addActionListener(this);        multiplayerButton.addActionListener(this);        creditsButton.addActionListener(this);                //Listeners for buttons in single player menu panel                singleplayerButton.addActionListener(this);                //Panel set-up                mainMenu.setLayout(new BoxLayout( mainMenu, BoxLayout.Y_AXIS ) );                mainMenu.add(singleplayerButton);        mainMenu.add(multiplayerButton);        mainMenu.add(creditsButton);        mainMenu.setBackground(new Color(255,30,60));        mainMenu.setBorder(BorderFactory.createMatteBorder(1,1,2,2,Color.black));        singleplayerMenu.add(playsingleButton);        singleplayerMenu.setBackground(new Color(255,30,60));        singleplayerMenu.setBorder(BorderFactory.createMatteBorder(1,1,2,2,Color.black));                singleplayerButton.addActionListener( this );                setContentPane(mainMenu);    }    public void actionPerformed(ActionEvent evt) {        setContentPane(singleplayerMenu);     	validate();    }}


In addtion the main function is not needed since this is an applet and can only be run either from an appletviewer or a browser

Add this code into a .html document to load from a browser.

<applet     code="Pearson"     width="320"     height="200"></applet>
RobAU78
RobAU78
Your problem is a scope issue. The JPanel singleplayerMenu is declared inside your init() method, so when that method returns, all of its local variables are popped from the stack. To solve the problem, just move the declaration outside of the method, making it an object variable.

Hope this helps,

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.