我正在制作一个二十一点类型的游戏,并且想将赌注从实际二十一点框架传递到另一个框架,当你赢/输时弹出赢/输的金额。我的代码是:
public int getBet() {
return (bet1);
}
public int getMoney() {
return (money1);
}
(以上所有代码都在公共类中,而不是公共方法中)。
当我尝试使用来自另一个框架(弹出)的这些 get 语句中的任何一个时,代码
public class LoseFrame extends JFrame {
JLabel Lost;
int bet;
public LoseFrame(){
super("LoseFrame");
JFrame LoseFrame = new JFrame("");
JPanel panel = new JPanel();
panel.setBackground(Color.LIGHT_GRAY);
Lost = new JLabel("Sorry, you busted and lost $" + blackJackFrame.getBet());
panel.add(Lost);
LoseFrame.setBounds (300, 300, 400, 70);
LoseFrame.setContentPane (panel);
LoseFrame.setVisible (true);
}
}
它给了我错误:
C:\LoseFrame.java:27: error: non-static method getBet() cannot be referenced from a static context
Lost = new JLabel("Sorry, you busted and lost $" + blackJackFrame.getBet());
感谢任何帮助过的人,如果需要更多信息,我可以发布它,在此停留了一段时间,可能是一个简单的错误。谢谢 编辑:这是 blackjackframe 的开始,它的代码超过 2500 行,不知道您是否要我发布它,但是 get 方法在公共类中...去掉了一些东西以使其更具可读性
public class blackJackFrame extends JFrame implements ActionListener{
JLabel bet,money,card1,card2,card3,card4,card5,handscore;
JButton hit,deal,stand;
JRadioButton b10,b50,b100,b250,b500,b1000;
int bet1=1,money1=1000;
boolean gameinprogress = false,playerbust = false,dealerbust = false;
public blackJackFrame() {
编辑#2:blackjackFrame 正在通过按钮从主页启动。它是用代码启动的:
public class PlayFrame extends JFrame implements ActionListener {
JButton slots,blackJack;
public PlayFrame(){
super("PlayFrame");
JFrame PlayFrame = new JFrame("Chrisino Lobby");
JPanel panel = new JPanel();
PlayFrame.setBounds (300, 300, 250, 100);
slots = new JButton("Slots");
blackJack = new JButton("BlackJack");
slots.addActionListener(this);
blackJack.addActionListener(this);
panel.add(slots);
panel.add(blackJack);
PlayFrame.setContentPane(panel);
PlayFrame.setVisible(true);
}
public void actionPerformed(ActionEvent e) {
JButton c = (JButton)e.getSource();
if (c.equals(slots)){
new SlotsFrame ();
}
else if (c.equals(blackJack)){
new blackJackFrame ();
}
}
}