如何将数据从另一个类传递到 GUI? [英] How can I pass data from another class to a GUI?

查看:36
本文介绍了如何将数据从另一个类传递到 GUI?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使用 GUI 将数据/值从一个类传递到另一个类?我正在尝试将 message2 数组传递给 GUI 中的 namesOut 标签.我被卡住了,并出现错误.

How do I pass data/values from one class to another using GUI? I'm trying to pass the message2 array to namesOut label in GUI. I'm stuck and getting an error.

这是我的代码:

package testClassesGUI;

import java.awt.BorderLayout;

public class UI extends JFrame {

    private JPanel contentPane;

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

    /**
     * Create the frame.
     */
    public UI() {
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(100, 100, 450, 300);
        contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        setContentPane(contentPane);
        contentPane.setLayout(null);

        JLabel lblDisplayOutNames = new JLabel("Display out names:");
        lblDisplayOutNames.setBounds(32, 25, 121, 16);
        contentPane.add(lblDisplayOutNames);

        JLabel namesOut = new JLabel(""); //here i need to bring the data
        namesOut.setBounds(32, 63, 228, 87);
        contentPane.add(namesOut);
    }
}

逻辑:

这里出现错误.

package testClassesGUI;

public class Logic {

    private String[] someArray = { "Great", "World" };



    // getter method
    public String[] message2(){
        return someArray;
    }


    // setter method
    public void setSomeArray(String[] someArray){
        this.someArray = someArray;
    }

    UI logicObject = new UI();
    logicObject.namesOut.setText(message2); //here my error misplaced construct(s), variable declaratoridexpected
}

非常感谢您的帮助.

推荐答案

把它放在你的 UI 构造函数中.您应该在其中创建一个 Logic 对象

Put this in your UI constructor. You should create a Logic object in it

    public UI() {
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(100, 100, 450, 300);
        contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        setContentPane(contentPane);
        contentPane.setLayout(null);

        JLabel lblDisplayOutNames = new JLabel("Display out names:");
        lblDisplayOutNames.setBounds(32, 25, 121, 16);
        contentPane.add(lblDisplayOutNames);

        JLabel namesOut = new JLabel(""); //here i need to bring the data
        namesOut.setBounds(32, 63, 228, 87);
        contentPane.add(namesOut);

        Logic logic = new Logic();           <<---
        String[] array = logic.message2();       |
                                                 |
        String s = "";                           |
        for (String str : array){                |
            s += str + " ";                      |
        }                                        |
                                                 |
        namesOut.setText(s);                <<----
    }

你可以从你的 Logic 类中删除它

You can delete this from your Logic class

UI logicObject = new UI();
logicObject.namesOut.setText(message2);

这篇关于如何将数据从另一个类传递到 GUI?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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