简体   繁体   English

摇摆,JLabel没有出现

[英]Swing, JLabel doesn't show up

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. 当我单击“规则和控件”,“选项”和“关于”按钮时,连接到它们的JLabel不会显示。

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();

                        }

                });

    }
}

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. 添加组件后必须revalidate面板并repaint面板。但是正如camicker和madprogrammer所指出的,当不使用布局管理器时revalidate是没有意义的。如果使用布局管理器,则必须在重新绘制之前调用revalidate also by the default jlables are visible unlike jframes so calling labelboxoptions.setVisible(true); 默认情况下,jlables也不同于jframe,因此可见,因此调用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. 使用布局管理器。 .

As suggested by Andrew and MadProgrammer 如Andrew和MadProgrammer所建议

Dont use setLayout(null) , 不要使用setLayout(null)

Updated and removed the not required statements 更新并删除了不需要的语句

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM