如何在使用未修饰的JFrame时添加对resize的支持?

我想自定义标题栏,最小化,最大化和关闭按钮。 所以我使用了setUndecorated(true); 在我的JFrame上,但我仍然希望能够调整窗口大小。 实现它的最佳方法是什么?

我在RootPane上有一个边框,我可以在Border或RootPane上使用MouseListeners。 有什么建议?

 import 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(); } } 

如您所说,您的根窗格上有边框。 因此,至少有一个位置(在绘制边框的位置下方),其中根窗格是最上面的组件。 因此,您可以添加鼠标侦听器和鼠标移动侦听器。

单击根窗格(并按下鼠标按钮)时,鼠标和动作侦听器将通知您初始和实际鼠标位置。 因此,您可以更新两个值之间偏移的帧大小,从而使您的帧可以resize。

调整组件大小显示了一种方法。

我在RootPane中找到了一个很好的方法,它给了我这个function,所以现在我只需要找出如何自定义标题栏和它上面的按钮。

我添加了this.getRootPane().setWindowDecorationStyle(JRootPane.FRAME); 在我的UndecoratedFrame构造函数中。

在Sexy Swing应用程序 – 统一工具栏和如何控制窗口装饰上还有更多关于此的内容