我将一个JTextField的3D数组添加到JFrame中的JPanel,然后什么都没有显示出来

时间:2015-05-24 13:49:16

标签: java arrays swing jpanel layout-manager

在我添加 3D数组的文本字段之前它正在工作,然后我创建的所有内容都没有显示。删除它们后,仍然没有显示任何内容。如何在不破坏整个布局的情况下添加JTextField的3D数组?

我先把它们初始化了:

int numOfPuzzles;
int puzzleSize;
JTabbedPane tabbedPane;
JPanel[] southPanel;
JPanel[] centerPanel;
JPanel[] sudokuPanel;
JPanel[] buttonsPanel;
JButton[] nextButton;
JButton[] prevButton;
ArrayList<int[][]> sudoku;
JTextField[][][] textFields;

public SudokuSolverUI(ArrayList<int[][]> sudoku){
    this.sudoku = sudoku;
    this.numOfPuzzles = numOfPuzzles;
    this.puzzleSize = puzzleSize;
    this.setTitle("Sudoku XY");
    this.setVisible(true);
    this.setSize(900, 700);
    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    this.setComponents();
}

然后在public void setComponents我实例化了必要的组件,将它们添加到面板中,然后:

public void setComponents(){
    this.tabbedPane = new JTabbedPane();
    centerPanel = new JPanel[this.numOfPuzzles];
    sudokuPanel = new JPanel[this.numOfPuzzles];
    buttonsPanel = new JPanel[this.numOfPuzzles];
    southPanel = new JPanel[this.numOfPuzzles];
    nextButton = new JButton[this.numOfPuzzles];
    prevButton = new JButton[this.numOfPuzzles];
    for(int i=0;i<this.numOfPuzzles;i++){
        nextButton[i] = new JButton("Next");
        nextButton[i].addActionListener(this);
        prevButton[i] = new JButton("Prev");
        prevButton[i].addActionListener(this);
        centerPanel[i] = new JPanel();
        sudokuPanel[i] = new JPanel();
        buttonsPanel[i] = new JPanel();
        southPanel[i] = new JPanel();

        for(int j=0;j<this.sudoku.get(i).length;j++){
            for(int k=0;k<this.sudoku.get(i).length;k++){
                textFields[i][j][k] tf = new JTextField();
                sudokuPanel[i].add(tf);
            }
        }

        southPanel[i].setLayout(new FlowLayout());
        southPanel[i].add(prevButton[i]);
        southPanel[i].add(nextButton[i]);
        centerPanel[i].setLayout(new BorderLayout());
        centerPanel[i].add(southPanel[i], BorderLayout.SOUTH);
        centerPanel[i].add(sudokuPanel[i], BorderLayout.CENTER);
        centerPanel[i].add(buttonsPanel[i], BorderLayout.EAST);
        centerPanel[i].setVisible(true);
        centerPanel[i].validate();
        tabbedPane.addTab("Puzzle #"+(i+1), centerPanel[i]);
        this.getContentPane().add(tabbedPane);
    }
}
什么都没有显示出来。请帮忙。

1 个答案:

答案 0 :(得分:0)

如果这就是全部,您将获得一个NullPointerException,您应该可以在控制台上看到它。您没有初始化numOfPuzzle,但在setComponents中将其用作索引。