Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
510 views
in Technique[技术] by (71.8m points)

java - How to add support for resizing when using an undecorated JFrame?

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();
    }
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...