Swing:JDialogs 如何与 JFrame 对话 [英] Swing: How JDialogs talk back to the JFrame

查看:25
本文介绍了Swing:JDialogs 如何与 JFrame 对话的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在构建我的第一个 Swing 应用程序,并试图弄清楚我的 JDialogs(在用户选择 JMenuItem 时专门调用)如何更新 JFrame 的主客户区中的组件,JFrame 是其父窗口"整个应用.

I'm building my first Swing app and am trying to figure out how my JDialogs - exclusively invoked when the user selects a JMenuItem - can update the components in the main client area of the JFrame which is the parent "window" of the whole app.

这是我想出的设计,但不知道它是否:(1) 很糟糕,(2) 不是标准的(因此最好的)方式,或者 (3) 如果我完全这里离基地.非常感谢任何建议.

This is the design I've come up with, but don't know if its: (1) just plain bad, (2) not the standard (thus best) way, or (3) if I'm totally off-base here. Any suggestions are enormously appreciated.

基本上,用户选择一个 JMenuItem,它会启动一个 JDialog.用户与对话框上的组件交互,然后单击完成".如果一切都通过验证,JDialog 将关闭,并且我希望父窗口(一个 JFrame)更新其状态(并最终波及到其组件中).

Basically, the user selects a JMenuItem, which launches a JDialog. The user interacts with the components on the dialog, and then clicks "Done". If everything validates, the JDialog closes, and I want the parent window (a JFrame) to have its state updated (and eventually rippled out into its components).

我的设计:

有一个 AppStateController,它是 JFrame 子类(我的应用程序)的成员.然后,我将创建一个 AppStateChangeListenerAppStateChange EventObject 子类,以便每当对话框验证和关闭时,它都会触发 AppStateChange 事件.由于父 JFrame 是该事件的唯一注册侦听器,因此我可以定义一个处理程序来获取传递给它的事件.我会确保 AppStateChangeEvent 有足够的元数据来描述所有可能的更改.

Have an AppStateController that is a member of the JFrame subclass (my application). I would then create an AppStateChangeListener and AppStateChange EventObject subclass so that whenever a dialog validates and closes, it fires an AppStateChange event. Since the parent JFrame is the only registered listener to that event, I could define a handler that gets the event passed to it. I would make sure the AppStateChangeEvent had enough metadata to describe all the possible changes.

理论上,我喜欢这种方法:每次触发不同的事件时,它应该是干净的并且没有对多个控件的意大利面条"类型的调用.不过,我担心这可能有点矫枉过正.

In theory, I like this approach: it should be clean and free of "spaghetti"-type calls to multiple controls every time a different event fires. However, I fear it may be overkill.

这里的最佳做法是什么?我不是一个真正的 GUI 人!

What do best practices dictate here? I'm not really a GUI person!

推荐答案

Java 有几种方法来实现 观察者模式;此处讨论了几个.

Java has several ways to implement the observer pattern; several are discussed here.

规定的机制EventListenerList 可能是最通用的,因为它允许您定义自己的事件和侦听器类型,并且 Swing 程序员对它很熟悉.与其让 JFrame 成为监听器,不如让最高级别的 JComponent 这样做.JComponent<的每个实例/a> 有一个合适的 protected 成员,listenerList.

The mechanism prescribed by EventListenerList is probably the most general, as it would allow you to define your own event and listener types, and it is familiar to Swing programmers. Instead of making the JFrame a listener, let the highest level JComponent do so. Each instance of JComponent has a suitable protected member, listenerList.

绑定属性也是一个很好的选择,如此处所示.

如果你使用 Observable,你需要使用委托.

If you go with Observable, you'll need to use a delegate.

附录:作为具体示例, 使用用于管理图表、数据集和系列事件的 EventListenerList 方案.相比之下, 使用 bean 属性 通知侦听器小部件选择.

Addendum: As concrete examples, jfreechart uses the EventListenerList scheme to manage chart, dataset and series events. In contrast, jcalendar uses bean properties to notify listeners of widget selections.

这篇关于Swing:JDialogs 如何与 JFrame 对话的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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