1

私はプロジェクトの GUI に取り組んできましたが、これまでのところ、JFrame がまったく表示されないようです。これが私のコードです。

package code;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class GUI extends JFrame {
    private JPanel ui, board, u1, u2, game, main;
    private JTextField console;
    private int x, y;

    public GUI (Controller c) {
        setSize(new Dimension(900,710));
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        //console.setText("Hello, and welcome to the game of Lotus!");
        main = new JPanel(new BorderLayout());
        game = new JPanel(new BorderLayout());
        board = new BoardPanel(c);
        ui = new JPanel (new GridLayout(1,2));;
        u1 = new JPanel (new FlowLayout());
        u2 = new StackPanel(c);
        board = new JPanel();

        createAndShowGUI();
        add(main);
        setVisible(true);
    }

    public GUI () {
        setSize(new Dimension(900,710));
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        //console.setText("Hello, and welcome to the game of Lotus!");
        main = new JPanel(new BorderLayout());
        game = new JPanel(new BorderLayout());
        board = new BoardPanel();
        ui = new JPanel (new GridLayout(1,2));;
        u1 = new JPanel (new FlowLayout());
        u2 = new StackPanel();
        board = new JPanel();


        createAndShowGUI();
        add(main);
        setVisible(true);
        printToConsole("Yes, it's working!");
    }

    public void createAndShowGUI() {
        //add components to ui
        u1.setSize(200,300);
        u2.setSize(200,400);
        ui.add(u1);
        ui.add(u2);

        //add components to game
        board.setSize(700,700);
        ui.setSize(200,700);
        game.add(board, BorderLayout.CENTER);
        game.add(ui, BorderLayout.EAST);


        //add main frame components to gui
        main.add(game, BorderLayout.CENTER);
        main.add(console, BorderLayout.SOUTH);
    }

    public void update () {
        repaint();
    }

    public void printToConsole (String s) {
        console.setText(s);
    }

}

これを実行するたびに、 NullPointerException が発生します

main.add(コンソール、BorderLayout.SOUTH);

その行をコメントアウトすると、エラーなしで実行されますが、表示されるのは巨大な空白の白いボックスだけです。

誰でも助けてもらえますか?

4

2 に答える 2