0

I'm working on a little menu for a game. I already did the game itself, but I'm experiencing some problems with my menu. When I click on the buttons "Rules and Controls", "Options", and "About", the JLabels attached to them don't show up.

What am I doing wrong..? Thanks in advance.

import java.awt.Color;
import java.awt.Font;
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.border.Border;



public class Menu extends JFrame
{


    private static final long serialVersionUID = 1L;

    public Menu()
    {


        // Creating a new JFrame and setting stuff

            JFrame frame = new JFrame("BREAK THE BRICKS - MENU");
            frame.setResizable(false);
            frame.setBounds(43, 10, 1280, 720);
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.setVisible(true);

        //Creating a menu panel

            JPanel menupanel = new JPanel();
            menupanel.setLayout(null);
            setContentPane(menupanel);
            frame.add(menupanel);



//PRINCIPAL BUTTONS OF THE MENU

        //Creating buttons

            JButton buttonrules = new JButton();
            JButton buttonoptions = new JButton();
            JButton buttonabout = new JButton();
            JButton buttonplay = new JButton("PLAY");

        //Setting their bounds

            buttonrules.setBounds(56, 224, 400, 83);
            buttonoptions.setBounds(56, 302, 400, 82);
            buttonabout.setBounds(56, 379, 400, 83);
            buttonplay.setBounds(56, 486, 400, 110);

        //Setting their border's color

            buttonrules.setBorder(BorderFactory.createLineBorder(Color.LIGHT_GRAY, 5));
            buttonoptions.setBorder(BorderFactory.createLineBorder(Color.LIGHT_GRAY, 5));
            buttonabout.setBorder(BorderFactory.createLineBorder(Color.LIGHT_GRAY, 5));
            buttonplay.setBorder(BorderFactory.createLineBorder(Color.GRAY, 5));

        //Setting their content and font

            buttonrules.setContentAreaFilled(false);
            buttonoptions.setContentAreaFilled(false);
            buttonabout.setContentAreaFilled(false);
            buttonplay.setFont(new Font("Mesquite Std", Font.PLAIN, 99));

        //Adding them to the principal panel

            menupanel.add(buttonrules);
            menupanel.add(buttonoptions);
            menupanel.add(buttonabout);
            menupanel.add(buttonplay);



//BACKGROUND MENU'S IMAGE

        //Attaching the principal background image to the principal panel

            JLabel labelbackground = new JLabel();
            menupanel.add(labelbackground);
            labelbackground.setBounds(0, 0, 1280, 720);                 
            Image background = new ImageIcon(this.getClass().getResource("/Menu_Principal.jpg")).getImage();
            labelbackground.setIcon(new ImageIcon(background));


//BOXES ON THE RIGHT-HAND SIDE OF THE SCREEN


        //RULES AND CONTROLS

                //Creating a JLabel and setting stuff

                    JLabel labelboxrules = new JLabel();
                    labelboxrules.setForeground(Color.WHITE);
                    labelboxrules.setBounds(475, 159, 754, 500);

                //Importing rules and controls' image and setting it to its label

                    Image rulandconimg = new ImageIcon(this.getClass().getResource("/Rules_And_Controls.jpg")).getImage();
                    labelboxrules.setIcon(new ImageIcon(rulandconimg));


        //OPTIONS

                //Creating a JLabel and setting stuff

                    JLabel labelboxoptions = new JLabel();
                    labelboxoptions.setForeground(Color.WHITE);
                    labelboxoptions.setBounds(475, 159, 754, 500);

                //Importing options' image and setting it to its label

                    Image optionsimg = new ImageIcon(this.getClass().getResource("/Options.jpg")).getImage();
                    labelboxoptions.setIcon(new ImageIcon(optionsimg));




        //ABOUT

                //Creating a JLabel and setting stuff

                    JLabel labelboxabout = new JLabel();
                    labelboxabout.setForeground(Color.WHITE);
                    labelboxabout.setBounds(475, 159, 754, 500);

                //Importing about's image and setting it to its label

                    Image aboutimg = new ImageIcon(this.getClass().getResource("/About.jpg")).getImage();
                    labelboxabout.setIcon(new ImageIcon(aboutimg)); 


    //THEIR FUTURE BORDER

        Border boxborder = BorderFactory.createLineBorder(Color.LIGHT_GRAY, 5);     


//ASSOCIATING ACTIONS WITH MENU'S BUTTONS   

        //Rules and Controls

            buttonrules.addActionListener(new ActionListener()
                    {

                    public void actionPerformed (ActionEvent a)
                        {
                            labelboxrules.setBorder(boxborder);
                            labelbackground.add(labelboxrules);
                            labelboxrules.setVisible(true);
                        }

                    });


        //Options

            buttonoptions.addActionListener(new ActionListener()
                    {

                    public void actionPerformed (ActionEvent a)
                        {
                            labelboxoptions.setBorder(boxborder);
                            labelbackground.add(labelboxoptions);
                            labelboxoptions.setVisible(true);       
                        }

                    });


        //About

            buttonabout.addActionListener(new ActionListener()
                    {

                    public void actionPerformed (ActionEvent a)
                        {
                            labelboxabout.setBorder(boxborder);
                            labelbackground.add(labelboxabout);
                            labelboxabout.setVisible(true);

                        }

                    });




        //Play  

            buttonplay.addActionListener(new ActionListener()
                {

                    public void actionPerformed (ActionEvent c)
                        {

                            Game.myGame();

                        }

                });

    }
}
Dado Del
  • 75
  • 1
  • 4
  • 3
    Don't use null layouts. Swing was designed to be used with layout managers. – camickr Aug 15 '15 at 18:53
  • That rubbish does not even compile. 1) For better help sooner, post a [MCVE] or [Short, Self Contained, Correct Example](http://www.sscce.org/). 2) Provide ASCII art or a simple drawing of the *intended* layout of the GUI at minimum size, and if resizable, with more width and height. – Andrew Thompson Aug 16 '15 at 01:39
  • BTW - a label with no text and either no icon (or a completely transparent icon) is about as 'invisible' as it can get. Just add the labels (using layouts(1)) at start-up, and change the text when needed. 1) Java GUIs have to work on different OS', screen size, screen resolution etc. using different PLAFs in different locales. As such, they are not conducive to pixel perfect layout. Instead use layout managers, or [combinations of them](http://stackoverflow.com/a/5630271/418556) along with layout padding and borders for [white space](http://stackoverflow.com/a/17874718/418556). – Andrew Thompson Aug 16 '15 at 02:25
  • `new Font("Mesquite Std", Font.PLAIN, 99)` That font won't exist on most systems. You'll need to include it with the app., explicitly load it, and add it to the available fonts. The `99` also seems like a steaming pile of guesswork. – Andrew Thompson Aug 16 '15 at 02:31
  • I'll amend 'point 1' above to two points: 1) For better help sooner, post a [MCVE] or [Short, Self Contained, Correct Example](http://www.sscce.org/). 2) *One way to get image(s) for an example is to hot link to images seen in [this Q&A](http://stackoverflow.com/q/19209650/418556).* – Andrew Thompson Aug 16 '15 at 02:40
  • JLabel labelboxrules; JLabel labelbackground; JLabel labelboxoptions; Border boxborder ; JLabel labelboxabout ; should declare as global, otherwise you can not even run the code. – Anptk Aug 18 '15 at 09:41

2 Answers2

1

you have to revalidate and repaint the panel after you add components .but as camicker and madprogrammer pointed out revalidate is pointless when not using layout managers.if you use layout managers then you have to call revalidate before repaint. also by the default jlables are visible unlike jframes so calling labelboxoptions.setVisible(true); is redundant .

for example

 buttonoptions.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent a) {
                labelboxoptions.setBorder(boxborder);
                labelbackground.add(labelboxoptions);
                labelboxoptions.setVisible(true);
                menupanel.repaint();

            }

 });

note: don't use null layout. use layout managers. .

Madhawa Priyashantha
  • 9,633
  • 7
  • 33
  • 60
-1

As suggested by Andrew and MadProgrammer

Dont use setLayout(null),

Updated and removed the not required statements

Garry
  • 4,493
  • 3
  • 28
  • 48