gpt4 book ai didi

java - 如何在 Java 中表示游戏的 Board Panel?

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

我想为游戏修复 2D 棋盘。我已经为 Gui 修复了其他面板,一切都很顺利。但电路板的面板不能打印在窗口上。我对此有点困惑,因为我认为我遵循了与我需要的其他面板相同的想法。

这是我所做的:

编辑:我想做的是根据游戏的尺寸修复游戏的棋盘面板,将每个方 block 保存在数组中,以便在需要时使用它。我用绘制方法绘制它的每个小方 block 并将其放回面板。因此,棋盘上的每个方 block 都是一个面板。这就是想法。但正如你所看到的。上面有麻烦/错误。

编辑:代码已更新。刚刚发现了问题的一部分。我首先以为我已将背景设置为平方,但我没有。有了这个,面板上就会出现一个宽的黑色“柱”。不幸的是,仍然没有正方形。 :(

再编辑一下:另外,我意识到绘图方法从未被调用。当我将绘制方法放入以下方法中时,我可以看到正方形,但它们仍然很小。我用 setSize 重新定义了它们,但仍然没有改变。

如何使用paint方法正确编辑面板???现在的情况是不能的。即使它不能返回对象(例如面板),因为它是多态 void!

/**
*Method used to construct the square in the area of the
*gui's grid. In this stage a GUISquare array is being constructed,
* used in the whole game as
*a mean of changing a square graphical state.
*@param squares is the squares array from whom the gui grid will be
*constructed.
*@see getSquare about the correspondance beetween a squareModel and
* a GUISquare.
*/
private void initBoardPanel(SquareModel[][] squares){

BoardPanel.setLayout(new GridLayout(height ,width )); //set layout

SquareRenderer[][] Squares;
JPanel[][] grid;
Squares=new GUISquare[height][width()];
grid=new JPanel[height()][width()];

for (int i=0; i<height(); i++){
for (int j=0; j<width() ; j++){
SquareRenderer kou=new SquareRenderer(i,j);
kou.setSquare(myGame.getSquares()[i][j]);
//NOTE: THE FOLLOWING DRAW METHOD CANT BE CALLED!!!?
if (myGame.getSquares()[i][j] instanceof SimpleSq ){
kou .paintPanel(i,j,"");}
else if (myGame.getSquares()[i][j] instanceof ActionSq )
{ kou .paintPanel(i,j);
}
//JUST BECAUSE DRAW CANT BE CALLED I PUT ITS CODE HERE:
//JUST TO CHECK:
JPanel panel = new JPanel();
panel.setLayout(new BorderLayout());
JLabel label1 = new JLabel("Move To "+myGame.getSquares()[i][j].getGoTo());
JLabel label2 = new JLabel(""+myGame.getSquares()[i][j].getSquare());

panel.setBackground(Color.ORANGE);
panel.add(label2, BorderLayout.NORTH);
panel.add(label1, BorderLayout.CENTER);
panel.setSize(250,250);

///////// <--until here ---paint method<---

kou.add(panel);
kou.setVisible(true);
kou.setBackground(Color.BLACK);

Squares[i][j]= kou;

BoardPanel.add(kou);
BoardPanel.setVisible(true);
BoardPanel.setBackground(Color.WHITE);
}
}
this.add(BoardPanel,BorderLayout.WEST);
// this.pack(); //sets appropriate size for frame
this.setVisible(true); //makes frame visible
}

由 SQUARERENDERER 实现:

/**
* Transformer for Snake/Ladder
* <br>This method is used to display a square on the screen.
*/
public void paintPanel(int i,int j) {
JPanel panel = new JPanel();
panel.setLayout(new BorderLayout());
JLabel label1 = new JLabel("Move To"+myGame.getSquares()[i][j].getGoTo());
JLabel label2 = new JLabel(""+myGame.getSquares()[i][j].getSquare());
JSeparator CellSeparator = new JSeparator(orientation);
panel.add(CellSeparator);
panel.setForeground(Color.ORANGE);
panel.add(label2, BorderLayout.NORTH);
panel.add(label1, BorderLayout.CENTER);
}

最佳答案

我发现有一些问题:

  1. 使用布局管理器时,应避免调用 setSize。大多数布局管理器都会简单地覆盖它。您应该使用 setPreferredSize、setMinimumSize 和 setMaximumSize 来代替,它们为布局管理器提供提示。
  2. SquareRenderer 的绘制方法似乎从未将您创建的面板添加到任何内容中。您应该在绘制的最后一行中将面板添加到 SquareRenderer,或者(更好)将子组件直接添加到 SquareRenderer。

关于java - 如何在 Java 中表示游戏的 Board Panel?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4694764/

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