有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

Java Swing验证?

我正在尝试创建一个带有swing的21点应用程序,当玩家点击“点击”按钮时,我很难添加新卡。我觉得这与JLabel没有得到验证有关,但我不知道这到底意味着什么,也不知道如何解决问题。请帮忙

我对Java swing非常陌生,所以它可能看起来很直观,但我希望有人能友好地解释一下

下面是我目前拥有的代码,它为庄家和玩家各发两张牌,没有重复的牌,但无法显示新发的牌,即使选择了这张牌,因为我可以在控制台上看到它们

            import javax.swing.*;
        import java.awt.*;

        @SuppressWarnings("serial")
        public class PlayerHand extends JPanel {

            //declaring private vars

            private JLabel cardPonTable[] = new JLabel[11];
            private int cardP[] = new int[11];
            private Image cardPImage[] = new Image[11];

            private int cardOnTableCount = 0; //counter for number of cards on the table

            public PlayerHand(boolean firstDeal){
                setLayout(null);
                /**
                 * Deals the first two cards for the player
                 */
                if (firstDeal == true) { //run this code if true

                    //playerHand config
                    setBackground(new Color(238, 238, 238));
                    setLayout(null);

                    JLabel playersHandLabel = new JLabel("Player's Hand"); //creates a label indicating the bottom half of the screen is the player's hand

                    //player's hand label config
                    playersHandLabel.setFont(new Font("Lucida Grande", Font.PLAIN, 25));
                    playersHandLabel.setHorizontalAlignment(SwingConstants.CENTER);
                    playersHandLabel.setBounds(192, 314, 200, 80);

                    add(playersHandLabel); //add player's hand label to the container

                    //creates JLabel for two of the player's card, set the positions, and add to the container
                    cardPonTable[0] = new JLabel("");
                    cardPonTable[0].setBounds(80, 6, 220, 320);
                    add(cardPonTable[0]);

                    cardPonTable[1] = new JLabel("");
                    cardPonTable[1].setBounds(340, 6, 220, 320);
                    add(cardPonTable[1]);

                    System.out.println("Player's cards"); //indicate that the following is the player's dealt card on the console


                    CardDeal.createDeck(); //create a deck

                    //deal two card for the player
                    cardP[0] = CardDeal.cardDeal(); 
                    cardP[1] = CardDeal.cardDeal(); 

                    //get the image from the src folder

                    cardPImage[0] = new ImageIcon (this.getClass().getResource(cardP[0]+".png")).getImage(); 
                    cardPImage[1] = new ImageIcon (this.getClass().getResource(cardP[1]+".png")).getImage();

                    cardPonTable[0].setIcon(new ImageIcon (cardPImage[0])); //set the JLabel of the card to the image chosen above
                    cardOnTableCount++; //increase the counter by one
                    cardPonTable[1].setIcon(new ImageIcon (cardPImage[1])); //set the JLabel of the card to the image chosen above
                    cardOnTableCount++; //increase the counter by one


                }
                /**
                 * Do not deal the first two cards (instance made)
                 */

            }

            public void cardAdded() throws Exception  {

                //cardP1onTable.setBounds(cardP1onTable.getX()-50, cardP1onTable.getY(), (int)(WIDTH*0.7), (int)(HEIGHT*0.7));
                //cardP2onTable.setBounds(cardP2onTable.getX()-50, cardP2onTable.getY(), (int)(WIDTH*0.7), (int)(HEIGHT*0.7));

                PlayerHand newDealt = new PlayerHand(false); //creates an instance of playerHand method (send false as a parameter so that the method won't deal two cards again)

                System.out.println("Player's card dealt");

                newDealt.setLayout(null);

                cardPonTable[cardOnTableCount] = new JLabel("");
                cardPonTable[cardOnTableCount].setBounds(192, 6, 220, 320);
                newDealt.add(cardPonTable[cardOnTableCount]);
                cardP[cardOnTableCount] = CardDeal.cardDeal();
                cardPImage[cardOnTableCount] = new ImageIcon (newDealt.getClass().getResource(cardP[cardOnTableCount]+".png")).getImage();
                cardPonTable[cardOnTableCount].setIcon(new ImageIcon (cardPImage[cardOnTableCount]));

                cardOnTableCount++;
            }
        }

下面的代码是允许玩家选择命中或停留的JPanel

        import java.awt.Dimension;
        import javax.swing.JButton;
        import javax.swing.JPanel;
        import java.awt.GridLayout;
        import java.awt.event.ActionEvent;
        import java.awt.event.ActionListener;

        @SuppressWarnings("serial")
        public class ChoiseBar extends JPanel{

            private JButton hitButton;
            private JButton stayButton;

            public ChoiseBar() {

                Dimension dim = getPreferredSize();
                dim.height = 100;
                setPreferredSize(new Dimension(1200, 100));

                hitButton = new JButton("HIT");

                hitButton.addActionListener(new ActionListener() {

                    public void actionPerformed(ActionEvent e) {

                        try {
                            PlayerHand.cardAdded();
                        } catch (Exception e1) {
                            // TODO Auto-generated catch block
                            e1.printStackTrace();
                        }

                    }

                });

                stayButton = new JButton("STAY");
                setLayout(new GridLayout(0, 2, 0, 0));



                add(hitButton);
                add(stayButton);
            }

        }

这是一个大型机类,其中添加了PlayerHand、DealerHand和ChoiceBar

import javax.swing.JFrame;
import java.awt.Color;

@SuppressWarnings("serial")
public class MainFrame extends JFrame{

//declaring private vars

private DealerHand dealerHand;
private PlayerHand playerHand;
private ChoiseBar choiseBar;

public MainFrame() {

    super("TABLE"); //calling the "TABLE" method in BJ_APP

    playerHand = new PlayerHand(true); //creates an instance of playerHand (firstDeal is true as it is the first deal)
    //playerHand config 
    playerHand.setForeground(new Color(0, 0, 0)); 
    playerHand.setBackground(new Color(238, 238, 238));
    playerHand.setLocation(300, 625);
    playerHand.setSize(600, 400);

    dealerHand = new DealerHand(); //creates an instance of dealerHand
    //playerHand config
    dealerHand.setLocation(300, 31);
    dealerHand.setSize(600, 429);

    choiseBar = new ChoiseBar(); //creates an instance of choiseBar
    //choiseBar config
    choiseBar.setSize(800, 120);
    choiseBar.setLocation(214, 472);

    getContentPane().setLayout(null); //mainFrame uses absolute layout


    //add these three containers to mainFrame
    getContentPane().add(choiseBar);
    getContentPane().add(playerHand);
    getContentPane().add(dealerHand);

    setSize(1200,1080); //set the size of mainFrame

    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //the program will terminated when mainFrame is closed

    this.setVisible(true); //set mainFrame visible


}

}


共 (0) 个答案