如何将数据从另一个类传递到GUI?

时间:2013-12-13 12:55:01

标签: java swing user-interface

如何使用GUI将数据/值从一个类传递到另一个类?我正在尝试将message2数组传递给GUI中的namesOut标签。我陷入困境并且出错了。

这是我的代码:

GUI

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
}

非常感谢您的帮助。

3 个答案:

答案 0 :(得分:2)

将它放在UI构造函数中。您应该在其中创建一个Logic对象

    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班级

中删除此内容
UI logicObject = new UI();
logicObject.namesOut.setText(message2);

答案 1 :(得分:0)

namesOut是在UI的构造函数中声明的局部变量。您无法从Logic访问它。将其声明为公共成员变量

public JLabel namesOut;

答案 2 :(得分:0)

您必须定义一个功能。也许是一个构造函数:

Logic () {
    logicObject.namesOut.setText(message2);
}

你也可以在代码块内部执行,但这不常见:

{
    logicObject.namesOut.setText(message2);
}