【问题标题】:Add Same Array Of Buttons To Two JFrame's将相同的按钮数组添加到两个 JFrame
【发布时间】:2016-07-12 14:35:43
【问题描述】:

我正在尝试将一组按钮添加到两个 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);
    }
}

如何在两个不同的 JFrame 中显示相同的数组?

【问题讨论】:

标签: java arrays swing jframe jbutton


【解决方案1】:

来自Java Tutorial

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

注意:我将您的 ReversiButtons 更改为简单的 JButtons,以便代码可以自行编译。

【讨论】:

    【解决方案2】:

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

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

    【讨论】:

      【解决方案3】:

      检查Container#add调用的Javadoc for Container#addImpl

      如果组件不是此容器的祖先并且具有 a non-null parent在它被删除之前从它的当前父级中移除 已添加到此容器中。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2018-03-19
        • 1970-01-01
        • 2020-06-02
        • 1970-01-01
        • 2013-09-09
        • 2018-10-01
        • 2013-12-09
        相关资源
        最近更新 更多