Java 使用静态类或此引用将数据从一个 Jframe 传输到另一个 jframe?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18580000/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 09:11:06  来源:igfitidea点击:

Transfer data from one Jframe to another jframe using static class or this reference?

javaswing

提问by Ronak Joshi

I have one jFrame and its have one jTextbox and a button. Another jFrame have a single jLabel. I want to bring out the text written in first frame's textbox to second frame's jLabel when button is pressed. And as i have searched about this than i got some unreliable answers. But as per my knowledge it could be done by creating another static class or by calling this reference.

我有一个 jFrame,它有一个 jTextbox 和一个按钮。另一个 jFrame 有一个 jLabel。我想在按下按钮时将第一帧文本框中写入的文本显示到第二帧的 jLabel 中。当我对此进行搜索时,我得到了一些不可靠的答案。但据我所知,可以通过创建另一个静态类或调用此引用来完成。

采纳答案by MadProgrammer

It is a question of "what" you want to achieve that will drive the "how"...

这是一个你想要实现的“什么”的问题,这将推动“如何”......

For example...

例如...

You could maintain a reference to the second frame within the first frame and when the button is clicked, tell the second frame that a change has occurred...

您可以在第一帧中保持对第二帧的引用,当单击按钮时,告诉第二帧发生了变化......

public class FirstFrame extends JFrame {
    // Reference to the second frame...
    // You will need to ensure that this is assigned correctly...
    private SecondFrame secondFrame;
    // The text field...
    private JTextField textField;

    /*...*/

    // The action handler for the button...
    public class ButtonActionHandler implements ActionListener {
        public void actionPerformed(ActionEvent evt) {
            secondFrame.setLabelText(textField.getText());
        }
    }
}

The problem with this is it exposes the SecondFrameto the first, allowing it to do nasty things to it, like remove all the components for example.

这样做的问题是它暴露了SecondFrame第一个,允许它对它做讨厌的事情,例如删除所有组件。

A better solution would be to provide a series of interfaces that would allow the two classes to talk with each other...

更好的解决方案是提供一系列接口,允许两个类相互交谈......

public interface TextWrangler {
    public void addActionListener(ActionListener listener);
    public void removeActionListener(ActionListener listener);
    public String getText();
}

public class FirstFrame extends JFrame implements TextWrangler {
    private JButton textButton;
    private JTextField textField;

    /*...*/

    public void addActionListener(ActionListener listener) {
        textButton.addActionListener(listener);
    }

    public void removeActionListener(ActionListener listener) {
        textButton.removeActionListener(listener);
    }

    public String getText() {
        return textField.getText();
    }
}

public class SecondFrame extends JFrame {
    private JLabel textLabel;
    private JTextField textField;
    private TextWrangler textWrangler;

    public SecondFrame(TextWrangler wrangler) {
        textWrangler = wrangler;
        wrangler.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent evt) {
                textLabel.setText(textWrangler.getText());
            }
        });
        /*...*/
    }
}

This basically restricts what the SecondFramecan actually gain access to. While it can be argued that the ActionListenerin the SecondFramecould use the ActionEventsource to find out more information, by it's nature, it would be an unreliable mechanism, as the interfacemakes no mention of how it should be implemented...

这基本上限制了SecondFrame实际可以访问的内容。虽然可以说ActionListenerinSecondFrame可以使用ActionEvent源来查找更多信息,但从本质上讲,这将是一种不可靠的机制,因为interface它没有提到应该如何实施......

This is a basic example of the Observer Pattern

这是观察者模式的基本示例