2012-05-17 6 views
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; 

} 

답변

2

당신은 아마 하나의 카드가 각 부분을 차지할 수있는 임의의 그리드를 만들어야 할 것입니다. GridLayout 또는 GridBagLayout과 같습니다.

코드를 숨김으로써 볼 수 있듯이 기본적으로 반복 할 때마다 마지막 요소를 덮어 쓰게됩니다.

+0

나는 그것이 너무 무서웠다. 하지만 실제로 어떻게 동적으로 이미지를 추가 할 수 있는지 모르겠다. – Arthur

+0

내가 말했던 것처럼, 다른 레이아웃을 시도해 보라. GridLayout과 같습니다. 허용 된 경우 NetBeans와 같은 GUI 빌더에서 쉽게 설정할 수 있습니다. – OmniOwl

관련 문제