I would like to customize my titlebar, minimize-, maximize- and the close-button. So I used setUndecorated(true);
on my JFrame, but I still want to be able to resize the window. What is the best way to implement that?
I have a border on the RootPane, and I could use MouseListeners on the Border or the RootPane. Any recommendations?
im开发者_如何转开发port java.awt.Color;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.border.LineBorder;
public class UndecoratedFrame extends JFrame {
private LineBorder border = new LineBorder(Color.BLUE,2);
private JMenuBar menuBar = new JMenuBar();
private JMenu menu = new JMenu("File");
private JMenuItem item = new JMenuItem("Nothing");
public UndecoratedFrame() {
menu.add(item);
menuBar.add(menu);
this.setJMenuBar(menuBar);
this.setUndecorated(true);
this.getRootPane().setBorder(border);
this.setSize(400,340);
this.setVisible(true);
}
public static void main(String[] args) {
new UndecoratedFrame();
}
}
As you said, you have a border on your root pane. As a consequence, there is at least one location (below the palce where your border is drawn) where your root pane is the upmost component. As a consequence, you can add it a mouse listener and a mouse motion listener.
When your root pane is clicked (and the mouse button is pressed), your mouse and motion listeners will inform you of the initial and actual mouse position. As a consequence, you can update your frame size of the offset between both values, making your frame resizable.
Resizing Components shows one way.
I found a nice method in RootPane that gave me this functionality, so now I only have to find out how to customize the titlebar and the buttons on it.
I added this.getRootPane().setWindowDecorationStyle(JRootPane.FRAME);
in my constructor for UndecoratedFrame
.
There is some more reading about this on Sexy Swing App – The Unified Toolbar and How to control window decorations
精彩评论