JInternalFrame选择
我有一个包含一些 JInternalFrames 的 JDesktopPane。我希望仅当选择 JInternalFrames 之一时才激活菜单栏上的某些菜单。我尝试使用 VetoableChangeListener,其中包含以下代码:
JInternalFrame selectedFrame = desk.getSelectedFrame();
if ((selectedFrame != null)) {
imageMenu.setEnabled(Boolean.TRUE);
} else {
imageMenu.setEnabled(Boolean.FALSE);
}
但结果不是我所期望的 - 例如,仅在我第二次添加框架时才启用菜单。当我关闭所有框架时,它保持启用状态。
我怎样才能做到这一点?
I have a JDesktopPane containing some JInternalFrames. I want some menus on the menubar to be activated only when one of the JInternalFrames is selected. I've tried using VetoableChangeListener, with the following code in it:
JInternalFrame selectedFrame = desk.getSelectedFrame();
if ((selectedFrame != null)) {
imageMenu.setEnabled(Boolean.TRUE);
} else {
imageMenu.setEnabled(Boolean.FALSE);
}
But the results are not what I expected - for example, the menu is enabled only the second time I add a frame. when I close all frames, it remains enabled.
How can I make this work?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您必须阅读有关 JInternalFrames 的基本教程,其中包含指向 < a href="http://download.oracle.com/javase/tutorial/uiswing/events/internalframelistener.html" rel="nofollow">InternalFrameListener,
但另一个看起来更好方法是以编程方式了解所有情况下的这些事件,并且每次都通过添加 PropertyChangeListener 如示例所示 获取JDesktopPane 容器中的所有框架,通过添加 PropertyChangeListener可以监听 这些事件
you have to read basic tutorial about JInternalFrames with link to the InternalFrameListener,
but another and look like as better way is programatically to know those event in all cases and evety times is by adding PropertyChangeListener as shows examples Getting All Frames in a JDesktopPane Container, by adding PropertyChangeListener you can listeng for these events
将
InternalFrameListener
添加到添加到桌面窗格的每个内部框架,并且每次触发事件时,执行您在问题中显示的代码。不过,这段代码可以写得更好:
setEnabled
采用原始布尔值作为参数,而不是java.lang.Boolean
。使用true
和false
而不是Boolean.TRUE
和Boolean.FALSE
。(selectedFrame != null)
计算结果为布尔值。只需编写imageMenu.setEnabled(selectedFrame != null);
而不是
Add an
InternalFrameListener
to each internal frame added to the desktop pane, and each time an event is triggered, execute the code you have shown in your question.This code could be better written though:
setEnabled
takes a primitive boolean as argument, not ajava.lang.Boolean
. Usetrue
andfalse
rather thanBoolean.TRUE
andBoolean.FALSE
.(selectedFrame != null)
evaluates as a boolean. Just writeimageMenu.setEnabled(selectedFrame != null);
instead of
我只是创建一个自定义事件并在
时触发它
JInternalFrame
获取 焦点(isActivated
)。菜单项将侦听此事件,拦截它并相应地设置其状态为启用或禁用。
这里的优点是您不必处理哪些菜单项应可用于哪些类型的内部框架,只需触发适当的事件即可。如果您将来添加更多内部框架,将使您的生活更轻松。
I would just create a custom event and fire it when a
JInternalFrame
gets focus (isActivated
).The menu items would listen for this event, intercept it and set their status enabled or disabled accordingly.
The advantage here is that you don't have to handle what menu items should be available for which types of internal frames, just fire the appropriate event. It'll make your life easier if you add more internal frames in the future.
这个答案基于@mKorbel 的答案。此示例显示了检测内部框架之间焦点的方法之一,如下所示:
This answer is based on the answer by @mKorbel. This example shows one of the ways to detect focus between internal frames as is demonstrated here: