視覚化されたコンテナーに同じコンポーネントを複数回追加することはできないため、これを行わないでください。複数の JLabel を使用することをお勧めしますが、同じ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]);
}
余談ですが、どの変数も静的であってはならないことに注意してください。
編集:最近の編集について:
これは機能します
boardLabels[0] = new JLabel(pieces[1]);
boardLabels[1] = new JLabel(pieces[1]);
ImageIconsを使用する場合、ボードを更新するにはJLabelsを削除してから再ロードする必要があるため、これを避けたいです。すでに読み込まれているラベルを更新するだけでよろしいです。」
解決策
いいえ、JLabels をまったく変更する必要はありません。JLabel はそのままにしておきますが、JLabelsetIcon(...)
メソッドを使用して保持するアイコンを交換するだけです。
編集
また、変数とオブジェクトを混同しないでください。多数の JLabel 変数を作成したとしても、それらがすべて同じ JLabel オブジェクトを参照している場合でも、コンテナにJLabel オブジェクトを複数回追加することはできません。
あなたの状態を編集 :
コードは、ゲームの表示関数の一部です。整数の配列は、解釈されるボードを表し (上記のコードではありません)、ボードの GUI を表示するために、正しい Jlabel イメージが gridlayout パネルに配置されます。表示コードが正常に動作するようになりましたが、現在のバージョンでは、ボードから jlabels を削除してから、新しい JLabels(piece...) を作成します...ただし、削除するのではなく、整数配列からそれ自体を更新することをお勧めしますラベル、配列の読み取り、ラベルの再作成。
したがって、GridLayout を使用する JPanel を作成し、変更されない JLabels で埋めます。次に、int 配列が保持する値に基づいて、JLabels が保持するアイコンを変更します。このプロセスを簡素化および自動化する方法を作成できます。
以下に関する編集:
編集以前にこれを試しましたが、ヌルポインター例外がスローされます。
次に、NPE と同じようにこれを解決します。どの行が NPE をスローしているかを調べ、その行の変数を確認し、少なくとも 1 つが null であることを確認してから、使用する前に変数を初期化するように修正します。
たとえば、次のように編集します。
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グリッドを示しています:
