简体   繁体   English

如何将自定义菜单操作和标题栏图标添加到JFrame

[英]How to add custom menu action and titlebar icon to JFrame

I have a custom JFrame. 我有一个自定义的JFrame。 On the title bar I have an icon in the top left, a title, and then the standard minimize, maximize, and close buttons on the right. 在标题栏上,我在左上方有一个图标,一个标题,然后在右侧有标准的最小化,最大化和关闭按钮。

When I click the icon in the title bar I get the standard options: Restore, Move, Size, Minimize, Maximize, Close. 单击标题栏中的图标时,将获得标准选项:“还原”,“移动”,“大小”,“最小化”,“最大化”,“关闭”。

How can I add my own menu option here? 如何在此处添加自己的菜单选项? I'd like to add and "Always On Top" option here. 我想在这里添加和“始终放在最前面”选项。

Additionally I'd like to add a button next to the max,min,close buttons on the title bar to allow uses to toggle the "Always On Top" state of the JFrame. 另外,我想在标题栏的max,min,close按钮旁边添加一个按钮,以允许使用它来切换JFrame的“ Always On Top”状态。

You could create your own customized Components. 您可以创建自己的自定义组件。 To do that, create a new class which extends JMenuBar for example and override the methods which fit your needs. 为此,请创建一个新类,该类例如extends JMenuBar并覆盖适合您需要的方法。 Very often, for example, one wants to override paintComponent(Graphics) . 例如,很多时候,您想覆盖paintComponent(Graphics)

That is not what you want, 'though. 那不是你想要的,虽然。 Customizing the JMenuBar wont work as you expect it to. 自定义JMenuBar不会像您期望的那样工作。 The "JMenuBar" is another bar below the title bar. “ JMenuBar”是标题栏下方的另一个栏。 I am Mac user, but as far as my knowledge goes, it is not possible to customize the title bar, because that isn't handled by the JVM. 我的Mac用户,但据我所知推移,它不是可以自定义标题栏,因为这不是由JVM处理。 The only thing that is modifiable without using native code is the Icon in the top left. 无需使用本机代码即可修改的唯一东西是左上方的Icon。

For further information on that, look at this question and the best answer there. 有关此问题的更多信息,请查看此问题以及那里的最佳答案。 This will help you a lot. 这将对您有很大帮助。

Your problem (adding a button at the top for toggling the alwaysOnTop status) is best solved by creating normal instances of a JMenuBar, a JMenu and a JMenuItem. 通过创建JMenuBar,JMenu和JMenuItem的常规实例,可以最好地解决您的问题(在顶部添加一个按钮来切换alwaysOnTop状态)。 To then add that MenuBar to your Frame, use JFrame.setJMenuBar(JMenuBar) . 然后将该MenuBar添加到Frame中,请使用JFrame.setJMenuBar(JMenuBar) See also How to use Menus . 另请参阅如何使用菜单

I hope this helps! 我希望这有帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM