import java.awt.*;
import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.*;
import static java.awt.Color.black;
import static java.awt.Color.white;
public class Game {
//GUI variables
JFrame window;
Container container;
JPanel titleNamePanel, startButtonPanel, mainTextPanel;
JLabel titleNameLabel;
Font titleFont = new Font("Times New Roman", Font.PLAIN, 98);
Font startButtonFont = new Font("Times New Roman", Font.PLAIN, 50);
Font normalFont = new Font("Times New Roman", Font.PLAIN, 28);
JButton startButton;
JTextArea mainTextArea;
TitleScreenHandler TSHandler = new TitleScreenHandler();
ChoiceHandler choiceHandler = new ChoiceHandler();
public static void main(String[]args){
new Game();
}
public Game(){
//window
window = new JFrame();
window.setSize(800, 800);
window.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
window.getContentPane().setBackground(black);
window.setLayout(null);
window.setTitle("ADVENTURE CAVE");
//Title Name
titleNamePanel.setBounds(100, 20, 600, 200);
titleNamePanel.setBackground(black);
titleNameLabel = new JLabel("ADVENTURE CAVE");
titleNameLabel.setForeground(white);
//Start button
startButtonPanel = new JPanel();
startButtonPanel.setBounds(300, 270, 250, 120);
startButtonPanel.setBackground(black);
startButtonPanel.setFont(startButtonFont);
startButton.addActionListener(TSHandler);
startButton.setFocusPainted(false);
window.setVisible(true);
}
public void gotoMainGameScreen(){
titleNamePanel.setVisible(false);
startButtonPanel.setVisible(false);
//Main Panel
mainTextPanel = new JPanel();
mainTextPanel.setBounds(100, 100, 600, 250);
mainTextPanel.setBackground(black);
container.add(mainTextPanel);
//Main Text
mainTextArea = new JTextArea("You have reached the Cave of Adventures;");
mainTextArea.setBounds(100, 100, 600, 250);
mainTextArea.setBackground(black);
mainTextArea.setForeground(white);
mainTextArea.setFont(normalFont);
mainTextArea.setLineWrap(true);
mainTextPanel.add(mainTextArea);
}
public class TitleScreenHandler implements ActionListener {
public void actionPerformed(ActionEvent e){
gotoMainGameScreen();
}
}
public class ChoiceHandler implements ActionListener{
public void actionPerformed(ActionEvent event){
}
}
}
答案 0 :(得分:0)
您正在尝试对尚未初始化的titleNamePanel
进行操作(默认情况下当前设置为null
)。
你需要这样做:
titleNamePanel = new JPanel();
...就在setBounds
来电