你好,我有两个类,一个只包含我的GUI,另一个包含我的方法。我需要从我的Blackjack类的方法中更新我的BlackjackGUI类中的playerMoneyLbl标签。我想我需要先将int转换为字符串才能显示为标签,但我的代码不起作用。我很感激任何帮助。这是我的代码:
BlackjackGUI Class
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
public class BlackjackGUI{
private JFrame frame;
private JPanel panel, panelTop, panelBottom, panelCenter, panelHitBtn, panelStandBtn;
private JButton newGameBtn, dealBtn, hitBtn, standBtn;
JLabel placeBetLbl, playerMoneyLbl, playerCardLbl, dealerCardLbl, playerCardTotalLbl, dealerCardTotalLbl;
private JTextField betInputBox;
private Blackjack Blackjack = new Blackjack();
Color bg = new Color(0, 100, 0);
public BlackjackGUI(){
createForm();
fillTop();
fillCenter();
fillBottom();
frame.add(panel);
frame.setVisible(true);
}
public void createForm() {
frame = new JFrame("Blackjack");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(1000,900);
panel = new JPanel();
panel.setLayout(new BorderLayout());
panel.setBackground(bg);
panelTop = new JPanel();
panelTop.setBackground(bg);
panel.add(panelTop, BorderLayout.NORTH);
panelBottom = new JPanel();
panelBottom.setBackground(bg);
panel.add(panelBottom, BorderLayout.SOUTH);
panelCenter = new JPanel();
panelCenter.setLayout(new GridLayout(3, 2));
panelCenter.setBackground(bg);
panel.add(panelCenter, BorderLayout.CENTER);
}
public void fillTop() {
newGameBtn = new JButton("New Game");
newGameBtn.setFont(new Font("Gill Sans MT", Font.PLAIN, 16));
panelTop.add(newGameBtn);
newGameBtn.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent event) {
System.exit(0);
}
});
}
public void fillCenter() {
ImageIcon defaultDealerCard1 = new ImageIcon("Images/Cards/BACK.png");
dealerCardLbl = new JLabel();
dealerCardLbl.setIcon(defaultDealerCard1);
dealerCardLbl.setHorizontalAlignment(dealerCardLbl.CENTER);
panelCenter.add(dealerCardLbl);
dealerCardTotalLbl = new JLabel("5");
dealerCardTotalLbl.setFont(new Font("Gill Sans MT", Font.PLAIN, 35));
dealerCardTotalLbl.setForeground(Color.WHITE);
dealerCardTotalLbl.setHorizontalAlignment(dealerCardTotalLbl.CENTER);
panelCenter.add(dealerCardTotalLbl);
ImageIcon defaultPlayerCard1 = new ImageIcon("Images/Cards/BACK.png");
playerCardLbl = new JLabel();
playerCardLbl.setIcon(defaultPlayerCard1);
playerCardLbl.setHorizontalAlignment(playerCardLbl.CENTER);
panelCenter.add(playerCardLbl);
playerCardTotalLbl = new JLabel("16 - BUST! You lose :(");
playerCardTotalLbl.setFont(new Font("Gill Sans MT", Font.PLAIN, 35));
playerCardTotalLbl.setForeground(Color.WHITE);
playerCardTotalLbl.setHorizontalAlignment(playerCardTotalLbl.CENTER);
panelCenter.add(playerCardTotalLbl);
panelHitBtn = new JPanel();
panelHitBtn.setLayout(new GridLayout(3, 3));
panelHitBtn.setBackground(bg);
panelCenter.add(panelHitBtn);
panelStandBtn = new JPanel();
panelStandBtn.setLayout(new GridLayout(3, 3));
panelStandBtn.setBackground(bg);
panelCenter.add(panelStandBtn);
hitBtn = new JButton("Hit");
hitBtn.setMaximumSize( new Dimension (50,50));
hitBtn.setHorizontalAlignment(hitBtn.CENTER);
hitBtn.setVerticalAlignment(hitBtn.CENTER);
panelHitBtn.add(hitBtn, BorderLayout.CENTER);
hitBtn.setVisible(false);
hitBtn = new JButton("Hit");
hitBtn.setMaximumSize( new Dimension (50,50));
hitBtn.setHorizontalAlignment(hitBtn.CENTER);
hitBtn.setVerticalAlignment(hitBtn.CENTER);
panelHitBtn.add(hitBtn, BorderLayout.CENTER);
hitBtn.setVisible(false);
hitBtn = new JButton("Hit");
hitBtn.setMaximumSize( new Dimension (50,50));
hitBtn.setHorizontalAlignment(hitBtn.CENTER);
hitBtn.setVerticalAlignment(hitBtn.CENTER);
panelHitBtn.add(hitBtn, BorderLayout.CENTER);
hitBtn.setVisible(false);
hitBtn = new JButton("Hit");
hitBtn.setMaximumSize( new Dimension (50,50));
hitBtn.setHorizontalAlignment(hitBtn.CENTER);
hitBtn.setVerticalAlignment(hitBtn.CENTER);
panelHitBtn.add(hitBtn, BorderLayout.CENTER);
hitBtn.setVisible(false);
hitBtn = new JButton("HIT"); //KEEP THIS ONE
hitBtn.setFont(new Font("Gill Sans MT", Font.PLAIN, 35));
hitBtn.setHorizontalAlignment(hitBtn.CENTER);
hitBtn.setVerticalAlignment(hitBtn.CENTER);
panelHitBtn.add(hitBtn, BorderLayout.CENTER);
hitBtn = new JButton("Hit");
hitBtn.setMaximumSize( new Dimension (50,50));
hitBtn.setHorizontalAlignment(hitBtn.CENTER);
hitBtn.setVerticalAlignment(hitBtn.CENTER);
panelHitBtn.add(hitBtn, BorderLayout.CENTER);
hitBtn.setVisible(false);
hitBtn = new JButton("Hit");
hitBtn.setMaximumSize( new Dimension (50,50));
hitBtn.setHorizontalAlignment(hitBtn.CENTER);
hitBtn.setVerticalAlignment(hitBtn.CENTER);
panelHitBtn.add(hitBtn, BorderLayout.CENTER);
hitBtn.setVisible(false);
hitBtn = new JButton("Hit");
hitBtn.setMaximumSize( new Dimension (50,50));
hitBtn.setHorizontalAlignment(hitBtn.CENTER);
hitBtn.setVerticalAlignment(hitBtn.CENTER);
panelHitBtn.add(hitBtn, BorderLayout.CENTER);
hitBtn.setVisible(false);
hitBtn = new JButton("Hit");
hitBtn.setMaximumSize( new Dimension (50,50));
hitBtn.setHorizontalAlignment(hitBtn.CENTER);
hitBtn.setVerticalAlignment(hitBtn.CENTER);
panelHitBtn.add(hitBtn, BorderLayout.CENTER);
hitBtn.setVisible(false);
standBtn = new JButton("Stand");
panelStandBtn.add(standBtn);
standBtn.setVisible(false);
standBtn = new JButton("Stand");
panelStandBtn.add(standBtn);
standBtn.setVisible(false);
standBtn = new JButton("Stand");
panelStandBtn.add(standBtn);
standBtn.setVisible(false);
standBtn = new JButton("Stand");
panelStandBtn.add(standBtn);
standBtn.setVisible(false);
standBtn = new JButton("STAND");
standBtn.setFont(new Font("Gill Sans MT", Font.PLAIN, 35));
panelStandBtn.add(standBtn);
standBtn = new JButton("Stand");
panelStandBtn.add(standBtn);
standBtn.setVisible(false);
standBtn = new JButton("Stand");
panelStandBtn.add(standBtn);
standBtn.setVisible(false);
standBtn = new JButton("Stand");
panelStandBtn.add(standBtn);
standBtn.setVisible(false);
standBtn = new JButton("Stand");
panelStandBtn.add(standBtn);
standBtn.setVisible(false);
}
public void fillBottom() {
betInputBox = new JTextField("25");
betInputBox.setFont(new Font("Gill Sans MT", Font.PLAIN, 35));
betInputBox.setHorizontalAlignment(SwingConstants.RIGHT);
betInputBox.setPreferredSize(new Dimension(175,50));
betInputBox.addMouseListener(new MouseAdapter(){
@Override
public void mouseClicked(MouseEvent e){
betInputBox.setText("");
}
});
panelBottom.add(betInputBox, BorderLayout.WEST);
dealBtn = new JButton("DEAL");
dealBtn.setFont(new Font("Gill Sans MT", Font.PLAIN, 35));
panelBottom.add(dealBtn);
newGameBtn.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent event) {
System.exit(0);
}
});
placeBetLbl = new JLabel("Place your bets!");
placeBetLbl.setFont(new Font("Gill Sans MT", Font.PLAIN, 35));
placeBetLbl.setForeground(Color.YELLOW);
panelBottom.add(placeBetLbl);
playerMoneyLbl = new JLabel();
playerMoneyLbl.setFont(new Font("Gill Sans MT", Font.PLAIN, 35));
playerMoneyLbl.setForeground(Color.WHITE);
panelBottom.add(playerMoneyLbl, BorderLayout.EAST);
}
public static void main(String[] args) {
new BlackjackGUI();
}
}
二十一点类
public class Blackjack {
int playerMoney = 2222;
String playerMoneyString;
public void process(){
playerMoneyString = Integer.toString(playerMoney);
BlackjackGUI.playerMoneyLbl.setText
}
public void deal() {
//deal code goes here.
}
}
答案 0 :(得分:2)
你有很多选择,有些好,有些不好
将JLabel
的引用传递给另一个类。这有点不好:
使用Observer Pattern,允许感兴趣的各方将自己注册到第二类,这样当某个事件发生时,第二类可以通知感兴趣的各方,然后他们可以采取他们想要的任何行动根据自己的个人需求
此:
您可能还想查看Model-View-Controller,它可能会为您提供有关如何构建代码的更多建议
答案 1 :(得分:0)
我认为更好的方法是在Blackjack类中创建一个getter方法并更新BlackjackGUI类中的标签(因为它是一个GUI类,它应该是执行GUI的东西,比如更新标签)。
此外,您正在尝试访问playerMoneyLbl变量,就好像它是静态的(BlackjackGUI.playerMoneyLbl),但事实并非如此。