GridBagLayout对齐问题,按钮扩展列宽

时间:2015-02-03 21:09:26

标签: java swing layout-manager gridbaglayout

我正在尝试使用一些JLabels图标(方框图标)和一个退出按钮创建一个界面。问题是,当我将按钮放在标签下面时,它们会根据按钮的位置分开:

example

GridBagConstraints c = new GridBagConstraints();
c.fill = GridBagConstraints.VERTICAL; // Adding space before the labels
c.gridy = 0;
c.weighty = 1;
add(new JLabel(" "), c);

for(int i = 0; i < label.length; i++){  // Adding the JLabels       
    c.fill = GridBagConstraints.NONE;
    c.weighty = 0;
    c.gridy = 1;
    c.gridx++;
    add(label[i], c);
}
// Adding the button
c.gridy ++;
c.gridx = 2;    // Changing the value of gridx, will move the button to that location and it will split another jlabel.
c.weighty = 0.09;
add(eButton, c);
revalidate();
repaint();

1 个答案:

答案 0 :(得分:0)

GridBagLayout的工作原理很像网格纸,它有行和列,组件放在这些单元格内;您可以指定如何分配额外空间并在这些单元格中对齐组件。

每个行和列的大小都是根据该行/列中其他组件的布局要求确定的,这意味着组件可能有比所需更多的空间,因为同一行/列中的某些其他组件需要更多空间布局。

您还可以允许组件跨越多行/列,从而占用更多空间。

所以,基本上,你需要做的是告诉按钮它可以占据它的所有列(或至少5个被标签占据的列)。然后,您需要告诉按钮它需要与其中心区域对齐。

GridBagLayout

import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.border.LineBorder;

public class Test {

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

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

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

    public class TestPane extends JPanel {

        public TestPane() {
            setLayout(new GridBagLayout());
            GridBagConstraints c = new GridBagConstraints();
            c.fill = GridBagConstraints.VERTICAL; // Adding space before the labels
            c.gridy = 0;
            c.weighty = 1;

            for (int i = 0; i < 5; i++) {  // Adding the JLabels       
                c.fill = GridBagConstraints.NONE;
                c.weighty = 0;
                c.gridy = 1;
                c.gridx++;
                c.insets = new Insets(4, 4, 4, 4);
                JLabel label = new JLabel() {
                    @Override
                    public Dimension getPreferredSize() {
                        return new Dimension(20, 20);
                    }
                };
                label.setBorder(new LineBorder(Color.DARK_GRAY));
                add(label, c);
            }
            c.gridy++;
            c.gridx = 0;
            c.gridwidth = GridBagConstraints.REMAINDER;
            c.anchor = GridBagConstraints.NORTH;
//          c.weighty = 0.09;
            add(new JButton("Exit"), c);
        }

    }

}

仔细查看How to Use GridBagLayout了解更多详情