使用静态类或此引用将数据从一个Jframe传输到另一个jframe? [英] Transfer data from one Jframe to another jframe using static class or this reference?

查看:96
本文介绍了使用静态类或此引用将数据从一个Jframe传输到另一个jframe?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

推荐答案

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

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

例如......

你可以保持对第一帧内第二帧的引用,当按钮是单击,告诉第二帧发生了更改...

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());
        }
    }
}

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

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

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

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());
            }
        });
        /*...*/
    }
}

这基本上限制了 SecondFrame 实际可以访问的内容。虽然可以认为 SecondFrame 中的 ActionListener 可以使用 ActionEvent 来源以查找更多信息,就其本质而言,它将是一个不可靠的机制,因为接口没有提及它应该如何实现......

This basically restricts what the SecondFrame can actually gain access to. While it can be argued that the ActionListener in the SecondFrame could use the ActionEvent source to find out more information, by it's nature, it would be an unreliable mechanism, as the interface makes no mention of how it should be implemented...

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

这篇关于使用静态类或此引用将数据从一个Jframe传输到另一个jframe?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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