Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/entity-framework/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 将相同的按钮数组添加到两个JFrame';s_Java_Arrays_Swing_Jframe_Jbutton - Fatal编程技术网

Java 将相同的按钮数组添加到两个JFrame';s

Java 将相同的按钮数组添加到两个JFrame';s,java,arrays,swing,jframe,jbutton,Java,Arrays,Swing,Jframe,Jbutton,我试图将一组按钮添加到两个JFrame中以形成一个网格,但当我尝试这样做时,第一个JFrame没有按钮,但第二次添加按钮时,所有按钮都在那里 我的代码是 public class GUI { ReversiButton[][] reversi = new ReversiButton[8][8]; JFrame WhiteFrame = new JFrame(); JFrame BlackFrame = new JFrame(); JLabel WhiteLabe

我试图将一组按钮添加到两个JFrame中以形成一个网格,但当我尝试这样做时,第一个JFrame没有按钮,但第二次添加按钮时,所有按钮都在那里

我的代码是

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);
    }
}
公共类GUI
{
ReversiButton[]reversi=新的ReversiButton[8][8];
JFrame WhiteFrame=新JFrame();
JFrame BlackFrame=新JFrame();
JLabel WhiteLabel=新的JLabel();
JLabel BlackLabel=新的JLabel();
JPanel BlackGrid=newjpanel();
JPanel-WhiteGrid=新的JPanel();
JButton WhiteButton=新JButton();
JButton BlackButton=新JButton();
公共图形用户界面()
{
populateArray();
initGUI();
}
私人空房
{
对于(int y=0;y检查调用方
容器#add

如果组件不是此容器的祖先并且具有 非空父项在删除之前,它将从当前父项中删除 已添加到此容器。

检查被调用的
容器#add

如果组件不是此容器的祖先并且具有 非空父项在删除之前,它将从当前父项中删除 已添加到此容器。


您需要有两组数组,每个JFrame一组


组件实例只能添加到一个容器中。在您的代码中,您将所有按钮都放在第一个JFrame中,但当您将它们添加到第二个JFrame中时,您也将从第一个JFrame中删除它们。

您需要有两组数组,每个JFrame对应一组

组件实例只能添加到一个容器中。在您的代码中,您将所有按钮都放在第一个JFrame中,但当您将它们添加到第二个JFrame中时,您也将它们从第一个JFrame中删除。

每个GUI组件只能包含一次。如果某个组件已在容器中,并且您尝试将其添加到另一个容器中,则该组件将从第一个容器中删除,然后添加到第二个容器中

因此,要解决您的问题,您需要创建两个独立的按钮阵列;请记住,现在当一个玩家移动时,您必须将其应用于两个网格:

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]);
        }
    }
}
private static final int GRID_WIDTH=8;
专用静态最终整型网格高度=8;
JFrame whiteFrame=新JFrame();
JFrame blackFrame=新JFrame();
JPanel blackGrid=newjpanel();
JPanel-whiteGrid=新的JPanel();
JButton[]whiteTiles=新JButton[网格宽度][网格高度];
JButton[][]blackTiles=新JButton[网格宽度][网格高度];
JButton whiteAIButton=新JButton();
JButton blackAIButton=新JButton();
JLabel whiteLabel=新的JLabel();
JLabel blackLabel=新的JLabel();
公共图形用户界面(){
平民区(白色瓷砖);
平民区(黑瓦);
initGUI();
}
私有void populateArray(JButton[][]btnArray){
对于(int x=0;x
注意:我将您的
ReversiButton
s更改为简单的
JButton
s,以便代码可以自行编译。

由此:

每个GUI组件只能包含一次。如果某个组件已在容器中,并且您尝试将其添加到另一个容器中,则该组件将从第一个容器中删除,然后添加到第二个容器中

因此,要解决您的问题,您需要创建两个独立的按钮阵列;请记住,现在当一个玩家移动时,您必须将其应用于两个网格:

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]);
        }
    }
}
private static final int GRID_WIDTH=8;
专用静态最终整型网格高度=8;
JFrame whiteFrame=新JFrame();
JFrame blackFrame=新JFrame();
JPanel blackGrid=newjpanel();
JPanel-whiteGrid=新的JPanel();
JButton[]whiteTiles=新JButton[网格宽度][网格高度];
JButton[][]blackTiles=新JButton[网格宽度][网格高度];
JButton whiteAIButton=新JButton();
JButton blackAIButton=新JButton();
JLabel whiteLabel=新的JLabel();
JLabel blackLabel=新的JLabel();
公共图形用户界面(){
平民区(白色瓷砖);
平民区(黑瓦);
initGUI();
}
私有void populateArray(JButton[][]btnArray){
对于(int x=0;x