如何在使用未修饰的JFrame时添加对调整大小的支持? [英] How to add support for resizing when using an undecorated JFrame?

查看:148
本文介绍了如何在使用未修饰的JFrame时添加对调整大小的支持?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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?

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

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


推荐答案

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

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.

这篇关于如何在使用未修饰的JFrame时添加对调整大小的支持?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆