0
학교용으로 게임용 [X] 임의 카드를 만들어야합니다. 이 코드를 사용하여 이미지를 빌드하고 표시했습니다. 하나만 잘못되었습니다. 프로젝트를 표시 할 때 마지막 카드는 입니다.for 루프 내에서 여러 이미지를 표시하려면 어떻게해야합니까?
이러한 이미지를 서로 표시 할 수있는 방법이 있습니까?
public static void main(String[] args) throws FileNotFoundException, IOException {
Cards cards = new Cards();
int dealSize = 6;
deal = cards.getShuffledCards(dealSize);
System.out.println("Deal of 4 randomly picked cards " + deal);
f = new JFrame();
for(int i=0; i < dealSize; i++) {
card = deal.get(i).toString();
f.getContentPane().add(setLabel(card));
}
f.pack();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setLocation(200,200);
f.setVisible(true);
}
public static JLabel setLabel(String fileLocation) throws FileNotFoundException, IOException {
InputStream file = new BufferedInputStream(
new FileInputStream([DIRECTORY TO IMAGES]" + fileLocation));
BufferedImage image = ImageIO.read(file);
label = new JLabel(new ImageIcon(image));
return label;
}
나는 그것이 너무 무서웠다. 하지만 실제로 어떻게 동적으로 이미지를 추가 할 수 있는지 모르겠다. – Arthur
내가 말했던 것처럼, 다른 레이아웃을 시도해 보라. GridLayout과 같습니다. 허용 된 경우 NetBeans와 같은 GUI 빌더에서 쉽게 설정할 수 있습니다. – OmniOwl