从另一个类访问GUI组件 [英] Access GUI components from another class

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

问题描述

我是Java的新手,我已经碰到了一堵砖墙。我想从另一个类访问GUI组件(在一个类中创建)。我正在从一个类创建一个新的GUI类,如此;

I'm new to Java and I've hit a brick wall. I want to access GUI components (that have been created in one class) from another class. I am creating a new GUI class from one class, like so;

GUI gui = new GUI();

我可以访问该类中的组件,但是当我去另一个班级时我不能。我真的只需要访问 JTextAreas 来更新他们的内容。有人能指出我正确的方向,任何帮助都非常感谢。

and I can access the components in that class, but when I go to a different class I cant. I really just need to access the JTextAreas to update their content. Could someone point me in the right direction please, any help is greatly appreciated.

GUI 等级:

public class GUI {

    JFrame frame = new JFrame("Server");        
    ...
    JTextArea textAreaClients = new JTextArea(20, 1);  
    JTextArea textAreaEvents = new JTextArea(8, 1);

    public GUI()
    {
        frame.setLayout(new FlowLayout(FlowLayout.LEADING, 5, 3));     
        ...
        frame.setVisible(true);
    }
}


推荐答案

首先尊重封装规则。使您的字段私有。接下来,您需要 getters 以获取您需要访问的字段。

First respect encapsulation rules. Make your fields private. Next you want to have getters for the fields you need to access.

public class GUI {
    private JTextField field = new JTextField();

    public GUI() {
        // pass this instance of GUI to other class
        SomeListener listener = new SomeListener(GUI.this);
    }

    public JTextField getTextField() {
        return field;
    }
}

然后你会想要把你的GUI传递给任何东西class需要访问文本字段。说一个 ActionListener 类。使用构造函数注入(或传递引用)来传递 GUI 类。执行此操作时, SomeListener 中引用的 GUI 是相同的,并且您永远不会创建一个新的(不会引用你需要的相同实例)。

Then you'll want to pass your GUI to whatever class needs to access the text field. Say an ActionListener class. Use constructor injection (or "pass reference") for the passing of the GUI class. When you do this, the GUI being referenced in the SomeListener is the same one, and you don't ever create a new one (which will not reference the same instance you need).

public class SomeListener implements ActionListener {
    private GUI gui;
    private JTextField field;

    public SomeListener(GUI gui) {
        this.gui = gui;
        this.field = gui.getTextField();
    }
}






虽然上面的可以工作,但它可能是不必要的。首先考虑一下你想要对文本字段做什么。如果某些操作可以在GUI类中执行,但您只需要访问类中的某些内容来执行它,您可以使用一个方法实现接口需要做点什么。像这样的东西


Though the above may work, it may be unnecesary. First think about what exactly it is you want to do with the text field. If some some action that can be performed in the GUI class, but you just need to access something in the class to perform it, you could just implement an interface with a method that needs to perform something. Something like this

public interface Performable {
    public void someMethod();
}

public class GUI implements Performable {
    private JTextField field = ..

    public GUI() {
        SomeListener listener = new SomeListener(GUI.this);
    }

    @Override
    public void someMethod() {
         field.setText("Hello");
    }
}

public class SomeListener implements ActionListener {
    private Performable perf;

    public SomeListener(Performable perf) {
        this.perf = perf;
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        perf.someMethod();
    }
}

这篇关于从另一个类访问GUI组件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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