0

学校では、[X]枚のランダムなカードを扱うゲームを作る必要があります。このコードを使用して、画像を作成して表示しました。うまくいかないことが1つだけあります。プロジェクトを表示するときは、最後のカードのみが表示されます。

これらの画像を並べて表示する方法はありますか?

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;

}
4

1 に答える 1

2

おそらく、1枚のカードが各部分を占めることができる任意のグリッドを作成する必要があります。GridLayoutまたはGridBagLayoutのように。

コードをスキミングして確認できることから、基本的に、反復ごとに最後に配置した要素を上書きしていることになります。

于 2012-05-17T13:07:24.180 に答える