JDesktopPane - 如何获取活动框架

时间:2022-07-06 07:32:53

How to get active (having focus) frame (JInternalFrame) that is inside JDesktopPane? I need it for my MDI notepad (not that anybody would use that, just a training project). Looking at api, I see only functions to get all JInternalFrames, not active one.

如何在JDesktopPane中获得活动(具有焦点)框架(JInternalFrame)?我需要它用于我的MDI记事本(不是任何人都会使用它,只是一个培训项目)。看看api,我只看到函数来获取所有JInternalFrame,而不是活动的。

3 个解决方案

#1


11  

Use JDekstopPane.getSelectedFrame() method (From doc: currently active JInternalFrame in this JDesktopPane, or null if no JInternalFrame is currently active.) or JDesktopPane.getAllFrames() to get list of all JInternalFrames currently displayed in the desktop and check isSelected() method.

使用JDekstopPane.getSelectedFrame()方法(来自doc:此JDesktopPane中当前活动的JInternalFrame,如果当前没有JInternalFrame当前处于活动状态,则返回null。)或JDesktopPane.getAllFrames()获取当前显示在桌面中的所有JInternalFrame的列表并检查isSelected()方法。

#2


2  

Make a List<JInternalFrame> and check isSelected() as you iterate though it.

创建一个List 并在迭代时检查isSelected()。

Addendum: See also this example that uses Action to select an internal frame from a menu.

附录:另请参阅此示例,该示例使用“操作”从菜单中选择内部框架。

#3


2  

Have you looked at the Java tutorial titled How to Use Internal Frames? In your code you need an InternalFrameListener (API) (Tutorial) and listen to activate/deactivate events. Activated means the internal frame was brought to the top; deactivated means it's no longer on top. Since JDesktopPane extends JLayeredPane you can also set the z-order of components added to it.

你看过标题为如何使用内部框架的Java教程吗?在您的代码中,您需要一个InternalFrameListener(API)(教程)并监听激活/停用事件。激活意味着内部框架被带到顶部;停用意味着它不再是最重要的。由于JDesktopPane扩展了JLayeredPane,您还可以设置添加到其中的组件的z顺序。

Don't iterate over all the panes - use events.

不要遍历所有窗格 - 使用事件。

If for some reason you prefer to poll your UI rather than use an event-driven approach you can call getSelectedFrame which returns the active JInternalFrame. I'm not sure why no one else mentioned it.

如果由于某种原因您更喜欢轮询UI而不是使用事件驱动方法,则可以调用getSelectedFrame来返回活动的JInternalFrame。我不确定为什么没有人提到它。

#1


11  

Use JDekstopPane.getSelectedFrame() method (From doc: currently active JInternalFrame in this JDesktopPane, or null if no JInternalFrame is currently active.) or JDesktopPane.getAllFrames() to get list of all JInternalFrames currently displayed in the desktop and check isSelected() method.

使用JDekstopPane.getSelectedFrame()方法(来自doc:此JDesktopPane中当前活动的JInternalFrame,如果当前没有JInternalFrame当前处于活动状态,则返回null。)或JDesktopPane.getAllFrames()获取当前显示在桌面中的所有JInternalFrame的列表并检查isSelected()方法。

#2


2  

Make a List<JInternalFrame> and check isSelected() as you iterate though it.

创建一个List 并在迭代时检查isSelected()。

Addendum: See also this example that uses Action to select an internal frame from a menu.

附录:另请参阅此示例,该示例使用“操作”从菜单中选择内部框架。

#3


2  

Have you looked at the Java tutorial titled How to Use Internal Frames? In your code you need an InternalFrameListener (API) (Tutorial) and listen to activate/deactivate events. Activated means the internal frame was brought to the top; deactivated means it's no longer on top. Since JDesktopPane extends JLayeredPane you can also set the z-order of components added to it.

你看过标题为如何使用内部框架的Java教程吗?在您的代码中,您需要一个InternalFrameListener(API)(教程)并监听激活/停用事件。激活意味着内部框架被带到顶部;停用意味着它不再是最重要的。由于JDesktopPane扩展了JLayeredPane,您还可以设置添加到其中的组件的z顺序。

Don't iterate over all the panes - use events.

不要遍历所有窗格 - 使用事件。

If for some reason you prefer to poll your UI rather than use an event-driven approach you can call getSelectedFrame which returns the active JInternalFrame. I'm not sure why no one else mentioned it.

如果由于某种原因您更喜欢轮询UI而不是使用事件驱动方法,则可以调用getSelectedFrame来返回活动的JInternalFrame。我不确定为什么没有人提到它。