번보다 더 많은 같은 구성 요소를 추가 할 수 없기 때문에이 작업을 수행하지 마십시오 시각화 된 컨테이너. 여러 개의 JLabel을 사용하는 것이 더 좋지만 같은 것을 사용하도록하십시오. ImageIcon. ImageIcons은 쉽게 번 이상 사용할 수 있습니다
또한 편집
, 객체와 변수를 혼동하지 마십시오. 모두 동일한 JLabel의 객체를 참조하는 경우는, JLabel의 변수의 무리를 만들 경우에도 할 수 없습니다 JLabel 객체를 추가하십시오. 용기에 한 번 이상.
편집 당신의 상태 :
코드 게임에 대한 표시 기능의 일부입니다.정수의 배열은 해석되는 보드를 나타낼 것이지만 위의 코드에서는 그렇지 않습니다. 올바른 Jlabel 이미지는 보드의 GUI를 표시하는 격자 레이아웃 패널에 배치됩니다. 나는 잘 작동하도록 표시 코드를 얻었지만 내 현재 버전에서는 보드에서 jlabels를 제거한 다음 새 JLabel (조각 ...)을 만듭니다 ...하지만 제거하는 대신 정수 배열에서 자체를 업데이트하는 것이 좋습니다. 레이블을 읽고 배열을 읽은 다음 레이블을 다시 만듭니다.
그래서 GridLayout을 사용하는 JPanel을 만들고 변경되지 않은 JLabel로 채 웁니다. 그런 다음 int 배열이 보유한 값을 기반으로 JLabel이 보유한 아이콘을 변경하십시오. 이 프로세스를 단순화하고 자동화하는 메소드를 작성할 수 있습니다. 대한
편집 :
편집은 내가 전에이 시도하지만 널 포인터 예외가 발생합니다.
그러면 NPE와 마찬가지로 이것을 해결하십시오. 어떤 라인이 NPE를 던 졌는지 확인하고, 라인의 변수를 확인하고, 적어도 하나는 null이며, 사용하기 전에 변수를 초기화하도록 수정하십시오.
편집 예를 들어
는 : GUI를 그리드 보여줍니다
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;
}
}
: 비교를 위해