【问题标题】:GridBagLayout Adding Space between buttons/stagger/varied columns?GridBagLayout 在按钮/交错/可变列之间添加空间?
【发布时间】:2022-12-28 11:58:37
【问题描述】:

我正在尝试使用 GridBagLayout 创建布局,但无法使 JButton 控件之间的空间相等。第一行有 5 个按钮,中间没有空格。第二行和第三行应该有 4 个按钮,它们之间的空间较小。

我尝试的解决方法是将其设为 35x35 网格,其中顶部按钮的宽度为 7,其他按钮的宽度为 5。我不知道如何让 4 个按钮均匀对齐(最后一列中的空间较小)。

代码如下:

import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.Icon;

import javax.swing.JButton;
import javax.swing.JColorChooser;
import javax.swing.JDialog;
import javax.swing.colorchooser.AbstractColorChooserPanel;
import java.awt.Insets;
import java.awt.GridBagLayout;
import java.awt.GridBagConstraints;

public class MainTwo {
    public static void main(String[] a) {
        final JColorChooser colorChooser = new JColorChooser();
        MyChooserPanel newChooser = new MyChooserPanel();
        colorChooser.addChooserPanel(newChooser);

        ActionListener okActionListener = new ActionListener() {
            public void actionPerformed(ActionEvent actionEvent) {
                System.out.println(colorChooser.getColor());
            }
        };

        ActionListener cancelActionListener = new ActionListener() {
            public void actionPerformed(ActionEvent actionEvent) {
                System.out.println("Cancel");
            }
        };

        final JDialog dialog = JColorChooser.createDialog(null, "Change Button Background", true, colorChooser,
                okActionListener, cancelActionListener);
        dialog.setVisible(true);
    }
}

class MyChooserPanel extends AbstractColorChooserPanel {
    public void buildChooser() {
        setLayout(new GridBagLayout()); 
        GridBagConstraints c = new GridBagConstraints();
        c.fill = GridBagConstraints.NONE;
        c.ipadx = 21;
        c.ipady = 15;
        c.gridwidth = 7;
        makeAddButton("Black", Color.BLACK, c);
        makeAddButton("Dark Grey", Color.PINK, c);
        makeAddButton("Custom PathVisio Grey", Color.YELLOW, c);
        makeAddButton("Grey", Color.CYAN, c);
        makeAddButton("White", Color.WHITE, c);
        c.ipadx = 15;
        c.gridwidth = 5;
        c.insets = new Insets(10,0,0,0);
        c.gridy = 1; // new row
        makeAddButton("Blue", Color.BLUE, c);
        c.gridx = 10;
        makeAddButton("Green", Color.GREEN, c);
        c.gridx = 20;
        makeAddButton("Purple", Color.MAGENTA, c);
        c.gridx = 30;
        makeAddButton("Orange", Color.ORANGE, c);
        c.gridy = 2; // new row
        c.gridx = 0;
        makeAddButton("Dark Blue", Color.BLUE, c);
        c.gridx = 10;
        makeAddButton("Dark Green", Color.GREEN, c);
        c.gridx = 20;
        makeAddButton("Dark Purple", Color.MAGENTA, c);
        c.gridx = 30;
        makeAddButton("Dark Orange", Color.ORANGE, c);
    }

    public void updateChooser() {
    }

    public String getDisplayName() {
        return "Panel";
    }

    public Icon getSmallDisplayIcon() {
        return null;
    }

    public Icon getLargeDisplayIcon() {
        return null;
    }

    private void makeAddButton(String name, Color color, GridBagConstraints c) {
        JButton button = new JButton(name);
        button.setBackground(color);
        button.setBorderPainted(false);
        button.setOpaque(true);
        button.setAction(setColorAction);
        button.setToolTipText(name);
        add(button, c);
    }

    Action setColorAction = new AbstractAction() {
        public void actionPerformed(ActionEvent evt) {
            JButton button = (JButton) evt.getSource();
            getColorSelectionModel().setSelectedColor(button.getBackground());
        }
    };
}

【问题讨论】:

    标签: java swing layout-manager gridbaglayout


    【解决方案1】:

    我会将此 GUI 视为包含两个网格布局的一个边框布局。

    import java.awt.*;
    import javax.swing.*;
    import javax.swing.border.*;
    
    public class FiveAndEightLayout {
    
        public FiveAndEightLayout() {
            JPanel gui = new JPanel(new BorderLayout(4,4));
            gui.setBorder(new TitledBorder("Border Layout"));
    
            JPanel topPanel = new JPanel(new GridLayout(1,0,0,0));
            for (int ii=1; ii<6; ii++) {
                topPanel.add(new JButton("Button " + ii));
            }
            topPanel.setBorder(new TitledBorder("Grid Layout"));
            gui.add(topPanel, BorderLayout.PAGE_START);
    
            JPanel centerPanel = new JPanel(new GridLayout(0,4,15,5));
            for (int ii=1; ii<9; ii++) {
                centerPanel.add(new JButton("Button " + ii));
            }
            centerPanel.setBorder(new TitledBorder("Grid Layout"));
            gui.add(centerPanel, BorderLayout.CENTER);
    
            JFrame f = new JFrame("Five And Eight Layout");
            f.add(gui);
            f.pack();
            f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
            f.setVisible(true);
        }
    
        public static void main(String[] args) {
            Runnable r = new Runnable() {
                public void run() {
                    new FiveAndEightLayout();
                }
            };
            SwingUtilities.invokeLater(r);
        }
    }
    

    【讨论】:

      【解决方案2】:

      通常,我反对使用多个布局来实现一个目标,但在这种情况下,我建议将其分成 2 个面板。这是一个例子:

      public static void main(String[] args) {
          JFrame frame = new JFrame();
          frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
          frame.setLayout(new GridBagLayout());
          Insets insets = new Insets(4, 4, 4, 4);
          
          JPanel top = new JPanel(new GridBagLayout());
          top.add(new JButton("Top 1"), new GridBagConstraints(0, 0, 1, 1, 1, 1, GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, insets, 0, 0));
          top.add(new JButton("Top 2"), new GridBagConstraints(1, 0, 1, 1, 1, 1, GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, insets, 0, 0));
          top.add(new JButton("Top 3"), new GridBagConstraints(2, 0, 1, 1, 1, 1, GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, insets, 0, 0));
          top.add(new JButton("Top 4"), new GridBagConstraints(3, 0, 1, 1, 1, 1, GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, insets, 0, 0));
          top.add(new JButton("Top 5"), new GridBagConstraints(4, 0, 1, 1, 1, 1, GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, insets, 0, 0));
          
          JPanel bottom = new JPanel(new GridBagLayout());
          bottom.add(new JButton("Mid 1"), new GridBagConstraints(0, 1, 1, 1, 1, 1, GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, insets, 0, 0));
          bottom.add(new JButton("Mid 2"), new GridBagConstraints(1, 1, 1, 1, 1, 1, GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, insets, 0, 0));
          bottom.add(new JButton("Mid 3"), new GridBagConstraints(2, 1, 1, 1, 1, 1, GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, insets, 0, 0));
          bottom.add(new JButton("Mid 4"), new GridBagConstraints(3, 1, 1, 1, 1, 1, GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, insets, 0, 0));
          
          bottom.add(new JButton("Bot 1"), new GridBagConstraints(0, 2, 1, 1, 1, 1, GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, insets, 0, 0));
          bottom.add(new JButton("Bot 2"), new GridBagConstraints(1, 2, 1, 1, 1, 1, GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, insets, 0, 0));
          bottom.add(new JButton("Bot 3"), new GridBagConstraints(2, 2, 1, 1, 1, 1, GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, insets, 0, 0));
          bottom.add(new JButton("Bot 4"), new GridBagConstraints(3, 2, 1, 1, 1, 1, GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, insets, 0, 0));
          
          frame.add(top, new GridBagConstraints(0, 0, 1, 1, 1, 1, GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, new Insets(0, 0, 0, 0), 0, 0));
          frame.add(bottom, new GridBagConstraints(0, 1, 1, 1, 1, 1, GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, new Insets(0, 0, 0, 0), 0, 0));
          frame.pack();
          frame.setLocationRelativeTo(null);
          frame.setVisible(true);
      }
      

      【讨论】: