java - 除非调整大小,否则 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),但都不起作用。请帮忙
解决方案
推荐阅读
- c++ - Denstructor 删除已被替换的成员?
- php - Visual Studio Code 和 Laravel 命令
- r - flextable:set_header_df 导致 R 会话崩溃
- git - 在 Git repo(不是 github)上使用 SVN 客户端
- node.js - 从 FTP 下载并直接将其复制到 Azure Blob/Amazon S3 中,而无需将其存储在 Node.js 中的本地系统中
- ios - SwiftUI:动态“列表”中的“切换”在重用时破坏了它们的布局?
- unit-testing - 使用 Mockito 模拟挂起函数返回 null
- c++ - 为什么这个示例 C++ 代码片段中的第一个参数是对象,而不是对象的内存地址?
- c++ - Cin 和 getline 不会正确保存非 ascii 字符
- html - 如何将图像放在容器的顶部中心