How to add any Component in the JFrame TitleBar

狂风中的少年 提交于 2021-01-29 02:39:24

问题


How to add any Component like a JButton on the JFrame TitleBar in java swing.


回答1:


I assume by "title bar" you think of the header with the window menu button, document or program title and buttons for (typical) maximize, minimize, close.

The window borders are drawn by the operating system (whatever part, e.g. with XServer it would be a window manager most of the time) - usually. And this is what the user wants - usually. Then this part of the window is outside the reach of Swing, you need to add components by native code (JNI or one of its nicer wrappers like JNA).

But you can set a Frame to undecorated via setUndecorated. This leaves the whole window area in your responsibility, you have to draw everything yourself and care for things like draggability of the window. Think of a completely "skinned" application. If the look and feel you use supports drawing of the windows borders (javax.swing.LookAndFeel#getSupportsWindowDecorations()) you don't have to do this yourself but set JFrame.setDefaultLookAndFeelDecorated(), the LookAndFeel will draw the border with Swing components. Then you can add Swing components either by glasspane or by changing the component that draws the title bar.



来源:https://stackoverflow.com/questions/9615088/how-to-add-any-component-in-the-jframe-titlebar

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!