【发布时间】:2012-05-25 01:33:19
【问题描述】:
为了学校,我需要做一个游戏,随机发 [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;
}
【问题讨论】: