简体   繁体   English

如何将值从一个类传递到另一类GUI

[英]How to pass value from one class to another class GUI

I'm new in JAVA programming but how do I pass variable/value from one class A to seconnd class B where B is my GUI and A is logic to display the players? 我是JAVA编程的新手,但是如何将变量/值从一个A类传递给第二个B类,其中B是我的GUI,A是显示玩家的逻辑? I need to print the players name in GUI to lblPlayer label. 我需要在GUI中将玩家名称打印到lblPlayer标签。

Class A: A类:

    package driverPkg;

    import game.Card;
    import game.CardsUI;
    import game.players.CardPlayer;


    public class GameConsole implements CardsUI {


        public void currentCard(CardPlayer[] player) {


            for (CardPlayer p : player) {
            // here somehow return p.getName() and pass to the GUI
            }
        }
....more code not really need it

Class B (GUI) B类(GUI)

public class CardsGameGUI extends JFrame {

    private JPanel contentPane;


    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    CardsGameGUI frame = new CardsGameGUI();
                    frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * Create the frame.
     */
    public CardsGameGUI() {
        setFont(new Font("Arial", Font.PLAIN, 26));
        setResizable(false);
        setTitle("Card Game");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(100, 100, 828, 556);
        contentPane = new JPanel();
        contentPane.setBackground(new Color(102, 153, 153));
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        setContentPane(contentPane);
        contentPane.setLayout(null);


           JLabel lblPlayer = new JLabel("");//here i need to bring the players name
        lblPlayer.setFont(new Font("Arial One", Font.PLAIN, 13));
        lblPlayer.setBounds(6, 11, 49, 16);
        panel.add(lblPlayer);

...more code below not need it now

Thank your for your help 感谢您的帮助

I guess the Player class is your businness object. 我猜Player类是您的业务对象。 Add PropertyChangeSupport to this class, let CardsGameGUI implement PropertyChangeListener and add it as a listener to the player instance. 将PropertyChangeSupport添加到此类,让CardsGameGUI实现PropertyChangeListener并将其作为侦听器添加到播放器实例。 Everytime you change something in the console the player will fire a property change event and you can listen and react in the gui. 每次您在控制台中更改某些内容时,播放器都会触发一个属性更改事件,您可以在gui中进行监听和做出反应。 It's a simple observer pattern. 这是一个简单的观察者模式。

A simple example can be found here: http://examples.javacodegeeks.com/core-java/beans/bean-property-change-event-listener/ 一个简单的例子可以在这里找到: http : //examples.javacodegeeks.com/core-java/beans/bean-property-change-event-listener/

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

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