JLabel 在程序开始时未显示在 Java 中

问题描述

所以我正在尝试用 Java 创建一个简单的游戏进行练习,但我一直遇到问题。 应该出现在程序中的第一个 JLabel(text) 没有显示,我不知道该怎么做。我希望它与其他 JLabels 无关,因为那会破坏我的整个游戏。 有谁知道为什么它没有出现?

另外,如果您有更多问题,请随时在下面发表评论。 我为我的英语不好而道歉

我的代码

import java.awt.Color;

import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;

public class MyGame extends JFrame {
    
    JButton wwBtnSelect;
    JButton wwBtnNo;
    JButton wwBtnExit;
    JButton wwBtnYes;
    
    MyGame(){
        // creates the window
        this.setDefaultCloSEOperation(JFrame.EXIT_ON_CLOSE);
        this.setSize(500,500);
        this.setVisible(true);
        this.setTitle("LoL Game");
        this.setResizable(false);
        this.setLayout(null);
        this.setLocationRelativeto(null);
        
        JMenuBar menuBar = new JMenuBar();
        JMenu fileMenu = new JMenu("File");
        JMenu editMenu = new JMenu("Edit");
        JMenu helpMenu = new JMenu("Help");
        
        JMenuItem exitItem = new JMenuItem("Exit");
        
        exitItem.addActionListener(e -> System.exit(0));
        
        fileMenu.add(exitItem);
        
        menuBar.add(fileMenu);
        menuBar.add(editMenu);
        menuBar.add(helpMenu);
        
        this.setJMenuBar(menuBar);
        this.setVisible(true);
        
        // creates the text label
        JLabel label = new JLabel();
        label.setText("Choose your Champion");
        label.setBounds(170,50,134,100);
        label.setVisible(true);
        
        
        
        //creates buttons
        wwBtnSelect = new JButton("Warwick");
        
        wwBtnNo = new JButton();
        wwBtnNo.setVisible(false);
        wwBtnNo.setBorder(BorderFactory.createLineBorder(Color.WHITE));
        
        wwBtnYes = new JButton();
        wwBtnYes.setVisible(false);
        wwBtnYes.setBorder(BorderFactory.createLineBorder(Color.WHITE));
        wwBtnYes.setFocusable(false);
        wwBtnYes.setForeground(Color.WHITE);
        wwBtnYes.setBackground(Color.BLACK);
        wwBtnYes.setopaque(true);
        
        wwBtnExit = new JButton();
        wwBtnExit.setVisible(false);
        wwBtnExit.setFocusable(false);
        wwBtnExit.setForeground(Color.WHITE);
        wwBtnExit.setBackground(Color.BLACK);
        wwBtnExit.setopaque(true);
        wwBtnExit.setBorder(BorderFactory.createLineBorder(Color.WHITE));
        
        wwBtnSelect.setBounds(170,150,50);
        wwBtnSelect.setFocusable(false);
        wwBtnSelect.setForeground(Color.WHITE);
        wwBtnSelect.setBackground(Color.BLACK);
        wwBtnSelect.setopaque(true);
        wwBtnSelect.setBorder(BorderFactory.createLineBorder(Color.WHITE));
        wwBtnSelect.addActionListener(
                e -> {
                    label.setBounds(120,75,250,50);
                    label.setText("<html><div style='text-align: center;'>Oh,a Lee Sin just challenged you to a 1v1! <br/>Will you take the fight?</div></html>");
                    wwBtnYes.setVisible(true);
                    wwBtnYes.setText("Yes");
                    wwBtnYes.setBounds(170,50);
                    wwBtnNo.setVisible(true);
                    wwBtnSelect.setVisible(false);
                }
        );
        
        // player refuses the challenge
        wwBtnNo.setText("No");
        wwBtnNo.setBounds(170,200,50);
        wwBtnNo.setFocusable(false);
        wwBtnNo.setForeground(Color.WHITE);
        wwBtnNo.setBackground(Color.BLACK);
        wwBtnNo.setopaque(true);
        wwBtnNo.addActionListener(
                e -> {
                    wwBtnYes.setVisible(false);
                    wwBtnNo.setVisible(false);
                    label.setText("<html><div style='text-align: center;'>What a coward.</div></html>");
                    label.setBounds(190,50);
                    wwBtnExit.setVisible(true);
                    wwBtnExit.setText("Exit");
                    wwBtnExit.setBounds(170,50);
                    wwBtnExit.addActionListener(r -> this.dispose());
                }
        );
        
        //player accepts the challange
                ImageIcon QAbility = new ImageIcon( getClass().getResource("images/Jaws_of_the_Beast.png"));
                ImageIcon WAbility = new ImageIcon( getClass().getResource("images/Blood_Hunt.png"));
                ImageIcon EAbility = new ImageIcon( getClass().getResource("images/Primal_Howl.png"));
                ImageIcon RAbility = new ImageIcon( getClass().getResource("images/Infinite_Duress.png"));
                    
                JLabel Qability = new JLabel();
                Qability.setText("Q");
                Qability.setIcon(QAbility);
                Qability.setVisible(false);
                Qability.setBounds(90,350,80,80);
                Qability.setHorizontalTextPosition(JLabel.CENTER);
                Qability.setVerticalTextPosition(JLabel.TOP);
                
                JLabel Wability = new JLabel();
                Wability.setText("W");
                Wability.setIcon(WAbility);
                Wability.setVisible(false);
                Wability.setBounds(160,80);
                Wability.setHorizontalTextPosition(JLabel.CENTER);
                Wability.setVerticalTextPosition(JLabel.TOP);
                
                JLabel Eability = new JLabel();
                Eability.setText("E");
                Eability.setIcon(EAbility);
                Eability.setVisible(false);
                Eability.setBounds(230,80);
                Eability.setHorizontalTextPosition(JLabel.CENTER);
                Eability.setVerticalTextPosition(JLabel.TOP);
                
                JLabel Rability = new JLabel();
                Rability.setText("R");
                Rability.setIcon(RAbility);
                Rability.setVisible(false);
                Rability.setBounds(300,80);
                Rability.setHorizontalTextPosition(JLabel.CENTER);
                Rability.setVerticalTextPosition(JLabel.TOP);
                
                wwBtnYes.addActionListener(
                        r -> {
                            wwBtnNo.setVisible(false);
                            wwBtnYes.setVisible(false);
                            label.setVisible(false);
                            Qability.setVisible(true);
                            Wability.setVisible(true);
                            Eability.setVisible(true);
                            Rability.setVisible(true);
                            }
                );
        
        
        // adds the elements
        this.add(label);
        this.add(wwBtnSelect);
        this.add(wwBtnNo);
        this.add(wwBtnExit);
        this.add(wwBtnYes);
        
        this.add(Qability);
        this.add(Wability);
        this.add(Eability);
        this.add(Rability);
    }

}

解决方法

不使用 LayoutManager 既丑陋又容易出错。通过这种方式,添加到框架中的等位基因组件通常会在默认位置 (0,0) 上彼此重叠放置。

我强烈推荐至少

setLayout(new FlowLayout());

帮助布局管理器放置子组件。为了更好地控制放置,我强烈建议使用这样的 GridBagLayout setLayout(new GridBagLayout()); 添加组件需要您添加一些关于如何相对于彼此放置组件的提示。使用这个例子来了解这个想法。 (顺便说一句:我确实在我的例子中使用了一个专用的 JFrame,没有无缘无故地扩展 JFrame 类。但这只是个人喜好)

JFrame f = new JFrame();
f.setLayout(new GridBagLayout());
GridBagConstraints gbc= new GridBagConstraints();
gbc.gridx = 1;
gbc.gridy = 1;
f.add(new JLabel("test"),gbc);
gbc.gridy++;
f.add(new JLabel("below"),gbc);
f.pack();
f.setVisible(true);

此外,我会首先放置所有组件,然后打包框架以对其进行布局并在最后显示它。一旦显示框架,稍后添加组件就不是即时的。你必须通知框架的变化...