在我的游戏中,玩家的手牌通常约为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);
}
}
}
这是一个代码片段,从单击主甲板时开始,它将卡片添加到阵容中。这很好用。
我遇到的问题是,对于playerHand
ArrayList
,我无法弄清楚如何动态添加JLabel
(必要时),指定基于绘制的卡将图像映射到它,然后将实际的卡对象分配给可以操作的变量。在上面的例子中,我将它分配给预定义的变量slot1Card
,所以当我无法在运行时定义卡时,如何将卡分配给变量或者是否有办法分配所有变量上面显示的值(丢弃图像,播放器手形图像,工具提示文本,卡片对象等)而不使用变量?另外,我在GridLayout
内使用JScrollPane
在面板中显示玩家手牌。
提前致谢!
编辑:更多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);
}
答案 0 :(得分:0)
不是为每个玩家的牌创建单独的变量,而是使用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
来显示玩家的手,而是使用JPanel
和CardLayout
。每当玩家抽一张牌时,请拨打panel.add(playerHandSlot)
。如果这种情况发生在playerDeckMouseClicked
方法之外,您可以使用for
循环来访问playerHandList
for(JLabel playerHandSlot : playerHandList) {
panel.add(playerHandSlot);
}