不要這樣做,因爲你不能多添加相同的組件不止一次到可視化容器。最好使用多個JLabels,但讓它們使用相同的ImageIcon。 ImageIcons可多次使用輕鬆:
public MainFrame() {
pieceIcon[0] = new ImageIcon(System.getProperty("user.dir") +
"/images/piece1.png");
pieceIcon[1] = new ImageIcon(System.getProperty("user.dir") +
"/images/piece2.png");
this.add(boardPanel);
displayGUIboard();
}
public void displayGUIboard() {
boardPanel.add(new JLabel(pieceIcon[0]);
boardPanel.add(new JLabel(pieceIcon[0]);
}
順便說一句:請注意,您的變量沒有應該是靜態的。
編輯:關於您最近編輯:
使用ImageIcons時,該工作
boardLabels[0] = new JLabel(pieces[1]);
boardLabels[1] = new JLabel(pieces[1]);
,但我想避免這種情況,因爲更新電路板,我將有刪除然後重新加載JLabels。我寧願只更新已加載的標籤。」
解決方案
不,你不必在所有改變的JLabel。保持你的JLabel他們在哪裏,但簡單地交換他們持有的圖標使用的JLabel setIcon(...)
方法。
編輯
另外,不要混淆對象的變量,即使你創造了一堆JLabel的變量,如果他們都指向同一個對象的JLabel,你仍然不能添加一個JLabel對象不止一次到一個容器。
編輯幽州:
的代碼是一個遊戲的顯示功能的一部分。一組整數將表示被解釋的板(但不在上面的代碼中),並且正確的Jlabel圖像將被放置到網格佈局面板中以顯示板的gui。我已經得到了顯示的代碼工作正常,但在我目前的版本它刪除的JLabel從電路板然後創建新的JLabel(件...)...但我寧願它本身從整數數組更新,而不是刪除標籤,讀取數組,然後重新創建標籤。
因此,創建一個使用GridLayout並使用不變的JLabel填充它的JPanel。然後只需根據int數組中保存的值更改JLabels所保存的圖標。您可以創建一個簡化和自動化此過程的方法。
編輯有關:
編輯我以前試過,但它拋出一個空指針異常。
然後解決這個問題,就像任何NPE。找出哪條線引發NPE,檢查線上的變量,至少有一個爲空,然後修復它,以便在嘗試使用它之前初始化變量。
編輯
例如:
import java.awt.Color;
import java.awt.Graphics;
import java.awt.GridLayout;
import java.awt.image.BufferedImage;
import javax.swing.*;
@SuppressWarnings("serial")
public class GridExample extends JPanel {
public static final int[][] MAP = {
{1, 0, 0, 2, 2, 2, 2, 2, 2, 2, 2},
{1, 1, 0, 0, 2, 2, 2, 2, 2, 2, 2},
{1, 1, 1, 0, 2, 2, 2, 2, 2, 2, 2},
{1, 1, 1, 0, 0, 2, 2, 2, 2, 2, 2},
{1, 1, 1, 1, 0, 2, 2, 2, 2, 2, 2},
{1, 1, 1, 0, 0, 0, 2, 2, 2, 2, 2},
{1, 1, 0, 0, 0, 2, 2, 2, 2, 2, 2},
{1, 1, 1, 0, 0, 0, 2, 2, 2, 2, 2},
{1, 1, 1, 1, 1, 0, 0, 0, 0, 2, 2},
{1, 1, 1, 1, 1, 0, 0, 0, 2, 2, 2},
{1, 1, 1, 1, 1, 1, 0, 0, 0, 2, 2}
};
public static final Color[] COLORS = {};
private JLabel[][] labelGrid = new JLabel[MAP.length][MAP[0].length];
public GridExample() {
setLayout(new GridLayout(MAP.length, MAP[0].length));
for (int r = 0; r < labelGrid.length; r++) {
for (int c = 0; c < labelGrid[r].length; c++) {
labelGrid[r][c] = new JLabel();
labelGrid[r][c].setIcon(Ground.getGround(MAP[r][c]).getIcon());
add(labelGrid[r][c]);
}
}
}
private static void createAndShowGui() {
GridExample mainPanel = new GridExample();
JFrame frame = new JFrame("GridExample");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(mainPanel);
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGui();
}
});
}
}
enum Ground {
DIRT(0, new Color(205,133, 63)), GRASS(1, new Color(0, 107, 60)),
WATER(2, new Color(29, 172, 214));
private int value;
private Color color;
private Icon icon;
private Ground(int value, Color color) {
this.value = value;
this.color = color;
icon = createIcon(color);
}
private Icon createIcon(Color color) {
int width = 24; // how to use const in enum?
BufferedImage img = new BufferedImage(width, width, BufferedImage.TYPE_INT_ARGB);
Graphics g = img.getGraphics();
g.setColor(color);
g.fillRect(0, 0, width, width);
g.dispose();
return new ImageIcon(img);
}
public int getValue() {
return value;
}
public Color getColor() {
return color;
}
public Icon getIcon() {
return icon;
}
public static Ground getGround(int value) {
for (Ground ground : Ground.values()) {
if (ground.getValue() == value) {
return ground;
}
}
return null;
}
}
其中顯示了一個GUI網格:
獲得從路徑相對於應用程序資源的'user.dir'是*** ***非常脆弱。由於這些圖像顯然是應用程序固有的,因此應將它們添加到Jar中作爲[嵌入資源](http://stackoverflow.com/tags/embedded-resource/info)。 – 2012-07-19 03:35:04