首页 > 解决方案 > 除非调整大小,否则 JPanel 拒绝显示在窗口中

问题描述

我是 Java 的 GUI 新手,我一直在使用 VSCODE。每当我运行此代码时,在通过标题屏幕(由第一个按钮提示)之后,除非我调整窗口大小,否则只会显示 choiceButtonPanel。每个其他面板都显示得非常好,我尝试使用 validate() 和 setVisible(true) 将其位置与其他面板切换,弄乱坐标,但这些都没有奏效。请帮忙。

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

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


public class Test {
    JFrame window;
    Container con;
    JPanel choiceButtonPanel, titleNamePanel, startButtonPanel, mainTextPanel;
    JLabel titleNameLabel;
    JButton startButton;
    Border border = BorderFactory.createLineBorder(Color.white);
    Font titleFont = new Font("Times New Roman", Font.PLAIN, 80);
    Font buttonFont = new Font("Times New Roman", Font.PLAIN, 30);
    Font normalFont = new Font("Times New Roman", Font.PLAIN, 25);
    JTextArea mainTextArea;
    TitleScreenHandler tsHandler = new TitleScreenHandler();

    public static void main(String[] args){
        new Test();
    }
    public Test(){
        window = new JFrame();
        window.setSize(800, 600);
        window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        window.getContentPane().setBackground(Color.black);
        window.setLayout(null);
        window.setVisible(true);
        con = window.getContentPane();
        
        titleNamePanel = new JPanel();
        titleNamePanel.setBounds(93, 100, 600, 100);
        titleNamePanel.setBackground(Color.black);
        titleNameLabel = new JLabel("PLACEHOLDER");
        titleNameLabel.setForeground(Color.white);
        titleNameLabel.setFont(titleFont);
        titleNameLabel.setVisible(true);

        startButtonPanel = new JPanel();
        startButtonPanel.setBounds(292, 290, 210, 60);
        startButtonPanel.setBackground(Color.black);

        startButton = new JButton(" START GAME ");
        startButton.setBackground(Color.black);
        startButton.setForeground(Color.white);
        startButton.setFont(buttonFont);
        startButton.setBorder(border);
        startButton.addActionListener(tsHandler);

        titleNamePanel.add(titleNameLabel);
        startButtonPanel.add(startButton);

        con.add(titleNamePanel);
        con.add(startButtonPanel);
    }

    public void createGameScreen(){
        
        titleNamePanel.setVisible(false);
        startButtonPanel.setVisible(false);

        mainTextPanel = new JPanel();
        mainTextPanel.setBounds(100, 100, 600, 250);
        mainTextPanel.setBackground(Color.blue);

        mainTextArea = new JTextArea("TEST FOR TEXT");
        mainTextArea.setBounds(100, 100, 600, 250);
        mainTextArea.setBackground(Color.black);
        mainTextArea.setForeground(Color.white);
        mainTextArea.setFont(normalFont);
        mainTextArea.setLineWrap(true);
        
        mainTextPanel.add(mainTextArea);

        choiceButtonPanel = new JPanel();
        choiceButtonPanel.setBounds(250, 350, 300, 150);
        choiceButtonPanel.setBackground(Color.red);

        con.add(choiceButtonPanel);
        con.add(mainTextPanel);

    }

    public class TitleScreenHandler implements ActionListener{
        
        public void actionPerformed(ActionEvent event){

            createGameScreen();
        }
    }
}

我尝试过使用 validate() 和 setVisible(true),但都不起作用。请帮忙

标签: javaswingjframeresizejpanel

解决方案


推荐阅读