简体   繁体   English

Java setText JLabel来自另一个类

[英]Java setText JLabel from another class

Hello I have two classes one which contains just my GUI and the other with my methods. 您好,我有两个类,一个仅包含我的GUI,另一个包含我的方法。 I need to update the playerMoneyLbl label in my BlackjackGUI class, from one of my Blackjack class's methods. 我需要从Blackjack类的一种方法中更新BlackjackGUI类中的playerMoneyLbl标签。 I thought I will need to first convert the int to string before it can be displayed as a label but my code does not work. 我以为我需要先将int转换为字符串,然后才能将其显示为标签,但是我的代码无法正常工作。 I am grateful for any help. 感谢您的帮助。 Here is my code: 这是我的代码:

BlackjackGUI Class 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();

    }

}

Blackjack Class 大酒杯类

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



}

You have any number of options, some good, some bad 您有许多选择,有些不错,有些不好

You could... 你可以...

Pass a reference of the JLabel to the other class. JLabel的引用传递给另一个类。 This is kind of bad: 这有点不好:

  • As it exposes the UI component to the class 因为它将UI组件暴露给类
  • Couples the code together making it difficult to manage or change in the future 将代码结合在一起,将来很难管理或更改
  • Exposes the UI component to the other class which would allow it to make unchecked to changes to it or other components 将UI组件公开给另一个类,这将使它无需检查即可对其或其他组件的更改
  • Really is beyond the scope of the respobsibility for the second class. 确实超出了第二类的责任范围。 What does it care about what you want to happen when something changes? 它对您在发生变化时想要发生的事情有什么影响?

You could... 你可以...

Use an Observer Pattern , allowing interested parties to register themselves to the second class, so that when some event occured, the second class could notify the interested parties and then they could take what ever action that they wanted to based on their own individual needs 使用观察者模式 ,允许感兴趣的团体向第二类注册,这样,当某些事件发生时,第二类可以通知感兴趣的团体,然后他们可以根据自己的个人需要采取他们想要采取的行动

This: 这个:

  • Decouples the code, as no one piece is reliant on the other, making it easier to change and modify the code in the future 解耦代码,因为没有代码可以相互依赖,因此将来更容易更改和修改代码
  • Defines and separates the area's of responsibility for the classes 定义并区分班级的职责范围

You might also like to have a look at Model-View-Controller which might give you some more ideas about how to structure your code 您可能还想看看Model-View-Controller ,它可能会给您更多有关如何构建代码的想法

I think a better approach would be to create a getter method inside the Blackjack class and update the label in the BlackjackGUI class (since it's a GUI class it should be the one that does GUI stuff, like updating labels). 我认为一种更好的方法是在Blackjack类中创建一个getter方法,并更新BlackjackGUI类中的标签(由于它是一个GUI类,因此应该是做GUI东西的类,例如更新标签)。

Also You are trying to access playerMoneyLbl variable as if it was static (BlackjackGUI.playerMoneyLbl), but it's not. 另外,您试图访问playerMoneyLbl变量,就好像它是静态变量一样(BlackjackGUI.playerMoneyLbl),但不是。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM