簡體   English   中英

搖擺,JLabel沒有出現

[英]Swing, JLabel doesn't show up

我正在為游戲制作一個小菜單。 我已經完成了游戲本身,但是菜單遇到一些問題。 當我單擊“規則和控件”,“選項”和“關於”按鈕時,連接到它們的JLabel不會顯示。

我究竟做錯了什么..? 提前致謝。

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

                        }

                });

    }
}

添加組件后必須revalidate面板並repaint面板。但是正如camicker和madprogrammer所指出的,當不使用布局管理器時revalidate是沒有意義的。如果使用布局管理器,則必須在重新繪制之前調用revalidate 默認情況下,jlables也不同於jframe,因此可見,因此調用labelboxoptions.setVisible(true); 是多余的。

例如

 buttonoptions.addActionListener(new ActionListener() {

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

            }

 });

注意: 請勿使用空布局。 使用布局管理器。

如Andrew和MadProgrammer所建議

不要使用setLayout(null)

更新並刪除了不需要的語句

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM