Java Swing - 如何更新 GUI 对象,即。来自同一包中子类的 JTextField 值

发布于 2024-12-19 05:04:29 字数 211 浏览 6 评论 0原文

我有一个用 Swing 设计的 GUI,其中布局了所有组件。例如,我有一个带有 JList 和 JTextField 的 JComboBox,

当我从 JComboBox 选择不同的项目时,我尝试使用 ListSelectionListener 调用子类中的方法以根据选择更新 JTextField,

我将如何处理这样做正确吗?如何调用子类,然后从子类更新 GUI 对象的值?

I have a GUI designed in Swing with all of the Components laid out. For example I have a JComboBox with a JList and a JTextField,

When I select a different item from the JComboBox I am trying to use a ListSelectionListener to call a method in a subclass to update the JTextField based on the choice,

How would I go about doing that properly? How do I call the subclass and then from the subclass update the GUI object's value?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

森罗 2024-12-26 05:04:29
public class Parent {

    private void init() {
        // ...
        combo.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                Object selected = combo.getSelectedItem();
                textField.setText(getTextBasedOnSelection(selected));
            }
        });
        // ...
    }

    /**
     * Returns the text to display when the given object is selected.
     * Subclasses may override this method to display what they want
     */
    protected String getTextBasedOnSelection(Object selected) {
        return selected.toString();
    }
    // ...
}
public class Parent {

    private void init() {
        // ...
        combo.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                Object selected = combo.getSelectedItem();
                textField.setText(getTextBasedOnSelection(selected));
            }
        });
        // ...
    }

    /**
     * Returns the text to display when the given object is selected.
     * Subclasses may override this method to display what they want
     */
    protected String getTextBasedOnSelection(Object selected) {
        return selected.toString();
    }
    // ...
}
百思不得你姐 2024-12-26 05:04:29

我希望我能解决你的问题。您有一个包含多个子视图的视图组件,并且由于在另一个子视图中进行了更改,因此您想要更新一个视图组件。

因此,您在主视图中为组合框编写一个操作侦听器:

comboBox.addActionListener(new ActionListener(){

            @Override
            public void actionPerformed(ActionEvent arg0) {
                textField.setText(comboBox.getSelectedItem());
            }

        });

I hope I get your problem right. You have a View Component with several subviews and you want to update one because of the changes done inside the other one.

Therefore you write an action listener for your combobox in the main View:

comboBox.addActionListener(new ActionListener(){

            @Override
            public void actionPerformed(ActionEvent arg0) {
                textField.setText(comboBox.getSelectedItem());
            }

        });
故事和酒 2024-12-26 05:04:29

我建议应用 Mediator 模式,而不是直接互连组件:
创建 JPanel 的子类(例如 XyzPane),将所有组件放入其中。该类将成为 Mediator。它

  • 监听其组件的事件,
  • 根据需要更新组件
  • ,如果需要,会触发自己的事件(这允许它成为父中介器的一部分:将组件分组到窗格中,然后嵌套窗格)

Instead of inter-connecting components directly, I recommend to apply the Mediator pattern:
Create a subclass of JPanel (e.g. XyzPane) where you put all your components in. This class becomes the Mediator. It

  • listens for events of its components
  • updates the components as needed
  • fires its own events, if needed (this allows it to be a part of a parent Mediator: grouping components in Panes and then nesting the Panes)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文