0

ロードボタンを作成していて、ボーダーレイアウト(メインウィンドウ)の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);
4

2 に答える 2

0

まず、以下を使用して変数として宣言boardします。final

final JPanel board = new JPanel();

したがって、boardの匿名内部クラスからアクセスできますActionListener。その後、次の行を変更します。

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


board.add(gridView[row][col]);
于 2013-03-24T20:43:33.877 に答える
0

これ

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

意味がありません。コンストラクターでコンテンツペインに追加しましたが、これはコンテンツペインのフィールドになったboardことを意味するものではありません。boardそれで

getContentPane().board

コンパイルに失敗するはずです。

boardコンストラクターでローカル変数として宣言するのではなく、クラスのフィールドを作成する必要があります。boardそうすれば、クラス全体を参照できるようになります。

基本的な例:

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]);
              }
         }
    }
}

loadおそらく、ある時点でフレームに追加したいことに注意してください。

于 2013-03-24T21:08:17.577 に答える