我正在创建一个加载按钮,并希望它填充嵌套在边框布局(主窗口)的 CENTER 面板内的 9x9 网格布局内容窗格。此方法位于边框布局的 PAGE_START 部分内。问题是如何从这里将按钮放置在 CENTER 部分的网格布局中?
//Load Button
JButton load = new JButton("Load");
load.addActionListener(new ActionListener() {
@Override
public void actionPerformed (ActionEvent a) {
//Dialog Box To Locate The Puzzle
SudokuPuzzle puzzle;
JFileChooser chooser = new JFileChooser();
FileNameExtensionFilter filter = new FileNameExtensionFilter(
"Text Files", "txt");
chooser.setFileFilter(filter);
int returnVal = chooser.showOpenDialog(SudukoGUI.this);
if(returnVal == JFileChooser.APPROVE_OPTION) {
String fileLocation = chooser.getSelectedFile().getName();
Scanner file = new Scanner(fileLocation);
puzzle = new SudokuPuzzle(file, file);
//Load Puzzle To Model and draw it
try {
gridView = new JButton[9][9];
int[][] viewArray = puzzle.getArray();
for (int row = 0; row < 9; row++) {
for (int col = 0; col < 9; col++) {
gridView[row][col] = new JButton(String.valueOf(viewArray[row][col]));
***************THE NEXT LINE REPRESENTS MY PROBLEM***************
this.getContentPane().board.add(gridView[row][col]);
}
}
这是在构造函数中
JPanel board = new JPanel();
board.setLayout (new GridLayout (9,9));
board.setPreferredSize(new Dimension(400,400));
this.getContentPane().add(board, BorderLayout.CENTER);