我有一个网格布局 JPanel,上面绘制了许多图标。我需要在玩家移动后更改图标,因为地图会发生变化。这是我想出的方法:
public void initGridIcons(JPanel pnl, String map) {
pnl.removeAll();
char[] tiles = map.toCharArray();
for (char ch : tiles) {
JLabel label = new JLabel("");
String icon = "";
switch (ch) { // lots of cases irrelevant to problem
}
label.setIcon(new ImageIcon(ClientGUI.class
.getResource("/resources/" + icon)));
pnl.add(label);
}
}
这最初是有效的,因为它确实正确地绘制了玩家和地图。然而,它不会在每回合后改变。
我已经尝试将此方法与迭代器计数器(偶数更改)一起使用,以确保它不是任何其他代码,并且它不起作用,所以我确定是这个。