2016-03-24 15 views
0

2つのJFrameにグリッドを追加しようとしていますが、最初のJFrameにはボタンがありませんが、2回目にボタンを追加しますボタンはそこにあります。ボタンの配列を2つのJFrameに追加する

enter image description here

私のコード

public class GUI 
{ 
    ReversiButton[][] reversi = new ReversiButton[8][8]; 
    JFrame WhiteFrame = new JFrame(); 
    JFrame BlackFrame = new JFrame(); 
    JLabel WhiteLabel = new JLabel(); 
    JLabel BlackLabel = new JLabel(); 
    JPanel BlackGrid = new JPanel(); 
    JPanel WhiteGrid = new JPanel(); 
    JButton WhiteButton = new JButton(); 
    JButton BlackButton = new JButton(); 
    public GUI() 
    { 
     populateArray(); 
     initGUI(); 
    } 
    private void populateArray() 
    { 
     for(int y = 0;y<8;y++) 
     { 
      for(int x = 0; x<8;x++) 
      { 
       reversi[x][y] = new ReversiButton(); 
      } 
     } 
    } 
    private void initGUI() 
    { 
     WhiteFrame.setTitle("Reversi White Player"); 
     BlackFrame.setTitle("Reversi Black Player"); 
     WhiteFrame.setLayout(new BorderLayout()); 
     WhiteLabel.setText("White Player - click place to put piece"); 
     WhiteGrid.setLayout(new GridLayout(8,8)); 
     for(int wy = 0;wy<8;wy++) 
     { 
      for(int wx = 0; wx<8;wx++) 
      { 
       WhiteGrid.add(reversi[wx][wy]); 
      } 
     } 
     WhiteButton.setText("Greedy AI(play white)"); 
     WhiteFrame.add(BorderLayout.NORTH,WhiteLabel); 
     WhiteFrame.add(BorderLayout.CENTER,WhiteGrid); 
     WhiteFrame.add(BorderLayout.SOUTH,WhiteButton); 
     WhiteFrame.pack(); 
     WhiteFrame.setVisible(true); 
     BlackFrame.setLayout(new BorderLayout()); 
     BlackLabel.setText("Black player - not your turn"); 
     BlackGrid.setLayout(new GridLayout(8,8)); 
     for(int y = 0; y<8; y++) 
     { 
      for(int x = 0; x<8; x++) 
      { 
       BlackGrid.add(reversi[x][y]); 
      } 
     } 
     BlackButton.setText("Greedy AI(play black)"); 
     BlackFrame.add(BorderLayout.NORTH, BlackLabel); 
     BlackFrame.add(BorderLayout.CENTER, BlackGrid); 
     BlackFrame.add(BorderLayout.SOUTH,BlackButton); 
     BlackFrame.pack(); 
     BlackFrame.setVisible(true); 
    } 
} 

は、どのように私は2つの異なるJFramesで同じ配列を示すことができていますか?

+1

関連:http://stackoverflow.com/a/20167550/5050667 –

+1

[複数のJFramesの使用、良い/悪い練習?](http://stackoverflow.com/q/9554636/418556)を参照してください。 –

答えて

2

各GUIコンポーネントは一度だけ含有させることができます。コンポーネントが既にコンテナ内にあり、それを別のコンテナに追加しようとすると、そのコンポーネントは最初のコンテナから削除され、次に2番目のコンテナに追加されます。

そのため、問題を解決するには、2つの独立したボタン配列を作成する必要があります。一人のプレイヤーが移動を行うと、今、あなたは両方のグリッドにそれを適用する必要があります念頭に置いて維持:

private static final int GRID_WIDTH = 8; 
private static final int GRID_HEIGHT = 8; 

JFrame whiteFrame = new JFrame(); 
JFrame blackFrame = new JFrame(); 

JPanel blackGrid = new JPanel(); 
JPanel whiteGrid = new JPanel(); 

JButton[][] whiteTiles = new JButton[GRID_WIDTH][GRID_HEIGHT]; 
JButton[][] blackTiles = new JButton[GRID_WIDTH][GRID_HEIGHT]; 
JButton whiteAIButton = new JButton(); 
JButton blackAIButton = new JButton(); 

JLabel whiteLabel = new JLabel(); 
JLabel blackLabel = new JLabel(); 

public GUI() { 
    populateArray(whiteTiles); 
    populateArray(blackTiles); 

    initGUI(); 
} 

private void populateArray(JButton[][] btnArray) { 
    for (int x = 0; x < btnArray.length; x++) { 
     for (int y = 0; y < btnArray[0].length; y++) { 
      btnArray[x][y] = new JButton(); 
     } 
    } 
} 

private void initGUI() { 
    whiteAIButton.setText("Greedy AI (play white)"); 
    whiteLabel.setText("White Player - click place to put piece"); 
    fillGrid(whiteGrid, whiteTiles); 

    whiteFrame.setLayout(new BorderLayout()); 
    whiteFrame.setTitle("Reversi White Player"); 
    whiteFrame.add(BorderLayout.NORTH, whiteLabel); 
    whiteFrame.add(BorderLayout.CENTER, whiteGrid); 
    whiteFrame.add(BorderLayout.SOUTH, whiteAIButton); 
    whiteFrame.pack(); 
    whiteFrame.setVisible(true); 

    blackAIButton.setText("Greedy AI (play black)"); 
    blackLabel.setText("Black player - not your turn"); 
    fillGrid(blackGrid, blackTiles); 

    blackFrame.setTitle("Reversi Black Player"); 
    blackFrame.setLayout(new BorderLayout()); 
    blackFrame.add(BorderLayout.NORTH, BlackLabel); 
    blackFrame.add(BorderLayout.CENTER, blackGrid); 
    blackFrame.add(BorderLayout.SOUTH, blackAIButton); 
    blackFrame.pack(); 
    blackFrame.setVisible(true); 
} 

private void fillGrid(JPanel grid, JButton[][] tiles) { 
    grid.setLayout(new GridLayout(GRID_WIDTH, GRID_HEIGHT)); 

    for (int x = 0; x < GRID_WIDTH; x++) { 
     for (int y = 0; y < GRID_HEIGHT; y++) { 
      grid.add(tiles[x][y]); 
     } 
    } 
} 

注:コードは、独自にコンパイルなるように、私は単純にJButton秒にごReversiButton秒を変更しました。

2

コンポーネントがこのコンテナの祖先ではなく、 null以外の親を持っている場合、それは追加 である前に、それが現在の親から削除されContainer#add

によって呼び出さJavadoc for Container#addImplをチェックこの容器に

2

各JFrameに1つずつ、2セットの配列が必要です。

コンポーネントのインスタンスは、1つのコンテナにしか追加できません。コードでは、すべてのボタンを最初のJFrameに配置しますが、2番目のJFrameに追加する場合は、最初のJFrameからも削除します。このJava Tutorialから

関連する問題