我有一个我正在做的卡片项目,它使用GUI。我有一个JFrame将图像添加到Java中的GUI的最佳方法?
JFrame frame = new JFrame("Card Game");
//Splits the Jframe into two sections where the cards will be placed
//according to what user draws them
frame.setLayout(new GridLayout(1,2));
//The users, with different hands of cards
JPanel user1 = new JPanel();
JPanel user2 = new JPanel();
frame.add(user1);
frame.add(user2);
我希望能够为每个用户的手有效地添加/删除卡。用户可以绘制的最大数量的卡片是9张。最初用户将开始使用两张卡片,并且我想使它能够整齐地添加另一张卡片。如果面板中的空间用完了,则应该开始在下面形成一排。
我该如何做到这一点?我想用JLabels以某种方式,但不知道这是否是一种正确的方法。此外,我不知道我应该使用什么布局,所以任何帮助/提示将不胜感激。谢谢。
编辑
谢谢你的帮助,但我仍然有一个问题。我使用“Lourenco”发布的代码作为我的面板添加卡片。我没有改变它。 在我的GUI类,我添加
MyPanel myPanel = new MyPanel();
public void addPlayerCard(Card c){
myPanel.addCard(c.getImageIcon());//should draw the card into the panel
}
这是我的我的ImageIcon添加到卡
java.net.URL imgURL = Card.class.getResource(imgFileName);
if (imgURL != null) {
image = new ImageIcon(imgFileName, "");
} else {
System.err.println("Couldn't find file: " + imgFileName);
image = new ImageIcon();
}
我在做什么错?
刊登[MCVE](http://stackoverflow.com/help/mcve)。一定要将你的代码复制粘贴到一个*新的项目中*,并确保它在发布之前编译并运行。 – user1803551
它运行并编译正常,但没有绘制图像 –
请再次阅读链接。发布*在这里,为我们*一个MCVE。 *我们*需要能够运行代码并查看问题。 – user1803551