使用未修饰的 JFrame 时如何添加对调整大小的支持?
我想自定义我的标题栏、最小化、最大化和关闭按钮。因此,我在 JFrame 上使用了 setUndecorated(true);
,但我仍然希望能够调整窗口大小。实现这一目标的最佳方法是什么?
我的 RootPane 上有一个边框,我可以在边框或 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();
}
}
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?
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();
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
正如您所说,您的根窗格上有一个边框。因此,至少有一个位置(在绘制边框的位置下方)根窗格是最上面的组件。因此,您可以为其添加鼠标侦听器和鼠标运动侦听器。
单击根窗格(并按下鼠标按钮)时,鼠标和运动侦听器将通知您初始和实际的鼠标位置。因此,您可以更新两个值之间的偏移量的帧大小,从而使帧的大小可调。
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.
我在 RootPane 中找到了一个很好的方法给了我这个功能,所以现在我只需要了解如何自定义标题栏及其上的按钮。
我在
UndecoratedFrame
的构造函数中添加了this.getRootPane().setWindowDecorationStyle(JRootPane.FRAME);
。在 Sexy 上有更多关于此内容的阅读Swing 应用程序 – 统一工具栏 和如何控制窗口装饰
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 forUndecoratedFrame
.There is some more reading about this on Sexy Swing App – The Unified Toolbar and How to control window decorations