【Java Swing】Java复杂的布局管理——网格组布局(GridBagLayout)

网格组布局是Java较为复杂的布局,虽然这又背于Java简单的初衷。网格组布局十分优雅,组件可以随着窗口的调整自动放大或缩小。

要有效地使用网格组布局,必须自定义GridBagConstraints与其组件关联的一个或多个对象。您可以GridBagConstraints 通过设置一个或多个实例变量来自定义对象:

GridBagConstraints.gridx, GridBagConstraints.gridy

每个格子都有自己的单元格坐标(gridx,gridy),并不以长度为单位,左上角的原点单元格,坐标为(0,0)。右边格子的坐标为(1,0),一次类推。

GridBagConstraints.gridwidth, GridBagConstraints.gridheight

指跨过的单元格数,网格坐标为(0,0)的单元格网格长度为(2,2),表明其占据了2*2的方格

GridBagConstraints.fill

指窗口拉伸时的填充的方向

GridBagConstraints.insets

指定组件的外部填充,即组件与其显示区域的边缘之间的最小间距。

GridBagConstraints.anchor

指对齐方式

GridBagConstraints.NORTH
GridBagConstraints.SOUTH
GridBagConstraints.WEST
GridBagConstraints.EAST
GridBagConstraints.NORTHWEST
GridBagConstraints.NORTHEAST
GridBagConstraints.SOUTHWEST
GridBagConstraints.SOUTHEAST
GridBagConstraints.CENTER (默认)
方向相对值:
GridBagConstraints.PAGE_START
GridBagConstraints.PAGE_END
GridBagConstraints.LINE_START
GridBagConstraints.LINE_END
GridBagConstraints.FIRST_LINE_START
GridBagConstraints.FIRST_LINE_END
GridBagConstraints.LAST_LINE_START
GridBagConstraints.LAST_LINE_END
基准相对值:
GridBagConstraints.BASELINE
GridBagConstraints.BASELINE_LEADING
GridBagConstraints.BASELINE_TRAILING
GridBagConstraints.ABOVE_BASELINE
GridBagConstraints.ABOVE_BASELINE_LEADING
GridBagConstraints.ABOVE_BASELINE_TRAILING
GridBagConstraints.BELOW_BASELINE
GridBagConstraints.BELOW_BASELINE_LEADING
GridBagConstraints.BELOW_BASELINE_TRAILING

GridBagConstraints.weightx, GridBagConstraints.weighty

指最大拉伸宽度或高度

要想更加方便地使用GridBagLayout,一般我们重写GridBagConstraints,改名为GBC。这样我们可以采用流式写法,快速定义GBC的参数。代码如下:

import java.awt.*;

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,int weighty)
	{
    
    
		this.weightx=weightx;
		this.weighty=weighty;
		return this;
	}
	public GBC setInsets(int distance)
	{
    
    
		this.insets=new Insets(distance,distance,distance,distance);
		return this;
	}
	public GBC setIpad(int ipadx,int ipady)
	{
    
    
		this.ipadx=ipadx;
		this.ipady=ipady;
		return this;
	}
}

下面给一个例子,如何使用GBC构建网格组布局,来自《Java核心技术》:

package GridBag;
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;

public class FontFrame extends JFrame
{
    
    
	private final int TEXT_ROWS=10;
	private final int TEXT_COLUMNS=20;
	private JComboBox<String> face;
	private JComboBox<Integer> size;
	private JTextArea sample;
	private JCheckBox italic;
	private JCheckBox bold;
	public FontFrame()
	{
    
    
		setDefaultCloseOperation(EXIT_ON_CLOSE);
		GridBagLayout layout=new GridBagLayout();
		setLayout(layout);
		
		ActionListener lisenter=event->{
    
    
			upsample();
		};
		
		JLabel faceLabel=new JLabel("Face:");
		JLabel sizeLabel=new JLabel("Size:");
		face=new JComboBox<>(new String[] {
    
    "Serif", "SansSerif", "Monospaced", "Dialog", "Dialoglnput"});
		face.addActionListener(lisenter);
		size=new JComboBox<>(new Integer[] {
    
     8, 10, 12, 15, 18, 24, 36, 48});
		size.addActionListener(lisenter);
		
		bold=new JCheckBox("bold");
		italic=new JCheckBox("italic");
		bold.addActionListener(lisenter);
		italic.addActionListener(lisenter);
		sample=new JTextArea(TEXT_ROWS,TEXT_COLUMNS);
		sample.setText("The quick brown fox jumps over the lazy dog");
		sample.setEditable(false);
		sample.setLineWrap(true);
		sample.setBorder(BorderFactory.createEtchedBorder());
		
		add(faceLabel, new GBC(0, 0).setFill(GBC.EAST).setWeight(100, 100));
		add(sizeLabel, new GBC(0,1).setFill(GBC.EAST).setWeight(100, 100));
		add(face,new GBC(1,0).setFill(GBC.HORIZONTAL).setWeight(100, 100));
		add(size,new GBC(1,1).setFill(GBC.HORIZONTAL).setWeight(100, 100));
		add(bold,new GBC(0,2,2,1).setFill(GBC.CENTER).setWeight(100, 100));
		add(italic,new GBC(0,3,2,1).setFill(GBC.CENTER).setWeight(100, 100));
		add(sample,new GBC(2,0,1,4).setFill(GBC.BOTH).setWeight(100, 100));
		
		pack();
		upsample();
		
	}
	private void upsample() 
	{
    
    
		String fontFace=(String)face.getSelectedItem();
		int fontStyle=(bold.isSelected()? Font.BOLD:0)+(italic.isSelected()? Font.ITALIC:0);
		int fontSize=(Integer)size.getSelectedItem();
		Font font=new Font(fontFace,fontStyle,fontSize);
		sample.setFont(font);
		sample.repaint();
	}

}

测试类:

package GridBag;

import java.awt.EventQueue;
import java.awt.Font;

public class FontTest {
    
    

	public static void main(String[] args) {
    
    
		EventQueue.invokeLater(()->{
    
    ;
		FontFrame frame=new FontFrame();
		frame.setVisible(true);
		});
	}
}

猜你喜欢

转载自blog.csdn.net/m0_47202518/article/details/108300061