来自另一个类的Java setText JLabel [英] Java setText JLabel from another class

查看:88
本文介绍了来自另一个类的Java setText JLabel的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Hello I have two classes one which contains just my GUI and the other with my methods. I need to update the playerMoneyLbl label in my BlackjackGUI class, from one of my Blackjack class's methods. 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. I am grateful for any help. Here is my code:

BlackjackGUI Class

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

    }

}

Blackjack Class

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

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

Pass a reference of the JLabel to the other class. This is kind of bad:


  • 因为它将UI组件暴露给类

  • 所以将代码组合在一起使其难以管理或在将来进行更改

  • 将UI组件暴露给另一个类,这样就可以对其进行取消选中以对其或其他组件进行更改

  • 真的超出了第二堂课的责任范围。当事情发生变化时,你会想要发生什么?

  • As it exposes the UI component to the class
  • 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
  • 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?

使用观察者模式,允许感兴趣的各方自行注册第二类,所以当某个事件发生时,第二类可以通知感兴趣的各方,然后他们可以根据自己的个人需要采取他们想做的任何行动

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

这个:


  • 解密代码,因为没有任何一个部分依赖于另一个部分,因此更容易更改和修改代码未来

  • 定义并分离区域的责任区域

您可能也喜欢看看模型 - 视图 - 控制器这可能会给你一些关于如何构建你的想法代码

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

这篇关于来自另一个类的Java setText JLabel的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆