JPanel, 창 및 JPanels 및 JLabel 배열이 있습니다. 5 개의 JPanel을 JFrame에 추가하고 JLabel을 각 JPanel에 추가하려고합니다. 각 JPanel은 주사위에 대한 데이터를 나타내는 데 사용됩니다.for 루프를 사용하여 JFrame에 JPanels 추가
그러나 코드를 실행하면 마지막 JPanel 만 "주사위 4"라는 텍스트가있는 JFrame에 나타납니다. 나는 왜 그런지 이해하지 못한다.
코드 :public static void main(String args[]) {
JFrame window = new JFrame("DICE Frame");
window.setVisible(true);
window.setTitle("DIE");
window.setSize(400, 200);
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel[] diceView=new JPanel[5];
JLabel[] labels=new JLabel[5];
for(int i=0; i<diceView.length; i++) {
diceView[i]=new JPanel();
window.add(diceView[i]);
labels[i]=new JLabel();
labels[i].setText("Dice "+i);
diceView[i].add(labels[i]);
}
}
어떤 레이아웃 관리자를 사용하고 있습니까? 확실한 대답은 모든 패널을 같은 위치에 놓는 것입니다. 그래서 4가 3의 상단에 있고 2는 2 위에 있습니다. – John3136