Java-JPanel设计问题

时间:2016-08-15 16:14:03

标签: java jpanel jbutton

更新:NVM IM IDI IDIOT PASSING NULL

嗯,所以我最近了解了面板,我正在尝试重新设计一个tictactoe项目的开始菜单,没什么大不了的。

这是我的梦想:

enter image description here

这是我目前的代码:

public static void modeGUI () {

    //MAIN JFRAME GUI
    JFrame gui = new JFrame("TicTacToe");
    gui.setVisible(true);
    gui.setSize(600, 200);
    gui.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    //MAIN PANEL
    JPanel mainPanel = new JPanel();
    mainPanel.setLayout(new GridLayout(3, 1));
    gui.add(mainPanel);

    //QUESTION PANEL
    JPanel questionPanel = new JPanel();
    questionPanel.setLayout(new FlowLayout());
    JLabel q1 = new JLabel("Please select your mode");
    q1.setFont(new Font("Times New Roman", Font.PLAIN, 20));
    questionPanel.add(q1);
    mainPanel.add(questionPanel);

    //MODE PANEL
    JPanel  modePanel = new JPanel();
    modePanel.setLayout(new GridLayout());
    JButton[] modeButtons = new JButton[3];
    modeButtons[0].setText("First turn");
    modeButtons[1].setText("Second turn");
    modeButtons[2].setText("P vs. P");
    for (JButton modeButton : modeButtons) {
        modeButton.setFont(new Font("Times New Roman", Font.PLAIN, 20));
        modeButton.addActionListener(new Response());
        modePanel.add(modeButton);
    }
    mainPanel.add(modePanel);

    //OPTION PANEL
    JPanel optionsPanel = new JPanel();
    optionsPanel.setLayout(new FlowLayout());
    mainPanel.add(optionsPanel);
}

但是,当我尝试运行它时,它会在第30行给出错误,该错误与此行相关:modeButtons[0].setText("First turn");。任何人都可以帮忙看看有什么问题吗?

这是错误btw:线程“main”中的异常java.lang.NullPointerException     在Ttt.modeGUI(Ttt.java:30)     在Ttt.main(Ttt.java:7)

0 个答案:

没有答案