gpt4 book ai didi

java - 如何在 for 循环中显示多个图像

转载 作者:行者123 更新时间:2023-12-02 07:47:29 24 4
gpt4 key购买 nike

对于学校,我需要制作一个处理 [X] 张随机纸牌的游戏。我使用这段代码来构建和显示我的图像。只有一件事出了问题。显示项目时,仅显示最后一张卡显示。

有什么方法可以将这些图像并排显示吗?

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;

}

最佳答案

您可能需要制作一个任意网格,其中一张卡可以占据每个部分。就像 GridLayout 或 GridBagLayout 一样。

通过浏览您的代码,我可以看到,对于每次迭代,您基本上都覆盖了您放置的最后一个元素。

关于java - 如何在 for 循环中显示多个图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10636312/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com