简体   繁体   中英

JFrame doesn't conform JPanel's setMinimunSize()

I expect JPanel 's setMinimumSize() to confine the resizing of JFrame too, but it doesn't.

The following is my example code:

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JSplitPane;

public class AutoResize{

    public static void main(String[] args) {

        JPanel leftPanel = new JPanel();
        JPanel rightPanel = new JPanel();
        leftPanel.setBackground(Color.RED);
        rightPanel.setBackground(Color.BLUE);
        leftPanel.setSize(500,400);
        rightPanel.setSize(500,400);
        Dimension d = new Dimension(450,300);
        leftPanel.setMinimumSize(d);
        rightPanel.setMinimumSize(d);

        JSplitPane split;
        split = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, leftPanel, rightPanel);
        split.setDividerLocation(400);

        JFrame frame = new JFrame();
        frame.getContentPane().setLayout(new BorderLayout());
        frame.getContentPane().add(split, BorderLayout.CENTER);
        frame.setSize(1000, 800);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);
    }
}

If you resize JFrame into a very small size it produces this: 怪异的行为

What I want is that JFrame couldn't be resized into a smaller region than JPanel s' minimum size. Is there anyway to implement this?

All I need is adding frame.setMinimumSize(); , I feel dumb.

Thanks @Andrew Thompson @MadProgrammer and @user1803551

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Toolkit;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JSplitPane;

public class AutoResize{

    private final Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
    private final double w = screenSize.getWidth();
    private final double h = screenSize.getHeight();
    private final Dimension d = new Dimension((int) w/3,(int) h/3);

    public void run() {
        JPanel leftPanel = new JPanel();
        JPanel rightPanel = new JPanel();
        leftPanel.setBackground(Color.RED);
        rightPanel.setBackground(Color.BLUE);
        //leftPanel.setSize(500,400);
        //rightPanel.setSize(500,400);
        leftPanel.setMinimumSize(d);
        rightPanel.setMinimumSize(d);

        JSplitPane split;
        split = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, leftPanel, rightPanel);
        split.setDividerLocation(400);

        JFrame frame = new JFrame();
        frame.getContentPane().setLayout(new BorderLayout());
        frame.getContentPane().add(split, BorderLayout.CENTER);

        frame.setSize((int) w,(int) h);
        //frame.pack();
        frame.setMinimumSize(new Dimension((int) w/3*2,(int) h/3*2));
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        AutoResize test = new AutoResize();
        test.run();
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM