2013-03-24 3 views
0

로드 버튼을 만들고 테두리 레이아웃의 CENTER 패널 (기본 윈도우) 안에 중첩 된 9x9 크기의 그리드 레이 아웃 내용 창을 채울 수 있습니다. 이 메소드는 테두리 레이아웃의 PAGE_START 섹션 안에 있습니다. 질문은 어떻게 여기에서 CENTER 섹션의 Grid Layout에 버튼을 배치 할 수 있습니까?actionPerformed 메서드에서 contentPane을 참조합니다.

final JPanel board = new JPanel(); 

가 그래서 boardActionListener의 익명의 내부 클래스에서 액세스 할 수 있습니다 :

 //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); 
+0

'보드'란 무엇입니까? –

+0

BorderLayout의 CENTER 내부에 생성 된 JPanel의 이름입니다. – metaDNA

+0

'보드'는 ur 클래스의 멤버 변수입니까? 전체 코드를 입력하면 정확한 대답을 제안 할 수 있습니다. –

답변

0

this.getContentPane().board.add(gridView[row][col]); 


사람 : 그 후,이 줄을 변경합니다. 생성자의 콘텐츠 창에 board을 추가했으나 이것이 이제 board이 콘텐츠 창 필드임을 의미하지는 않습니다. 따라서

getContentPane().board 

컴파일에 실패해야합니다.

board을 생성자의 로컬 변수로 선언하는 대신 클래스의 필드로 설정해야합니다. 그런 다음 학급 전체에서 board을 참조 할 수 있습니다.

기본 예 : 당신은 아마 어떤 시점에서 당신의 프레임에 load을 추가 할

public class Example 
{ 
    private JPanel board; 

    public Example() 
    { 
     board = new JPanel(); 
     getContentPane().add(board); 
     //assuming above code takes place in the constructor 
     JButton load = new JButton("load"); 
     load.addActionListener(new ActionListener() 
     { 
       public void actionPerformed(ActionEvent e) 
       { 
        ...lots of code 
        //as board is no longer a local variable this will compile 
        board.add(gridView[row][col]); 
       } 
     } 
    } 
} 

참고.

0

먼저 사용 boardfinal 등의 변수를 선언 생성자입니다. 이해가되지 않습니다

this.getContentPane().board.add(gridView[row][col]); 

board.add(gridView[row][col]); 
+0

@MBarnett 업데이트 된 대답을 참조하십시오. –

+0

변경 사항을 적용했지만 코드가 컴파일되지만 어떤 이유로 인해 보드가로드되지 않습니다. 그것을 표시합니까? 이 코드에 다른 문제가 있습니까? – metaDNA

+0

나는이 코드에 어떤 문제도 보이지 않는다. 나는 ur 코드를 본 후에 알게 될 다른 이유가있을 것이다. –