栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

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

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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

您要实现的“什么”问题将推动“如何”发展。

例如…

您可以在第一帧中保持对第二帧的引用,并在单击按钮时告诉第二帧发生了更改。

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
第一个,使它可以对它做一些讨厌的事情,例如删除所有组件。

更好的解决方案是提供一系列接口,使两个类可以相互交谈…

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
实际可以访问的内容。尽管可以争论的是
ActionListener
,该组织
Secondframe
可以使用
ActionEvent
源来查找更多信息,但从本质上讲,这是一种不可靠的机制,因为
interface
没有提及应如何实施…

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



转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/495871.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号