gpt4 book ai didi

java - 将相同的按钮数组添加到两个 JFrame

转载 作者:行者123 更新时间:2023-12-01 07:51:44 25 4
gpt4 key购买 nike

我试图将按钮数组添加到两个 JFrame 以形成网格,但是当我尝试这样做时,第一个 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);
}
}

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

最佳答案

从此Java Tutorial :

Each GUI component can be contained only once. If a component is already in a container and you try to add it to another container, the component will be removed from the first container and then added to the second.

因此,要解决您的问题,您需要创建两个单独的按钮数组;请记住,现在当一名玩家采取行动时,您必须将其应用到两个网格:

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

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

关于java - 将相同的按钮数组添加到两个 JFrame,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36209752/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com