我在运行时更新2D数组中的JLabel时遇到问题。在JPanel上重绘/刷新JLabels
我正在开发的程序是Connect Four的变体。我创建了一个JLabels二维数组,它们都默认为包含空白插槽图像的ImageIcon。玩家1和2选择他们的颜色,并且在玩家轮到时,他可以点击将一块物品放入一个柱子(重力导致该物品落到底部或直到它落在另一块物品上)。
我很积极,我的addToColumn方法工作正常。我唯一的问题是,我似乎无法得到任何JLabel更新。以下是我正在处理的方法:
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();
现在随着最后两行改变了的JLabel砖[] [],显然我并不需要两个,不知道哪种方式比较好...这只是一些什么我已经试过,无济于事。 (我的getColor()方法返回一个Color,并且findColorIcon(Color c)返回具有该颜色的tile的相应JLabel)。
,是的,我在我的paintComponent方法已经添加过:
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
}
我一直停留在这一段时间,现在,我觉得我失去了一些东西明显。有什么建议么?
有关您的特定问题的更好指导,请使用[sscce](http://sscce.org/)更新您的问题。 – trashgod 2011-04-02 09:49:34