gpt4 book ai didi

java - 从 jpanel 中绘制和删除 jlabel

转载 作者:行者123 更新时间:2023-12-01 14:52:04 26 4
gpt4 key购买 nike

我正在制作一个国际象棋游戏 GUI,方法是设置一个 jpanels 网格,顶部带有 jlabel(棋子图像)。我设置了板子,每个方 block (jpanel)都有一个空的 jlabel,可见性设置为 false。然后,我浏览每个 jpanels,并将 jlabel 图片设置为与我的其中一幅作品相对应的图像。我知道是否要画一 block ,因为我有一个变量 _board[8][8] 告诉我该位置是否被占用以及被谁占用。这就是我创建原始板的方式:

编辑:

我实际上对此做了很大的改变,这样我就不必每次都重新加载图像:

public ChessGameGUI(Square[][] board) throws IOException 
{
Dimension size = new Dimension(500, 400);
Dimension boardSize = new Dimension(400, 400);

layeredPane = new JLayeredPane();
getContentPane().add(layeredPane);
layeredPane.setPreferredSize(size);

theBoard = new JPanel();
layeredPane.add(theBoard, JLayeredPane.DEFAULT_LAYER);
layeredPane.addMouseListener(this);

GridLayout chessBoardLayout = new GridLayout(8,8);
theBoard.setLayout(chessBoardLayout);
theBoard.setPreferredSize(boardSize);
theBoard.setBounds(0, 0, boardSize.width, boardSize.height);

boolean drawWhite = true;

// setup initial squares
for(int y = 0; y < 8; y++)
{
for(int x = 0; x < 8; x++)
{
BorderLayout squareBorder = new BorderLayout();
JPanel aChessSquare = new JPanel(squareBorder);

if(drawWhite) aChessSquare.setBackground(Color.WHITE);
else aChessSquare.setBackground(Color.GRAY);

// load initial images
if(board[x][y]._isOccupied)
{
BufferedImage logo = null;

try{
// load the image
String path = findPieceImagePath(board, x, y);
logo = ImageIO.read(new File(path));
ImageIcon pieceImage = new ImageIcon(logo);

JLabel pieceJLabel = new JLabel();
pieceJLabel.setIcon(pieceImage); // new
pieceJLabel.setVisible(true); // new

aChessSquare.add(pieceJLabel);
}
catch (IOException e)
{
e.printStackTrace();
} // end try catch
}// end if
else
{
JLabel pieceJLabel = new JLabel();
pieceJLabel.setVisible(false);
aChessSquare.add(pieceJLabel);
}

theBoard.add(aChessSquare);

// alternate background colors
if(x == 7);
else drawWhite = !drawWhite;
}
}

System.out.println("the number of components = " + theBoard.getComponentCount());

}

这可以很好地加载初始板,但是我似乎无法获得特定的组件。当我点击其中一 block 时,该方 block 应该亮起。唯一获得绿色轮廓的方 block 是左上角的方 block (0, 0)。

public void mousePressed(MouseEvent e) 
{
System.out.println("mouse press x = " + e.getX() + " y = " + e.getY());

// find which piece on the board is being clicked
int gameX, gameY;
gameX = e.getX() / 50;
gameY = e.getY() / 50;

System.out.println("gameX = " + gameX + " gameY = " + gameY);

// out of bounds
if(gameX < 0 || gameY < 0) return;

// true if selected a piece on this team
if(myGame._board[gameX][gameY]._isOccupied && !_pieceSelected && myGame._board[gameX][gameY]._team == myGame._turn)
{
System.out.println("selected new piece");
JPanel curSquare = (JPanel)theBoard.getComponentAt(gameX, gameY);
curSquare.setBorder(BorderFactory.createLineBorder(Color.green));
_pieceSelected = !_pieceSelected;



return;
}

我想我在这行上遇到了问题:JPanel curSquare = (JPanel)theBoard.getComponentAt(gameX, gameY);

但是,gameX 和 gameY 值已正确设置。

谢谢

最佳答案

我认为您缺少一行来将标签添加到每个面板。在“//设置初始方 block ”期间:

curLabel.setVisible(false);

应该是:

curLabel.setVisible(false);aChessSquare.add(curLabel);

关于java - 从 jpanel 中绘制和删除 jlabel,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14748028/

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