我正在寻找是否可以在当前JFrame
的Winwdows系统菜单中添加一个项目(“关于”应用程序)。
正如 that answer 中所建议的那样,我知道可以通过 JNI/JNA 和 Windows API 使用 GetSystemMenu()
调整菜单,但我正在寻找是否可以在普通 Java/Swing 中实现。
我在 that forum thread 中发现了一些东西,表明它应该是可能的。 :
this prior to creating a JFrame
JFrame.setDefaultLookAndFeelDecorated(true);
then, before adding anything to the frame, iterate the frame's components, looking for the JMenu. When found, add your 'about' menuItem
just tested, works fine.
唉,没有提供完整的 sscce,而且我没有设法让它在我的代码中工作。在创建窗口之后添加组件之前设置断点时,我看不到 JMenu
。既不在第一层 (JRootPane
),也不在连续嵌入的组件 (JPanel
、JLayeredPane
) 中。
如果重要的话,我正在 WinXP 和 Win7 上处理 Java 6/7,窗口被创建为 new JFrame("some title")
并且我正在使用 native 外观和感觉UIManager.getSystemLookAndFeelClassName()
。
最佳答案
使用跨平台外观时,JMenu 组件确实存在。组件的层次结构是:
class Test
class javax.swing.JRootPane
class javax.swing.JPanel
class javax.swing.JLayeredPane
class javax.swing.JPanel
class javax.swing.plaf.metal.MetalTitlePane
class javax.swing.plaf.metal.MetalTitlePane$SystemMenuBar
class javax.swing.JMenu
class javax.swing.JButton
class javax.swing.JButton
class javax.swing.JButton
不幸的是,当使用 Windows 外观时,Java 根本不知道标题栏。您可能陷入使用 JNA 或不同 laf 的困境。
关于java - 无需 winapi 将项目添加到 Windows 系统菜单,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12815659/