I am trying to add a array of buttons to two JFrame's to form a grid but when I try to do so the first JFrame has no buttons but the second time that i add the Buttons all the buttons are there.
My code is
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);
}
}
How can I show the same array in two different JFrames?