ulw1 ulw1 - 11 days ago 6
Java Question

How to force JPanels using GridBagLayout to resize with JFrame

I need the components of my menuPanel to expand when the frame expands. MenuPanel is using GridBayLayout, and I've tinkered with both the fill and weight values for the panel, but nothing really seems to cause the components to resize with the frame.

I suspect something is wrong with the way I'm adding menuPanel in the display class. Furthermore, I also tried setting a layout to TanksApplication, and that didn't work either. I'm not understanding something fundamental, I think.

Main app

public class TanksApplication extends JFrame{

public TanksApplication(){
super("Tanks");
}

public static void main (String args[]){
Display display = new Display();
TanksApplication frame = new TanksApplication();
frame.add(display);
frame.setSize(600,425);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLocationRelativeTo(null);
}
}


Display class

public class Display extends JPanel{
public Display(){
menuPanel = new JPanel(new GridBagLayout());
GridBagConstraints con = new GridBagConstraints();

mainMenuImageP = new JPanel();
mainMenuImageP.setBackground(Color.BLACK);
con.fill = GridBagConstraints.BOTH;
con.gridy = 0;
con.gridx = 0;
con.gridwidth = 2;
con.weightx = 0.5;
con.weighty = 0.5;
con.anchor = GridBagConstraints.CENTER;
con.ipadx = 400;
con.ipady = 300;
con.insets = new Insets(0,20,0,20);
menuPanel.add(mainMenuImageP, con);

newGameB = new JButton("New Game");
con.fill = GridBagConstraints.HORIZONTAL;
con.gridy = 1;
con.gridx = 0;
con.gridwidth = 1;
con.weightx = 0.5;
con.weighty = 0.5;
con.anchor = GridBagConstraints.CENTER;
con.ipadx = 0;
con.ipady = 0;
con.insets = new Insets(10,10,10,10);
menuPanel.add(newGameB, con);

loadGameB = new JButton("Load Game");
con.fill = GridBagConstraints.HORIZONTAL;
con.gridy = 1;
con.gridx = 1;
con.gridwidth = 1;
con.weightx = 0.5;
con.weighty = 0.5;
con.anchor = GridBagConstraints.CENTER;
con.ipadx = 0;
con.ipady = 0;
con.insets = new Insets(10,10,10,10);
menuPanel.add(loadGameB, con);

add(menuPanel);
}

Answer

Your problem is that you're adding your GridBagLayout using menuPanel into the Display JPanel, the latter of which uses JPanel's default FlowLayout. FlowLayout will not resize components added to it. The solution: get rid of menuPanel and just use the Display JPanel. Give it a GridBagLayout and add the components to it.

so have within your Display's constructor

setLayout(new GridBagLayout());

delete the JPanel menuPanel = new JPanel(); and anywhere you have menuPane.add(...);, change it to simply add(...);

e.g.,

public class Display extends JPanel{    
    public Display(){
        // !! menuPanel = new JPanel(new GridBagLayout());
        GridBagConstraints con = new GridBagConstraints();

        mainMenuImageP = new JPanel();
        mainMenuImageP.setBackground(Color.BLACK);
        con.fill = GridBagConstraints.BOTH;
        con.gridy = 0;
        con.gridx = 0;
        con.gridwidth = 2;
        con.weightx = 0.5;
        con.weighty = 0.5;
        con.anchor = GridBagConstraints.CENTER;
        con.ipadx = 400;
        con.ipady = 300;
        con.insets = new Insets(0,20,0,20);
        // !! menuPanel.add(mainMenuImageP, con);
        add(mainMenuImageP, con); // !!

        newGameB = new JButton("New Game");
        con.fill = GridBagConstraints.HORIZONTAL;
        con.gridy = 1;
        con.gridx = 0;
        con.gridwidth = 1;
        con.weightx = 0.5;
        con.weighty = 0.5;
        con.anchor = GridBagConstraints.CENTER;
        con.ipadx = 0;
        con.ipady = 0;
        con.insets = new Insets(10,10,10,10);
        // !! menuPanel.add(newGameB, con);
        add(newGameB, con); // !!

        loadGameB = new JButton("Load Game");
        con.fill = GridBagConstraints.HORIZONTAL;
        con.gridy = 1;
        con.gridx = 1;
        con.gridwidth = 1;
        con.weightx = 0.5;
        con.weighty = 0.5;
        con.anchor = GridBagConstraints.CENTER;
        con.ipadx = 0;
        con.ipady = 0;
        con.insets = new Insets(10,10,10,10);
        // !! menuPanel.add(loadGameB, con);
        add(loadGameB, con); // !!

        // !! add(menuPanel);
    }

}

Note that if this doesn't help, then please consider creating and posting a minimal example program or SSCCE.

Comments