重新绘制/刷新 JPanel 上的 JLabels
我无法在运行时更新 2D 数组中的 JLabels。
我正在开发的程序是四子棋的一个变体。我创建了一个 JLabels 的 2D 数组,它们都默认为包含空白槽图像的 ImageIcon。玩家 1 和 2 选择他们的颜色,轮到玩家时,他可以点击将棋子放入柱子中(重力导致棋子落到底部或直到它落在另一棋子的顶部)。
我非常肯定我的 addToColumn 方法工作正常。我唯一的问题是我似乎无法更新任何 JLabels。这是我正在研究的方法:
p1、p2 和 current 是 Player 对象。 grid[][] 是一个二维整数数组,设置为 0、1 或 2,以便更轻松地跟踪谁拥有哪些图块。 tiles[][] 是我的 JLabels 二维数组。
public void addToColumn(int column) { // drop a tile in the specified column
int i = 0;
while (grid[column][5-i] != 0) i++; // move upward through the 6 rows of tiles
// until we find an empty one
if (current == p1) grid[column][5-i] = 1; // update to the current player's value
else grid[column][5-i] = 2;
tiles[column][5-i] = new JLabel(findColorIcon(current.getColor()));
tiles[column][5-i].setIcon(findColorIcon(current.getColor()));
repaint();
现在最后两行更改了tiles[][]中的JLabel,显然我不需要两者,不确定哪种方法更好......这只是我尝试过的一些方法,但无济于事。 (我的 getColor() 方法返回一个 Color,findColorIcon(Color c) 返回具有该图块颜色的相应 JLabel)。
是的,我也在我的paintComponent方法中添加了:
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
}
我已经坚持了一段时间了,我觉得我错过了一些明显的东西。有什么建议吗?
I'm having trouble getting my JLabels in a 2D array to update during runtime.
The program I'm working on is a variant of Connect Four. I create a 2D array of JLabels, which all default to an ImageIcon containing an image of a blank slot. Players 1 and 2 choose their colors, and on a player's turn, he can click to drop a piece into a column (gravity causes the piece to fall to the bottom or until it lands atop another piece).
I'm pretty positive that my addToColumn method is working fine. My only problem is that I can't seem to get any of the JLabels to update. Here's the method I'm working on:
p1, p2, and current are Player objects. grid[][] is a 2D array of integers set to 0, 1, or 2 to more easily track who owns which tiles. tiles[][] is my 2D array of JLabels.
public void addToColumn(int column) { // drop a tile in the specified column
int i = 0;
while (grid[column][5-i] != 0) i++; // move upward through the 6 rows of tiles
// until we find an empty one
if (current == p1) grid[column][5-i] = 1; // update to the current player's value
else grid[column][5-i] = 2;
tiles[column][5-i] = new JLabel(findColorIcon(current.getColor()));
tiles[column][5-i].setIcon(findColorIcon(current.getColor()));
repaint();
now with those last two lines changing the JLabel in tiles[][], obviously I don't need both, not sure which way is better... that's just some of what I've tried, to no avail. (my getColor() method returns a Color, and findColorIcon(Color c) returns the corresponding JLabel with that color of tile).
and yes, I have added in my paintComponent method too:
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
}
I've been stuck on this for a while now, and I feel like I'm missing something obvious. any suggestions?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我没有看到您的
paintComponent()
方法执行任何操作。特别是,替换JLabel
需要您validate()
容器。作为替代方案,您可能想了解这个简单的游戏如何使用模型–视图–控制器 模式并绘制彩色图标。附录:此相关示例描述了如何仅替换
Icon
,而不是整个JLabel
。相比之下,此示例展示了如何validate()
更换组件后的容器。I don't see that your
paintComponent()
method does anything. In particular, replacing aJLabel
requires that youvalidate()
the container. As an alternative, you might like to see how this simple game uses the Model–View–Controller pattern and draws colored icons.Addendum: This related example describes how to replace just the
Icon
, rather than the entireJLabel
. In contrast, this example shows how tovalidate()
a container after replacing components.