简体   繁体   中英

Java gridbaglayout adjusting

This is an extension to this question: Java gridbaglayout problems . Panel1 is overlapping panel2 because of the panel1's width overflow. However, if i remove the gc.gridwidth = 2 it will align panel2 properly, but it will also move the combobox to the its original spot on the right side. I have been playing around with the different gridbaglayout properties but can't get it aligned like i want it to.

Heres my code:

public void createGUI()
{
   main_panel = new JPanel();
   main_panel.setLayout(new GridBagLayout());
   GridBagConstraints gc = new GridBagConstraints();

   gc.fill = GridBagConstraints.HORIZONTAL;
   gc.gridx = 0;
   gc.gridy = 0;
   gc.insets = new Insets(5, 0, 10, 0);
   main_panel.add(label, gc);

   gc.fill = GridBagConstraints.HORIZONTAL;
   gc.gridx = 1;
   gc.gridy = 0;
   gc.insets = new Insets(5, 0, 10, 0);
   main_panel.add(band_combobox, gc);

   gc.fill = GridBagConstraints.HORIZONTAL;
   gc.gridx = 0;
   gc.gridy = 1;
   gc.gridwidth = 2;
   gc.insets = new Insets(0, 0, 10, 0);
   main_panel.add(panel1, gc);

   gc.fill = GridBagConstraints.HORIZONTAL;
   gc.gridx = 1;
   gc.gridy = 1;
   gc.gridwidth = 2;
   gc.insets = new Insets(0, 0, 10, 0);
   main_panel.add(panel2, gc);

   gc.fill = GridBagConstraints.HORIZONTAL;
   gc.gridx = 0;
   gc.gridy = 2;
   gc.gridwidth = 2;
   gc.insets = new Insets(0, 0, 10, 0);
   main_panel.add(panel3, gc);
}

在此处输入图片说明

I am trying to do something like this:

在此处输入图片说明

Try this :

public void createGUI()
{
   main_panel = new JPanel();
   main_panel.setLayout(new GridBagLayout());
   GridBagConstraints gc = new GridBagConstraints();

   gc.fill = GridBagConstraints.HORIZONTAL;
   gc.gridx = 0;
   gc.gridy = 0;
   gc.insets = new Insets(5, 0, 10, 0);
   main_panel.add(label, gc);

   gc.fill = GridBagConstraints.HORIZONTAL;
   gc.gridx = 1;
   gc.gridy = 0;
   gc.insets = new Insets(5, 0, 10, 0);
   main_panel.add(band_combobox, gc);

   gc.fill = GridBagConstraints.HORIZONTAL;
   gc.gridx = 0;
   gc.gridy = 1;
   gc.gridwidth = 1;
gc.weightx = 1;
gc.weighty = 1;
   gc.insets = new Insets(0, 0, 10, 0);
   main_panel.add(panel1, gc);

   gc.fill = GridBagConstraints.HORIZONTAL;
   gc.gridx = 1;
   gc.gridy = 1;
   gc.gridwidth = 1;
gc.weightx = 1;
gc.weighty = 1;
   gc.insets = new Insets(0, 0, 10, 0);
   main_panel.add(panel2, gc);

   gc.fill = GridBagConstraints.BOTH;
   gc.gridx = 0;
   gc.gridy = 2;
gc.weightx = 1;
gc.weighty = 1;
   gc.gridwidth = 2;
   gc.insets = new Insets(0, 0, 10, 0);
   main_panel.add(panel3, gc);
}

You are not setting the weight, and your width setting is a bit dodgy.

Also put the band label and combo in a JPanel, and set its width to two, and add it at the top. It looks much nicer that way.

Also look at the GridBagLayout tutorial I have (in my blog, in my profile) for some more help.

Don't be afraid to use compound layouts to achieve your needs. Your label and combo box want to "float" on the top row. This would be a difficult thing to achieve with a GridBagLayout on it's own, but by adding them to another container, with a different layout manager, it's achievable...

在此处输入图片说明

When you change a components gridWidth or gridHeight it will allow them to "flow" over into other columns and rows, meaning that they may sit above or below components that occupy that space, depending on when they are added...

Also, check out the weight properties.

import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.border.TitledBorder;

public class TestPane {
    private JPanel main_panel;

    public static void main(String[] args) {
        new TestPane();
    }

    public TestPane() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                }

                createGUI();;

                JFrame frame = new JFrame("Testing");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setLayout(new BorderLayout());
                frame.add(main_panel);
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public void createGUI() {

        main_panel = new JPanel();
        main_panel.setLayout(new GridBagLayout());
        GridBagConstraints gc = new GridBagConstraints();

        JLabel label = new JLabel("Band Directory");
        JComboBox band_combobox = new JComboBox();

        JPanel panel1 = new JPanel();
        panel1.add(new JLabel("1"));
        panel1.setBorder(new TitledBorder("Panel1"));
        JPanel panel2 = new JPanel();
        panel2.setBorder(new TitledBorder("Panel2"));
        panel2.add(new JLabel("2"));
        JPanel panel3 = new JPanel();
        panel3.setBorder(new TitledBorder("Panel3"));
        panel3.add(new JLabel("3"));

        JPanel top = new JPanel();
        top.add(label);
        top.add(band_combobox);

        gc.fill = GridBagConstraints.HORIZONTAL;
        gc.weightx = 1;
        gc.gridx = 0;
        gc.gridy = 0;
        gc.gridwidth = GridBagConstraints.REMAINDER;
        gc.insets = new Insets(5, 0, 10, 0);
        main_panel.add(top, gc);

        gc.gridwidth = 1;
        gc.weightx = 0.5;
        gc.gridx = 0;
        gc.gridy = 1;
        gc.insets = new Insets(0, 0, 10, 0);
        main_panel.add(panel1, gc);

        gc.gridx = 1;
        gc.gridy = 1;
        gc.insets = new Insets(0, 0, 10, 0);
        main_panel.add(panel2, gc);

        gc.fill = GridBagConstraints.HORIZONTAL;
        gc.weightx = 1;
        gc.gridx = 0;
        gc.gridy = 2;
        gc.gridwidth = GridBagConstraints.REMAINDER;
        gc.insets = new Insets(0, 0, 10, 0);
        main_panel.add(panel3, gc);
    }
}

You might find it useful to have a read through How to use GridBagLayout

Band Directory label, Combo Box, Member Panel and CD List Panel should be assigned gridwidth = 1 and Add a CD Panel as gridwith = 2.

public JPanel getComponentPanel()
{
   main_panel = new JPanel();
   main_panel.setLayout(new GridBagLayout());
   GridBagConstraints gc = new GridBagConstraints();

   gc.gridx = 0;
   gc.gridy = 0;
   gc.insets = new Insets(5, 0, 10, 0);
   main_panel.add(label, gc);

   gc.gridx = 1;
   gc.gridy = 0;
   gc.insets = new Insets(5, 10, 10, 0);
   main_panel.add(band_combobox, gc);

   gc.gridx = 0;
   gc.gridy = 1;
   gc.insets = new Insets(0, 10, 10, 0);
   panel1.setBorder(
           BorderFactory.createLineBorder(Color.BLACK));
   panel1.setMinimumSize(new Dimension(100, 50));
   panel1.setPreferredSize(new Dimension(100, 50));
   panel1.setMaximumSize(new Dimension(100, 50));
   main_panel.add(panel1, gc);

   gc.gridx = 1;
   gc.gridy = 1;
   gc.insets = new Insets(0, 10, 10, 0);
   panel2.setBorder(
           BorderFactory.createLineBorder(Color.BLACK));
   panel2.setMinimumSize(new Dimension(100, 50));
   panel2.setPreferredSize(new Dimension(100, 50));
   panel2.setMaximumSize(new Dimension(100, 50));
   main_panel.add(panel2, gc);

   gc.gridx = 0;
   gc.gridy = 2;
   gc.gridwidth = 2;
   gc.insets = new Insets(0, 10, 10, 0);
   panel3.setBorder(
           BorderFactory.createLineBorder(Color.BLACK));
   panel3.setMinimumSize(new Dimension(100, 50));
   panel3.setPreferredSize(new Dimension(100, 50));
   panel3.setMaximumSize(new Dimension(100, 50));
   main_panel.add(panel3, gc);

   return main_panel;
}
  • GBC required to create columns coordinates in 1st row, then is created matrix for whole container, gentle gradient is necessary for resize without flickering

  • sure is possible to create this GBC matrix virtually without placing invisible JComponents (JLabel with added Borders in this case) to the contianer, maybe why bothering,

  • this is about AbsoluteLayout by using GBC, another of ways is to use invisible JComponents

在此处输入图片说明

import java.awt.Color;
import java.awt.ComponentOrientation;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import javax.swing.BorderFactory;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingConstants;
import javax.swing.SwingUtilities;

public class FrameAndGBC {

    private JLabel hidelLabel;
    private JLabel firstLabel;
    private JTextField firstText;
    private JFrame frame = new JFrame("Frame And GBC");

    public FrameAndGBC() {
        JPanel panel = copyTextNorthPanel();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.add(panel);
        frame.pack();
        frame.setLocation(150, 150);
        frame.setVisible(true);
    }

    private JPanel copyTextNorthPanel() {
        JPanel panel = new JPanel(new GridBagLayout());
        GridBagConstraints gbc = new GridBagConstraints();
        panel.setComponentOrientation(ComponentOrientation.LEFT_TO_RIGHT);
        for (int k = 0; k < 50; k++) {
            hidelLabel = new JLabel("     ");
            hidelLabel.setBorder(BorderFactory.createLineBorder(Color.BLACK, 1));
            gbc.fill = GridBagConstraints.HORIZONTAL;
            gbc.weightx = 0.5;
            gbc.weighty = 0.5;
            gbc.gridx = k;
            gbc.gridy = 0;
            panel.add(hidelLabel, gbc);
        }
        for (int k = 0; k < 5; k++) {
            firstLabel = new JLabel("Testing Label : ", SwingConstants.RIGHT);
            firstLabel.setFont(new Font("Serif", Font.BOLD, 20));
            firstLabel.setBorder(BorderFactory.createLineBorder(Color.BLACK, 1));
            gbc.fill = GridBagConstraints.HORIZONTAL;
            gbc.insets = new Insets(0, 0, 5, 0);
            gbc.gridx = 0;
            gbc.gridwidth = 8;
            gbc.gridy = k + 1;
            panel.add(firstLabel, gbc);
        }
        for (int k = 0; k < 5; k++) {
            firstText = new JTextField("Testing TextField");
            firstText.setFont(new Font("Serif", Font.BOLD, 20));
            firstText.setBorder(BorderFactory.createLineBorder(Color.BLACK, 1));
            gbc.fill = GridBagConstraints.HORIZONTAL;
            gbc.insets = new Insets(0, 0, 5, 0);
            gbc.gridx = 9;
            gbc.gridwidth = k + 8;
            gbc.gridy = k + 1;
            panel.add(firstText, gbc);
        }
        for (int k = 0; k < 5; k++) {
            firstLabel = new JLabel("Testing Label : ", SwingConstants.RIGHT);
            firstLabel.setFont(new Font("Serif", Font.BOLD, 20));
            firstLabel.setBorder(BorderFactory.createLineBorder(Color.BLACK, 1));
            gbc.fill = GridBagConstraints.HORIZONTAL;
            gbc.insets = new Insets(0, 0, 5, 0);
            gbc.gridx = 20 + k;
            gbc.gridwidth = 8;
            gbc.gridy = k + 1;
            panel.add(firstLabel, gbc);
        }
        for (int k = 0; k < 5; k++) {
            firstText = new JTextField("Testing TextField");
            firstText.setFont(new Font("Serif", Font.BOLD, 20));
            firstText.setBorder(BorderFactory.createLineBorder(Color.BLACK, 1));
            gbc.fill = GridBagConstraints.HORIZONTAL;
            gbc.insets = new Insets(0, 0, 5, 0);
            gbc.gridx = 29 + k;
            gbc.gridwidth = 21 - k;
            gbc.gridy = k + 1;
            panel.add(firstText, gbc);
        }
        return panel;
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                FrameAndGBC fs = new FrameAndGBC();
            }
        });
    }
}

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