我正试着在图形用户界面上做一个RPG游戏,但进展不是很顺利。在我添加JButton之前,一切都正常工作,并在窗口中正常显示。我不确定在我添加JButton之后发生了什么。标题应该显示在灰色区域,按钮显示在蓝色区域。我尝试过正常运行和使用调试器运行,但没有显示任何文本或按钮。我正在一步一步地遵循here教程,我没有看到任何不对劲的地方。(我知道我已经更改了变量名)。
我在这里做错了什么?我需要添加额外的东西吗?
import javax.swing.*;
import java.awt.*;
public class Game {
JFrame window;
Container c;
JPanel titlePanel;
JPanel startButtonPanel;
JLabel titleLabel;
JButton startButton;
Font titleFont = new Font("Cooper Black", Font.PLAIN, 90);
Font buttonFont = new Font("Cooper Black", Font.PLAIN, 32);
public static void main(String[] args) {
new Game();
}
public Game() {
//Main Window
window = new JFrame();
window.setSize(800, 600);
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
window.getContentPane().setBackground(Color.BLACK);
window.setLayout(null);
window.setVisible(true);
c = window.getContentPane();
//Title Panel
titlePanel = new JPanel();
titlePanel.setBounds(100, 100, 600, 150);
titlePanel.setBackground(Color.GRAY);
titleLabel = new JLabel("TEXT RPG");
titleLabel.setForeground(Color.WHITE);
titleLabel.setFont(titleFont);
//Start Button Panel
startButtonPanel = new JPanel();
startButtonPanel.setBounds(300, 400, 200, 100 );
startButtonPanel.setBackground(Color.BLUE);
//Start Button
startButton = new JButton("START");
startButton.setBackground(Color.BLACK);
startButton.setForeground(Color.WHITE);
startButton.setFont(buttonFont);
//Add Elements to Window
titlePanel.add(titleLabel);
startButtonPanel.add(startButton);
//Add Elements to Container
c.add(titlePanel);
c.add(startButtonPanel);
}
}
https://stackoverflow.com/questions/53770715
复制相似问题