问题
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