Java中GridBagConstraints类的重要性是什么?


GridBagLayout是一个非常灵活的布局管理器,它允许我们使用约束来设置组件之间的相对位置。每个GridBagLayout使用一个动态的矩形单元格网格,每个组件占据一个或多个单元格,称为其显示区域。由GridBagLayout管理的每个组件都与一个GridBagConstraints实例相关联,该实例指定组件在其显示区域内的布局方式。

GridBagConstraints

我们可以通过设置一个或多个公共实例变量来自定义GridBagConstraints对象。这些变量指定组件的位置、大小、增长因子、锚点、内边距、填充和间距

  • gridx:一个整数值,指定组件占据的最左单元格。gridx指定组件将放置的列。
  • gridy:一个整数值,指定组件占据的最上单元格。gridy指定组件将放置的行。
  • gridheight:一个整数值,指定组件占据的垂直单元格数。
  • gridwidth:一个整数值,指定组件占据的水平单元格数。
  • ipadx:一个整数值,指定要添加到每个控件的内部水平填充量。
  • ipady:一个整数值,指定要添加到每个控件的内部垂直填充量。
  • insets:一个Insets对象,指定要在单元格每一侧留出的空空间量。
  • weightx:一个双精度值,指定如果生成的布局在水平方向上小于分配的区域,则如何分配额外的水平空间。
  • weighty:一个双精度值,指定如果生成的布局在垂直方向上小于分配的区域,则如何分配额外的垂直空间。
  • anchor:一个整数值,指定组件在单元格内的对齐方式。
  • fill:一个整数值,指定如何处理单元格中的额外空间。
  • RELATIVE:对于gridx和gridy字段,此字段指定组件将放置在最后添加的组件旁边。对于gridwidth和gridheight字段,此字段指定组件将是行或列中倒数第二个组件。
  • REMAINDER:对于gridwidth和gridheight字段,此字段指定组件是行或列中的最后一个组件。

示例

import java.awt.*;
import javax.swing.*;
public class GridBagLayoutTest {
   public static void main(String[] a) {
      JFrame frame = new JFrame("GridBagLayout Test");
      Container myPane = frame.getContentPane();
      myPane.setLayout(new GridBagLayout());
      GridBagConstraints c = new GridBagConstraints();
      setMyConstraints(c,0,0,GridBagConstraints.CENTER);
      myPane.add(getFieldPanel(),c);
      setMyConstraints(c,0,1,GridBagConstraints.CENTER);
      myPane.add(getButtonPanel(),c);
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      frame.setSize(300, 250);
      frame.setLocationRelativeTo(null);
      frame.setVisible(true);
   }
   private static JPanel getFieldPanel() {
      JPanel p = new JPanel(new GridBagLayout());
      p.setBorder(BorderFactory.createTitledBorder("Details"));
      GridBagConstraints c = new GridBagConstraints();
      setMyConstraints(c,0,0,GridBagConstraints.EAST);
      p.add(new JLabel("Name:"),c);
      setMyConstraints(c,1,0,GridBagConstraints.WEST);
      p.add(new JTextField(16),c);
      setMyConstraints(c,0,1,GridBagConstraints.EAST);
      p.add(new JLabel("System:"),c);
      setMyConstraints(c,1,1,GridBagConstraints.WEST);
      p.add(getSystemPanel(),c);
      setMyConstraints(c,0,2,GridBagConstraints.EAST);
      p.add(new JLabel("Language:"),c);
      setMyConstraints(c,1,2,GridBagConstraints.WEST);
      p.add(getLanguagePanel(),c);
      setMyConstraints(c,0,3,GridBagConstraints.EAST);
      p.add(new JLabel("Year:"),c);
      setMyConstraints(c,1,3,GridBagConstraints.WEST);
      p.add(new JComboBox(new String[] {"2019","2020","2021"}),c);
      return p;
   }
   private static JPanel getButtonPanel() {
      JPanel p = new JPanel(new GridBagLayout());
      p.add(new JButton("OK"));
      p.add(new JButton("Cancel"));
      return p;
   }
   private static JPanel getSystemPanel() {
      JRadioButton winButton = new JRadioButton("Windows",true);
      JRadioButton macButton = new JRadioButton("Mac",false);
      ButtonGroup systemGroup = new ButtonGroup();
      systemGroup.add(winButton);
      systemGroup.add(macButton);
      JPanel p = new JPanel(new GridBagLayout());
      p.add(winButton);
      p.add(macButton);
      return p;
   }
   private static JPanel getLanguagePanel() {
     JPanel p = new JPanel(new GridBagLayout());
      p.add(new JCheckBox("Java",true));
      p.add(new JCheckBox("Python",true));
      p.add(new JCheckBox("Spark",false));
      return p;
   }
   private static void setMyConstraints(GridBagConstraints c, int gridx, int gridy, int anchor) {
      c.gridx = gridx;
      c.gridy = gridy;
      c.anchor = anchor;
   }
}

输出


更新于:2020年2月7日

2K+ 浏览量

启动你的职业生涯

通过完成课程获得认证

开始学习
广告
© . All rights reserved.