gpt4 book ai didi

Java Swing 验证?

转载 作者:行者123 更新时间:2023-12-02 10:09:35 25 4
gpt4 key购买 nike

我正在尝试使用 swing 创建一个 Blackjack 应用程序,但当玩家单击“命中”按钮时,我很难添加新牌。我觉得这与 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 的 MainFrame 类。

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


}

}

最佳答案

What exactly does "keeping a reference" mean?

你一直这样做:

hitButton = new JButton("HIT");

上面创建了 JButton 的实例并保留对其的引用。

然后在代码中使用以下方法更改按钮的属性:

hitButton.addActionListener(new ActionListener() ...

您的自定义面板没有什么不同。您可以使用要执行的方法创建自定义类。

因此,在代码中的某个地方,您需要如下逻辑:

PlayHand playHandPanel = new PlayHand();
ChoiceBar choiceBarPanel = new ChoiceBar( playHandPanel );
frame.add( playHandPanel );
frame.add( choiceBar );

然后在 ChoiceBar 的构造函数中将对“playHandPanel”的引用保存为类中的实例变量。然后,在按钮的 ActionListener 中,您现在可以调用 cardAdded() 方法。

关于Java Swing 验证?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55095767/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com