制作卡牌游戏,需要根据玩家手中的牌数在运行时将JLabels添加到JPanel



在我的游戏中,玩家的手牌通常约为 5 张牌(和一个单独的ArrayList),但游戏中有很多"抽牌"选项可以迅速变大。在显示卡片的其他任何地方,我将它们放在一个JLabel中,该更新以显示图像,单击时,图像移动到玩家丢弃堆中,并将卡片添加到丢弃ArrayList中,如下所示:

private void mainDeckMouseClicked(java.awt.event.MouseEvent evt) {                                      
ImageIcon mainDeckEmpty = new ImageIcon("C:\DC Card Game\src\DCCardGame\resources\Other Cards\Empty Main Deck.png");
if (DrawCards.cards.isEmpty())
mainDeck.setIcon(mainDeckEmpty);
if (slot1Card == noCard)
{
if (!DrawCards.cards.isEmpty())
{
slot1Card = DrawCards.drawFromMainDeck();
lineupSlot1.setIcon(new ImageIcon(slot1Card.getImage()));
lineupSlot1.setToolTipText(slot1Card.getCardText());
if (DrawCards.cards.isEmpty())
mainDeck.setIcon(mainDeckEmpty);
}
}
}

这是点击主套牌时的代码片段,它将卡片添加到阵容中。这工作得很好。

我遇到的问题是,对于playerHandArrayList,我无法弄清楚如何动态添加JLabel(必要时),根据绘制的卡片为其分配图像,然后将实际的卡片对象分配给可以操作的变量。在上面的示例中,我将其分配给预定义的变量slot1Card那么当我无法在运行时定义卡片时,我如何能够将卡片分配给变量,或者有没有办法分配上面显示的所有值(丢弃图像、玩家手部图像、工具提示文本、卡片对象等)而不使用变量?另外,我在JScrollPane内使用GridLayout在面板中显示玩家手牌。

提前感谢!

编辑:更多SSCCE - 这是我刚刚快速编写的代码,以便在鼠标单击时将牌从套牌添加到玩家手中。我相信,这需要修改才能向 jpanel 添加标签。

private void playerDeckMouseClicked(java.awt.event.MouseEvent evt) {                                        
ImageIcon playerDeckEmpty = new ImageIcon("C:\DC Card       Game\src\DCCardGame\resources\Other Cards\Empty Player Deck.png");
if (DrawCards.player_1_deck.isEmpty())
playerDeck.setIcon(playerDeckEmpty);
if (!DrawCards.cards.isEmpty())
{
playerHandSlot1Card = DrawCards.drawFromPlayerDeck();
playerHandSlot1.setIcon(new ImageIcon(playerHandSlot1Card.getImage()));
playerHandSlot1.setToolTipText(playerHandSlot1Card.getCardText());
playerHandSlot2Card = DrawCards.drawFromPlayerDeck();
playerHandSlot2.setIcon(new ImageIcon(playerHandSlot2Card.getImage()));
playerHandSlot2.setToolTipText(playerHandSlot2Card.getCardText());
playerHandSlot3Card = DrawCards.drawFromPlayerDeck();
playerHandSlot3.setIcon(new ImageIcon(playerHandSlot3Card.getImage()));
playerHandSlot3.setToolTipText(playerHandSlot3Card.getCardText());
playerHandSlot4Card = DrawCards.drawFromPlayerDeck();
playerHandSlot4.setIcon(new ImageIcon(playerHandSlot4Card.getImage())); 
playerHandSlot4.setToolTipText(playerHandSlot4Card.getCardText());
playerHandSlot5Card = DrawCards.drawFromPlayerDeck();
playerHandSlot5.setIcon(new ImageIcon(playerHandSlot5Card.getImage()));
playerHandSlot5.setToolTipText(playerHandSlot5Card.getCardText());
}
if (DrawCards.player_1_deck.isEmpty())
playerDeck.setIcon(playerDeckEmpty);
}                                       

与其为玩家的每张牌创建一个单独的变量,不如使用List,例如ArrayList。您可以根据需要创建列表的每个元素,如下所示:

private void playerDeckMouseClicked(java.awt.event.MouseEvent evt) {                                        
ImageIcon playerDeckEmpty = new ImageIcon("C:\DC Card       Game\src\DCCardGame\resources\Other Cards\Empty Player Deck.png");
if (DrawCards.player_1_deck.isEmpty())
playerDeck.setIcon(playerDeckEmpty);
for (int i = 0; i < 5 && !DrawCards.cards.isEmpty(); i++)
{
playerHandSlotCard = DrawCards.drawFromPlayerDeck();
playerHandSlot.setIcon(new ImageIcon(playerHandSlot1Card.getImage()));
playerHandSlot.setToolTipText(playerHandSlot1Card.getCardText());
playerHandList.add(platerHandSlot);
}
if (DrawCards.player_1_deck.isEmpty())
playerDeck.setIcon(playerDeckEmpty);
}

要显示玩家手牌包含的许多标签,您可以将它们动态添加到容器中。与其使用JLabel来显示玩家的手牌,不如使用带有CardLayoutJPanel。每当玩家抽到一张牌时,就叫panel.add(playerHandSlot)。如果这种情况发生在playerDeckMouseClicked方法之外,则可以使用for循环来访问playerHandList的元素

for(JLabel playerHandSlot : playerHandList) {
panel.add(playerHandSlot);
}

最新更新