简体   繁体   中英

how to make a java swing JFrame resize correctly

I'm wondering why I can't resize my Frame below and have all of the components resize smaller and bigger as I resize frame. Thank you!!!

public class TPASimulatorGUI extends JFrame{
    JPanel mainPanel = new JPanel();
    BoxLayout layout = new BoxLayout(mainPanel,BoxLayout.Y_AXIS);       
    mainPanel.setLayout(layout);

                // add things to main panel

    JPanel it = new JPanel(new FlowLayout(FlowLayout.LEADING));

    it.add(mainPanel);

    this.getContentPane().add(it);
    this.setSize(new Dimension(1190,770));
    this.setVisible(true);

}

JPanel it = new JPanel(new FlowLayout(FlowLayout.LEADING));
it.add(mainPanel);

A FlowLayout always respects the preferred size of the components added to it.

Get rid of the "it" panel and try just using

//this.getContentPane().add(it);
add(mainPanel);

The default layout for a frame is a BorderLayout which will try to increase/decrease the size of all components added to it.

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