简体   繁体   中英

Java 2 JPanel's in one JFrame layout

Hi I am trying to add 2 JPanel's to a JFrame that take the full width and height of the JFrame.I managed to add them with GridBagLayout() but I can't seem to set the size of the JPanels using the setsize().I have also tryied to used ipady and ipadx while that seemed to work at first after I aded some 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 that each JPanel is 300px width and 400px in height?

for GridBaglayout you have to set

  • fill

  • anchor

  • weightx and weighty

  • gridx / gridy (depend of orientations)

then 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();
            }
        });
    }
}

on most cases will be better use another LayoutManager

    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);

Invoke setPreferredSize(new Dimension(int width, int height)); method on your panel objects.

Here is the way to do that :

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 :

网格布局测试

You are using setSize() instead of setPreferredSize() . The difference is somewhat misleading and I would consider it a gotcha in java. Some more information about what the difference between the two can be found here.

The article I link has some other pitfalls/gotchas and a useful read if you are new to Java.

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