GridBagConstraints详解

        GridBagLayout要算是java中最复杂的布局管理器,要想得到一副令人满意的界面,需要耐心调整。GridBagConstraints的作用,就是对添加到GridBagLayout中的组件进行约束性限制,诸如组件插入的位置、大小、对齐方式、填充方式等等。最近在看《java核心技术第7版》,结合书中的例子,和java API,对其约束性字段做个详细的介绍。

  1. 位置字段    gridx,gridy:其相对位置从左上角起,即gridx=0,gridy=0表示左上角,需要注意的是,gridx表示列,gridy表示行。
  2. 大小字段    gridwidth、gridheight:这里所讲的大小,是指组件在布局中所占用的行数与列数

  1. 填充方式字段    fill: 即当整个面板放大、拉伸是,组件是如何填充的
  2. 对齐方式字段    anchor:字段内的文字以什么方向对齐
  3. 增量方式字段    weightx、weighty:通常以相对百分比来表示,也是在整个面板放大、拉伸时,组件以何种方向放大和拉伸
  4. 外部填充字段    insets:组件与其显示区域边缘之间间距的最小量,注意,Insets构造函数有4个变量,分别为上下左右四个方向
  5. 内部填充字段    ipadx、ipady:表明组件的最小高度与宽度

另:组件的位置,有时候可以不用gridx、gridy来进行绝对性的约束,GridBagConstraints还提供了两个静态变量来进行相对的约束。

  1. GridBagConstraints.RELATIVE:表明次组件为本行、列的倒数第二组件
  2. GridBagConstraints.REMAINDER表明此组件为本行的最后一个组件

为了更能说明字段的作用,用书中的例子来解释,就将代码贴出来:

辅类:GBC.java,继承自GridBagConstraints,为方便调用。

package corejava;

import java.awt.GridBagConstraints;
import java.awt.Insets;

public class GBC extends GridBagConstraints {
    public GBC(int gridx,int gridy) {
        this.gridx=gridx;
        this.gridy=gridy;
    }
    public GBC(int gridx,int gridy,int gridwidth,int gridheight) {
        this.gridx=gridx;
        this.gridy=gridy;
        this.gridwidth=gridwidth;
        this.gridheight=gridheight;
    }
    public GBC setAnchor(int anchor) {
        this.anchor=anchor;
        return this;
    }
    public GBC setFill(int fill) {
        this.fill=fill;
        return this;
    }
    public GBC setWeight(double weightx,double weighty) {
        this.weightx=weightx;
        this.weighty=weighty;
        return this;
    }
    public GBC setInsets(int top,int left,int bottom,int right) {
        this.insets=new Insets(top,left,bottom,right);
        return this;
    }
    public GBC setIpad(int ipadx,int ipady) {
        this.ipadx=ipadx;
        this.ipady=ipady;
        return this;
    }
}

主类:FontDialogFrame.java

package corejava;

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

import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextArea;

import java.awt.Font;
import java.awt.GridBagLayout;

import javax.swing.BorderFactory;

public class FontDialogFrame extends JFrame {
    private JComboBox face;
    private JComboBox size;
    private JCheckBox bold;
    private JCheckBox italic;
    private JTextArea sample;
    public static void main(String[] args) {
        JFrame f=new FontDialogFrame();
    }
    public FontDialogFrame() {
        setTitle("FontDialogFrame");
        setSize(800,600);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setLayout(new GridBagLayout());
        ActionListener listener=new FontAction();
        JLabel faceLabel=new JLabel("Face");
        face=new JComboBox(new String[] {
                "Serif","SansSerif","Monospaced",
                "Dialog","DialogInput"
        });
        face.addActionListener(listener);
        JLabel sizeLabel=new JLabel("Size");
        size=new JComboBox(new String[] {
                "8","10","12","15","18","24","36","48"
        });
        size.addActionListener(listener);
        bold=new JCheckBox("Bold");
        bold.addActionListener(listener);
        italic=new JCheckBox("Italic");
        italic.addActionListener(listener);
        sample=new JTextArea(10,50);
        sample.setText("The quick brown fox quick jump the lazy dog!");
        sample.setEditable(true);
        sample.setLineWrap(true);
        sample.setBorder(BorderFactory.createEtchedBorder());
        add(faceLabel,new GBC(0,0).setAnchor(GBC.EAST));
        add(face,new GBC(1,0).setFill(GBC.HORIZONTAL).setWeight(100,0).setInsets(4,4,4,4));
        add(sizeLabel,new GBC(0,1).setAnchor(GBC.EAST));
        add(size,new GBC(1,1).setFill(GBC.HORIZONTAL).setWeight(100,0).setInsets(4,4,4,4));
        add(bold,new GBC(0,2,2,1).setAnchor(GBC.CENTER).setWeight(100,100));
        add(italic,new GBC(0,3,2,1).setAnchor(GBC.CENTER).setWeight(100,100));
        add(sample,new GBC(2,0,1,4).setFill(GBC.BOTH).setWeight(100,100));
        pack();
        setVisible(true);
    }
    private class FontAction implements ActionListener {
        public void actionPerformed(ActionEvent event) {
            String fontFace=(String)face.getSelectedItem();
            int fontStyle=(bold.isSelected() ? Font.BOLD :0)+(italic.isSelected() ? Font.ITALIC :0);
            int fontSize=Integer.parseInt((String)size.getSelectedItem());
            Font font=new Font(fontFace,fontStyle,fontSize);
            sample.setFont(font);
            sample.repaint();
        }
    }
}

发表评论

电子邮件地址不会被公开。 必填项已用 * 标注

*

您可以使用这些 HTML 标签和属性: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="" highlight="">

Go back to top