gpt4 book ai didi

来自另一个类的 Java setText JLabel

转载 作者:行者123 更新时间:2023-12-02 03:40:37 25 4
gpt4 key购买 nike

你好,我有两个类,一个只包含我的 GUI,另一个包含我的方法。我需要通过 Blackjack 类的方法之一更新 BlackjackGUI 类中的playerMoneyLbl 标签。我认为我需要首先将 int 转换为字符串,然后才能将其显示为标签,但我的代码不起作用。我很感激任何帮助。这是我的代码:

BlackjackGUI 类

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.
}



}

最佳答案

你有很多选择,有些好,有些坏

你可以...

JLabel 的引用传递给另一个类。这有点糟糕:

  • 因为它将 UI 组件公开给类
  • 将代码耦合在一起,导致将来难以管理或更改
  • 将 UI 组件公开给其他类,这将允许对其或其他组件进行未经检查的更改
  • 确实超出了二等兵的职责范围。当事情发生变化时,它关心你想要发生什么?

你可以...

使用 Observer Pattern ,允许感兴趣的各方将自己注册到第二类​​,这样当某个事件发生时,第二类可以通知感兴趣的各方,然后他们可以根据自己的个人需求采取任何行动

这个:

  • 解耦代码,因为没有一个部分依赖于另一部分,从而使将来更容易更改和修改代码
  • 定义并划分类的职责范围

您可能还想查看Model-View-Controller这可能会给你一些关于如何构建代码的更多想法

关于来自另一个类的 Java setText JLabel,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36877540/

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