我想修一个游戏的2D板。我已经为Gui安装了其他面板,一切顺利。但是电路板的面板不能印在窗户上。我对它有点困惑,因为我认为我的想法与我需要的其他面板一样。
这就是我所做的:
修改: 我正在尝试做的是根据游戏的尺寸修复游戏的棋盘面板,将每个正方形保持在一个阵列中以便在任何地方使用它之后需要它。我用方法绘制绘制每个小方块并将其放回面板。因此,电路板上的每个方块都是一个面板。这是个主意。但是你可以看到。它有麻烦/错误。
编辑:代码已更新。 刚发现问题的一部分。我首先想到我已经设定了平方背景,但我没有。在这个面板上出现一个宽黑色的“列”。不幸的是,仍然没有正方形:(
再一次编辑: 此外,我意识到绘制方法永远不会被调用。当我把绘制方法放在下面的方法中时,我可以看到正方形,但它们仍然很小。我用setSize重新定义它们但仍然没有变化。
如何使用paint方法正确编辑面板????因为现在它不能。即使它不能返回一个对象(例如面板),因为它的多态性无效!
/**
*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);
}
答案 0 :(得分:2)
我看到一些有问题的事情: