Java – how to add support for resizing when using an undecorated JFrame?

I want to customize my title bar, minimize, maximize and close buttons So I use setundecorated (true); On my JFrame, but I still want to be able to resize the window What is the best implementation method?

I have a border on the rootpane. I can use mouselistener on border or rootpane Any suggestions?

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

Solution

As you said, your root pane has a border Therefore, in at least one location (below your border drawing), your root pane is the most important component Therefore, you can add mouse listeners and mouse movement listeners

When your root pane is clicked (and the mouse button is pressed), your mouse and action listener will notify you of the initial and actual mouse position Therefore, you can update the frame size of the offset between the two values so that your frame can be resized

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>