로드 버튼을 만들고 테두리 레이아웃의 CENTER 패널 (기본 윈도우) 안에 중첩 된 9x9 크기의 그리드 레이 아웃 내용 창을 채울 수 있습니다. 이 메소드는 테두리 레이아웃의 PAGE_START 섹션 안에 있습니다. 질문은 어떻게 여기에서 CENTER 섹션의 Grid Layout에 버튼을 배치 할 수 있습니까?actionPerformed 메서드에서 contentPane을 참조합니다.
final JPanel board = new JPanel();
가 그래서 board
이 ActionListener
의 익명의 내부 클래스에서 액세스 할 수 있습니다 :
//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);
'보드'란 무엇입니까? –
BorderLayout의 CENTER 내부에 생성 된 JPanel의 이름입니다. – metaDNA
'보드'는 ur 클래스의 멤버 변수입니까? 전체 코드를 입력하면 정확한 대답을 제안 할 수 있습니다. –