我一直在为一个项目开发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);
}
}
每当我运行它时,我在
处得到一个NullPointerExceptionmain.add(console,BorderLayout.SOUTH);
如果我注释掉那一行,它就没有错误地运行,但所有显示的都是一个巨大的空白框。
有人可以帮忙吗?
答案 0 :(得分:1)
您尚未初始化console
正在抛出NullPointerException
console = new JTextField("Some Name");
答案 1 :(得分:0)
console
未在任何地方实例化。除此之外,你所拥有的只有JPanel
s,所以没有任何东西可以显示为JPanel
本身并没有提供太多的视觉反馈。