JFrame显示了一个巨大的白框



我一直在为一个项目开发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

主要。添加(控制台,BorderLayout.SOUTH);

如果我注释掉这行,它运行没有错误,但所有显示的是一个巨大的空白的白色框。

有人能帮忙吗?

您还没有初始化抛出NullPointerException的console

 console = new JTextField("Some Name");

没有在任何地方实例化console。除此之外,你所拥有的都是JPanel s,所以没有什么可以显示的,因为JPanel本身并没有提供太多的视觉反馈。

相关内容

最新更新