Java Swing - 如何更新 GUI 对象,即。来自同一包中子类的 JTextField 值
我有一个用 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我希望我能解决你的问题。您有一个包含多个子视图的视图组件,并且由于在另一个子视图中进行了更改,因此您想要更新一个视图组件。
因此,您在主视图中为组合框编写一个操作侦听器:
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:
我建议应用 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