이것은이 문제를 해결하는 데 사용 된 코드 조각입니다.
private void initialize() {
frame = new JFrame();
frame.setBounds(100, 100, 457, 330);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(null);
JLayeredPane panel = new JLayeredPane();
panel.setBounds(0, 0, 457, 330);
frame.getContentPane().add(panel);
panel.setLayout(null);
JPanel background = new JPanel();
background.setBounds(0, 0, 457, 330);
panel.add(background);
background.setLayout(new MigLayout("","",""));
//In my particular problem I used some constraints, but they're not needed
backgroundLabels = new JLabel[NCOLS][NROWS];
for (int i = 0; i < NCOLS; i++)
for (int j = 0; j < NROWS; j++) {
backgroundLabels[i][j] = new JLabel(backgroundIcon);
//Assuming you have a backgroundIcon variable where you saved your icon
background.add(backgroundLabels[i][j], "cell " + i + " " + j);
}
JPanel characterPanel = new JPanel();
panel.setLayer(characterPanel, 1);
characterPanel.setBounds(0, 0, 457, 330);
panel.add(characterPanel);
characterPanel.setOpaque(false);
characterPanel.setLayout(new MigLayout("","",""));
characterLabels = new JLabel[NCOLS][NROWS];
for (int i = 0; i < NCOLS; i++)
for (int j = 0; j < NROWS; j++) {
characterLabels[i][j] = new JLabel("");
//Creates empty character labels. You can then add icons using setIcon()
characterPanel.add(characterLabels[i][j], "cell " + i + " " + j);
}
}
여기에는 여러 가지 방법이 있습니다. 잔디 레이블에 레이아웃 관리자를 적용하여 컴포넌트를 추가 할 수 있습니다. 이렇게하면 단순히 잔디 타일을 표시하는 데 'GridLayout'을 사용하거나 잔디 이미지를 사용하여 배경을 칠하는 사용자 정의 구성 요소를 만들 수 있습니다. 다시 레이아웃 관리자를 추가하여 구성 요소를 추가 할 수 있습니다. 두 가지 모두 장점이 있지만 사용자 정의 패널은 더 복잡하지만 구성 요소의 크기를 더 많이 제어 할 수 있습니다. 레이블은 더 간단하지만 내용의 아이콘이 아닌 아이콘의 크기가됩니다. – MadProgrammer
레이블이 아이콘의 크기. 시간이 있다면 샘플 코드를 게시 해주십시오. –
"이 문제를 해결했습니다 ..."로 질문을 편집하는 대신 질문에 답변을 추가하십시오 (예 : [mcve] 형태의 코드). 앞으로이 질문을 보러 오는 사람들은 그들이 비슷한 문제 또는 질문을 가지고 있다면 그것을 할 수 있습니다. @MadProgrammer는 작업 할 코드를 게시하지 않았으므로 샘플 코드를 게시하지 않았지만 여기에는 몇 가지 예가 있습니다 (http://stackoverflow.com/questions/19125707/simplest-way-to-set-image- as-jpanel-background) 및 [here] (http://stackoverflow.com/questions/1466240/how-to-set-an-image-as-a-background-for-frame-in-swing-gui-of -java) – Frakcool