日期:2014-05-16  浏览次数:20516 次

JAVA之网格布局管理器中JSeparator的使用以及GridBagLayout的注意细节

1.通过下面的例子,我们可以看到,网格布局面板中添加分割线的方法是:

GridBagConstraints grid4 = new GridBagConstraints();
		grid4.gridx = 3;
		grid4.gridy = 0;
		grid4.fill = GridBagConstraints.VERTICAL;
		grid4.insets =  new Insets(0, 5, 0, 5);
		
		JSeparator separator4 = new JSeparator();	//创建竖直分隔线
		separator4.setOrientation(JSeparator.VERTICAL);


container.add(separator4, grid4);

如果去掉grid4.fill = GridBagConstraints.VERTICAL;语句,就会造成分割线不能显示,这点需要特别注意。

有兴趣的读者可以去试下。

2.另外就是对于网格布局管理器的单位网格,当添加到单位网格中的组件过大或者过小时,该网格会自动调整大小,直至能够容下该组件。当然自动调整时仍会保证同行网格高度相同,同列网格宽度相同。

  还有就是weightx,weighty的默认值是0,即不自动扩展空白面板区域。如下面的代码,当某列weightx的值都为1,而其他列却都为默认值0时,该列会自动扩展空白区域。。

package Project;

import java.awt.*;
import java.util.Date;

import javax.swing.*;

public class Table3 extends JFrame
{
	public Table3()
	{
		Container container = getContentPane();
		container.setLayout(new GridBagLayout());
		
		GridBagConstraints grid1 = new GridBagConstraints();
		grid1.gridx = 0;
		grid1.gridy = 0;
		grid1.weightx = 1;		//该列所有网格都设置为水平方向自动扩充
		grid1.insets = new Insets(0, 5, 0, 5);
		
		JLabel label1 = new JLabel("没有任何窗口打开");
		
		GridBagConstraints grid2 = new GridBagConstraints();
		grid2.gridx = 1;
		grid2.gridy = 0;
		grid2.fill = GridBagConstraints.VERTICAL;//去掉该语句,面板添加的分割线不显示
		grid2.insets = new Insets(0, 5, 0, 5);
		
		JSeparator separator2 = new JSeparator();	//创建竖直分隔线
		separator2.setOrientation(JSeparator.VERTICAL);
		
		
		GridBagConstraints grid3 = new GridBagConstraints();
		grid3.gridx = 2;
		grid3.gridy = 0;
//		grid3.weighty = 1;		//设置该行所有网格在竖直方向上自动扩展
		grid3.insets = new Insets(0, 5, 0, 5);
		
		JLabel label3 = new JLabel("操作员:");
		
		GridBagConstraints grid4 = new GridBagConstraints();
		grid4.gridx = 3;
		grid4.gridy = 0;
		grid4.fill = GridBagConstraints.VERTICAL;
		grid4.insets =  new Insets(0, 5, 0, 5);
		
		JSeparator separator4 = new JSeparator();	//创建竖直分隔线
		separator4.setOrientation(JSeparator.VERTICAL);
		
		
		
		GridBagConstraints grid5 = new GridBagConstraints();
		grid5.gridx = 4;
		grid5.gridy = 0;
		grid5.insets = new Insets(0, 5, 0, 5);
		
		Date date = new Date();
		JLabel label5 = new JLabel(String.format("%tF", date));
		
		GridBagConstraints grid6 = new GridBagConstraints();
		grid6.gridx = 5;
		grid6.gridy = 0;
		grid6.fill = GridBagConstraints.VERTICAL;
		grid6.insets = new Insets(0, 5, 0, 5);
		
		JSeparator separator6 = new JSeparator();	//创建竖直分隔线
		separator6.setOrientation(JSeparator.VERTICAL);
		
		
		GridBagConstraints grid7 = new GridBagConstraints();
		grid7.gridx = 6;
		grid7.gridy = 0;
		grid7.insets = new Insets(0, 5, 0, 5);
		
		JLabel label7 = new JLabel("**公司进销存管理系统");
		
		container.add(label1, grid1);
		container.add(separator2, grid2);
		container.add(label3, grid3);
		container.add(separator4, grid4);
		container.add(label5, grid5);
		container.add(separator6, grid6);
		container.add(label7, grid7);
		
		setVisible(true);
		setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
		setBounds(350, 150, 800, 200);
	}
	public static void  main (String []args)
	{
		Table3 table = new Table3();
	}
}