Java 2 JPanel in one JFrame layout

Hi I am trying to add 2 JPanels to a JFrame that take the full width and height of the JFrame. I was able to add them with GridBagLayout () but I cannot set the size of the JPanels using setize (). I also tried using ipady and ipadx, although it seemed to work at first after I pressed a few buttons, the whole layout became a mess. Here is my code:
           JFrame tradeframe = new JFrame("Trade");
           JPanel P1panel = new JPanel();         
           P1panel.setBackground(Color.red);
           JPanel P2panel = new JPanel();
           P2panel.setBackground(Color.BLACK);


           tradeframe.setVisible(true);
           tradeframe.setSize(600, 400);
           tradeframe.setResizable(false);
           tradeframe.setLocationRelativeTo(null);
           tradeframe.setLayout(new GridBagLayout());

           P1panel.add(new JButton ("P1 Agree"));

           P2panel.add(new JButton ("P2 Agree"));


           GridBagConstraints a = new GridBagConstraints();
           a.gridx = 0;
           a.gridy = 0;
           a.weightx = 360;
           a.weighty = 300;
           //a.fill = GridBagConstraints.HORIZONTAL;
           tradeframe.add(P1panel , a);

           GridBagConstraints b = new GridBagConstraints();
           b.gridx = 1;
           b.gridy = 0;
           b.weightx = 360;
           b.weighty = 300;
          // b.fill = GridBagConstraints.HORIZONTAL;
           tradeframe.add(P2panel , b);

      

How can I make each JPanel 300px wide and 400px high?

+3


source to share


5 answers


for GridBaglayout you must set

  • fill in

  • anchor

  • weightx and weighty

  • gridx / gridy (depends on orientation)

then it is possible, for example,



import java.awt.*;
import javax.swing.*;
import javax.swing.border.*;

public class BorderPanels extends JFrame {

    private static final long serialVersionUID = 1L;

    public BorderPanels() {
        setLayout(new GridBagLayout());// set LayoutManager
        GridBagConstraints gbc = new GridBagConstraints();
        JPanel panel1 = new JPanel();
        Border eBorder = BorderFactory.createEtchedBorder();

        panel1.setBorder(BorderFactory.createTitledBorder(eBorder, "20pct"));
        gbc.gridx = gbc.gridy = 0;
        gbc.gridwidth = gbc.gridheight = 1;
        gbc.fill = GridBagConstraints.BOTH;
        gbc.anchor = GridBagConstraints.NORTHWEST;
        gbc.weightx = gbc.weighty = 20;
        add(panel1, gbc); // add compoenet to the COntentPane

        JPanel panel2 = new JPanel();
        panel2.setBorder(BorderFactory.createTitledBorder(eBorder, "60pct"));
        gbc.gridy = 1;
        gbc.weightx = gbc.weighty = 60;
        //gbc.insets = new Insets(2, 2, 2, 2);
        add(panel2, gbc); // add component to the COntentPane

        JPanel panel3 = new JPanel();
        panel3.setBorder(BorderFactory.createTitledBorder(eBorder, "20pct"));
        gbc.gridy = 2;
        gbc.weightx = gbc.weighty = 20;
        gbc.insets = new Insets(2, 2, 2, 2);
        add(panel3, gbc);

        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // important
        pack();
        setVisible(true); // important
    }

    public static void main(String[] args) {
        javax.swing.SwingUtilities.invokeLater(new Runnable() { // important

            public void run() {
                BorderPanels borderPanels = new BorderPanels();
            }
        });
    }
}

      

in most cases it is better to use another LayoutManager

+4


source


    JFrame tradeframe = new JFrame("Trade");
    JPanel P1panel = new JPanel();         
    P1panel.setBackground(Color.red);
    JPanel P2panel = new JPanel();
    P2panel.setBackground(Color.BLACK);
    tradeframe.setSize(600, 400);
    tradeframe.setResizable(false);
    tradeframe.setLocationRelativeTo(null);

    Box content = new Box(BoxLayout.X_AXIS);

    P1panel.add(new JButton ("P1 Agree"));

    P2panel.add(new JButton ("P2 Agree"));

    content.add(P1panel);
    content.add(P2panel);

    tradeframe.setContentPane(content);
    tradeframe.setVisible(true);

      



+3


source


Call the method setPreferredSize(new Dimension(int width, int height));

for the panel objects.

+2


source


Here's how to do it:

import java.awt.*;
import javax.swing.*;

public class GridBagLayoutTest
{
    public GridBagLayoutTest()
    {
        JFrame frame = new JFrame("GridBag Layout Test");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setLocationByPlatform(true);

        Container container = frame.getContentPane();
        container.setLayout(new GridBagLayout());

        JPanel leftPanel = new JPanel();
        leftPanel.setBackground(Color.WHITE);
        GridBagConstraints gbc = new GridBagConstraints();
        gbc.gridx = 0;
        gbc.gridy = 0;
        gbc.weightx = 0.5;
        gbc.weighty = 1.0;
        gbc.anchor = GridBagConstraints.FIRST_LINE_START;
        gbc.fill = GridBagConstraints.BOTH;

        container.add(leftPanel, gbc);

        JPanel rightPanel = new JPanel();
        rightPanel.setBackground(Color.BLUE);
        gbc = new GridBagConstraints();
        gbc.gridx = 1;
        gbc.gridy = 0;
        gbc.weightx = 0.5;
        gbc.weighty = 1.0;
        gbc.anchor = GridBagConstraints.FIRST_LINE_END;
        gbc.fill = GridBagConstraints.BOTH;

        container.add(rightPanel, gbc);

        frame.setSize(600, 400);
        frame.setVisible(true);
    }

    public static void main(String... args)
    {
        Runnable runnable = new Runnable()
        {
            public void run()
            {
                new GridBagLayoutTest();
            }
        };
        SwingUtilities.invokeLater(runnable);
    }
}

      

OUTPUT:

GRIDBAG LAYOUT TEST

+2


source


You are using setSize()

instead setPreferredSize()

. The difference is somewhat misleading and I believe this is a gocha in java. More information on what the difference is between them can be found here.

The article I'm linking has some other pitfalls / gotchas and a good read if you're new to Java.

0


source







All Articles