2

所以我正在创建纸牌游戏 Gin Rumy。我正在处理图形,特别是创建一个更新的面板,以便它始终显示玩家手中的卡片对象的矢量。Cards 本身是一个扩展画布类的组件。我要问的是如何创建一些函数 updatePanel() 在玩家的手(卡片对象的向量)发生变化时更新面板。我能想到的就是从 GUI 框架中移除旧面板,创建一个新面板,用玩家手中的卡片填充新面板,然后将新面板粘贴到 GUI 框架上。我什至没有对此进行编码,因为我担心从 GUI 框架中取出一个组件然后将其重新装上会冒破坏布局的风险,而且我的直觉告诉我,每次创建一个新面板都是一个坏主意. 所以我'

您还会注意到 selectButton 的 actionPerformed() 方法是不完整的。我想要做的是删除有焦点的卡片。我了解焦点和动作事件的工作原理,但我不了解的是如何在面板中搜索并找到被选中的卡片并将其删除。

我非常感谢有关 updatePanel() 或 selectButton 的 actionPerformed() 方法的任何帮助,或有关在整个程序过程中从面板中添加和删除对象的任何其他有用信息。

// GraphicGinRumy

import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.util.Vector;
import java.util.Random;

public class GraphicGinRumy extends GUIFrame {

    protected Card card;
    protected Button selectButton, newGameButton;
    protected Panel controlPanel, playerPanel;
    protected RandomCardDeck deck;
    protected Vector<Card> playerHand;

    //Pardon this weird indentation...the code didn't copy to stack overflow correctly
    public GraphicGinRumy() {
        super("Gin Rumy");
        deck = new RandomCardDeck();
        playerHand = new Vector<Card>();

        MouseListener ml = new MouseAdapter() {
            public void mousePressed(MouseEvent e) {
                Card selectedCard = (Card) e.getSource();
                if (selectedCard.getFocused()) {
                    selectedCard.setFocused(false);
                } else {
                    selectedCard.setFocused(true);
                }
            }
        };

        deck.shuffle();
        rumyDeal();

        //Player Panel
        playerPanel = new Panel();
        for (int c = 0; c < playerHand.size(); c++) {
            playerPanel.add(playerHand.elementAt(c));
        }
        add(playerPanel, BorderLayout.CENTER);

        //Control Panel
        controlPanel = new Panel();
        selectButton = new Button("Select");
        selectButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
            }
        });
        controlPanel.add(selectButton);
        newGameButton = new Button("New Game");
        newGameButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                deck.shuffle();
                deck.deal();
                updatePanel();
            }
        });
        controlPanel.add(newGameButton);
        add(controlPanel, BorderLayout.SOUTH);

        setSize(500, 100);
        setVisible(true);

        //__________________________________________________________________
        //Game Setup
        //__________________________________________________________________

        //Set card values:
        //2 to 9 is worth 5 pts; 10 to K is worth 10 pts; A is worth 15 pts
        for (int c = 0; c < deck.getNumCards(); c++) {
            card = deck.getCard(c);
            if (card.getFaceValue() < 10) {
                card.setValue(5);
            } else if (card.getFaceValue() < Card.ACE) {
                card.setValue(10);
            } else {
                card.setValue(15);
            }
        }

    }

    public static void main(String args[]) {
        GraphicGinRumy rumy = new GraphicGinRumy();
        //rumy.play();
    }

    protected void rumyDeal() {
        //Give player and CPU 7 cards and add 1 to the discardPile
        Card card;
        int numCards = 7;
        for (int i = 0; i < numCards; i++) {
            playerHand.add(deck.deal());
        }
        for (int i = 0; i < numCards; i++) {
            card = deck.deal();
            card.setVisible(true);
            cpuHand.add(card);
        }
        discardPile.add(deck.deal());
    }

    protected void updatePanel() {
        playerPanel = new Panel();
    }
}
4

0 回答 0