本文介绍了java Netbeans IDE - 叠加图像(棋盘 - 棋子)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用NetBeans IDE为GUI制作国际象棋游戏,但我不能让棋子停留在棋盘上方,当我尝试棋盘被推开时。我正在为两个图像使用标签。如何在不使棋盘成为背景的情况下让棋子保持在棋盘上方? (窗口将有棋盘和右侧的信息面板,如计时器和东西,所以我不想让棋盘图像成为整个框架的背景)

I'm trying to make a chess game in java using NetBeans IDE for the GUI, but I can't make a chesspiece stay above the chessboard, when I try the chessboard just gets pushed away. I'm using a label for both images. How can I make the chesspieces stay above the chessboard without making the chessboard a background? (The window will have the chessboard and a information panel to the right like timer and stuff, so i don't want to make the chessboard image a background to the entire frame)

推荐答案

你可以尝试一些事情。

将片标添加到电路板标签上。这将允许您在单个单元格上叠加该块。这将要求您使用单元格的布局管理器,例如 GridBagLayout 可能会这样做,因为它会自动将组件置于其父级空间中。

add the piece label to the board label. This would allow you to "overlay" the piece over the individual cells. This would require you to use a layout manager for the cell, something like a GridBagLayout might do, as it automatically centers the components within it's parent's space.

使用 JLayeredPane 使用像 GridBagLayout ,允许你将一个面板放在另一个面板之上。

Use a JLayeredPane using something like a GridBagLayout, would allow you to over one panel on top of another.

底层将是 JPanel ,其中包含电路板的每个单元格,顶层将是透明的 JPanel ,其中包含所有部分

The bottom layer would be a JPanel, which contained each cell of the board and the top layer would be a transparent JPanel which contained all the pieces

这变得更加复杂,因为您需要设计一些方法来填充件图层的空白区域。

This becomes more complicated as you'd need to devise some way to fill the empty spaces of the "pieces" layer.

根据您的需要,两个图层都将使用 GridBagLayout GridLayout ...

Both layers would use either a GridBagLayout or GridLayout depending on your needs...

使用一个容器和一个 GridBagLayout 。这将允许您同时向布局中的同一(虚拟)单元格添加两个组件。

Use a single container and a GridBagLayout. This would allow you to add two components to the same (virtual) cell within the layout at the same time.

这变得有点棘手,因为您需要管理z-组件的深度,以确保这些部件布置在板的顶部。

This becomes a little tricky, as you need to manage the z-depthness of the components to ensure that the pieces are laid out on top of the board.

这篇关于java Netbeans IDE - 叠加图像(棋盘 - 棋子)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-26 00:27