在JPanel上重画/刷新JLabel [英] Repainting/refreshing JLabels on a JPanel

查看:150
本文介绍了在JPanel上重画/刷新JLabel的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法在运行时更新2D数组中的JLabels.

我正在使用的程序是Connect Four的变体.我创建了一个JLabels的2D数组,它们全部默认为一个ImageIcon,其中包含一个空白插槽的图像.玩家1和2选择他们的颜色,然后在玩家的回合上,可以单击将其放入一个列中(重力使该片段下降到底部或直到它落在另一块之上).

我非常肯定我的addToColumn方法可以正常工作.我唯一的问题是,我似乎无法获取任何JLabel进行更新.这是我正在使用的方法:

p1,p2和current是Player对象. grid [] []是设置为0、1或2的2D整数数组,可以更轻松地跟踪谁拥有哪些图块. tile [] []是我的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();

现在,最后两行更改了tile [] []中的JLabel,显然我不需要两者,也不知道哪种方法更好……这只是我尝试过的一部分,无济于事. (我的getColor()方法返回一个Color,而findColorIcon(Color c)返回具有该tile颜色的相应JLabel.)

是的,我也添加了我的paintComponent方法:

@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
}

我已经在这个问题上停留了一段时间了,我觉得我缺少明显的东西.有什么建议吗?

解决方案

我看不到您的paintComponent()方法有任何作用.特别是,替换JLabel要求您validate()容器.作为替代方案,您可能想看看这个简单的游戏如何使用示例描述了如何仅替换Icon而不是整个JLabel.与此相反,此示例显示了如何在替换组件后validate()容器. /p>

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?

解决方案

I don't see that your paintComponent() method does anything. In particular, replacing a JLabel requires that you validate() 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 entire JLabel. In contrast, this example shows how to validate() a container after replacing components.

这篇关于在JPanel上重画/刷新JLabel的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆