`
dreamoftch
  • 浏览: 484371 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

GridBagLayout布局管理器应用详解

阅读更多
package test;

import java.awt.Color;
import java.awt.Container;
import java.awt.GridBagLayout;

import javax.swing.JFrame;
import javax.swing.JPanel;

public class GridTest extends JFrame{
	private static final long serialVersionUID = 1L;

	public static void main(String[] args) {
		new GridTest().addGridBagPanes();
	}

	public void addGridBagPanes() {
		//上侧的工具选择面板
		Container content = this.getContentPane();
		content.setLayout(new GridBagLayout());
		JPanel toolSelectPanel = new JPanel();
		toolSelectPanel.setBackground(Color.green);
		content.add(toolSelectPanel, new GBC(0,0,2,1).setFill(GBC.BOTH).setIpad(200, 50).setWeight(100, 0));
		//左侧的具体工具面板
		JPanel toolConcretePanel = new JPanel();
		toolConcretePanel.setBackground(Color.YELLOW);
		content.add(toolConcretePanel,new GBC(0,1).setFill(GBC.BOTH).setIpad(70, 90).setWeight(0, 100));
		//右侧的绘图面板
		JPanel drawPanel = new JPanel();
		drawPanel.setBackground(Color.WHITE);
		content.add(drawPanel,new GBC(1,1).setFill(GBC.BOTH));
		//下侧的颜色选择面板
		JPanel colorPanel = new JPanel();
		colorPanel.setBackground(Color.LIGHT_GRAY);
		content.add(colorPanel,new GBC(0,2,2,1).setFill(GBC.BOTH).setIpad(200,50).setWeight(100, 0));
		//下侧的状态面板
		JPanel statePanel = new JPanel();
		statePanel.setBackground(Color.CYAN);
		content.add(statePanel,new GBC(0,3,2,1).setFill(GBC.BOTH).setIpad(200, 20).setWeight(100, 0));
		this.setSize(500, 500);
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		this.setVisible(true);
	}
	
}

 

package test;

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

public class GBC extends GridBagConstraints
{
	private static final long serialVersionUID = 1L;

//初始化左上角位置
   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;
   }

   //x和y方向上的增量
   public GBC setWeight(double weightx, double 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 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;
   }
}

 

 

 

 

 

 

GridBagLayout布局管理器应用详解

 

     很多情况下,我们已经不需要通过编写代码来实现一个应用程序的图形界面,而是通过强大的IDE工具通过拖拽辅以简单的事件处理代码即可很轻松的完成。但是我们不得不面对这样操作存在的一些问题,有时候我们希望能够自由改变一个程序界面的大小,通过托拽生成的界面往往是不提供这个功能的,因为定制的界面一旦改变形状,组件间的布局会变得杂乱无章。

     Java中的布局管理器应用了策略者模式(Strategy),为不同类型的组件布局提供了很好的模型。而其中的网格组布局管理器(GridBagLayout)被认为是所有布局管理器中最强大的。下面笔者将通过一个例子讲述它的使用方法。

 

1.笔者目标是实现一个WindowsXP下画图板的框架,下面是设计模型图


      我们可以将GridBagLayout看作没有任何约束或限制的网格布局(GridLayout),一个组件可以占据若干行和列,而且大小设定也是自由的。从上图我们可以清晰的看到整个画图板界面被划分为4行2列的一个表格,我们先不用管某个单元格的大小,只管划分。

     这样五个具体的面板(我们使用JPanel对象进行填充)就构成了整个界面,上侧占据1行2列的工具选择面板(toolSelectPanel),左侧1行1列的某个工具的选项面板(toolConcretePanel),右侧的drawPanel,下侧的colorPanel和statePanel

 

2.我们通过以下步骤为界面进行GridBagLayout布局

      1).设置主界面的布局管理器为GridBagLayout(不用指定行和列)

      2).为界面中的每一个组件(这里是JPanel对象)指定一个GridBagConstraints对象,通过设置该对象的属性值指出组件在

管理器中的布局方案

      3).通过下面的调用添加组件极其约束条件(GridBagConstraints对象)

        add(Component,constraints);

 

      我们有必要了解一下GridBagConstraints中各个属性的具体含义以便我们更好的进行个性化的布局

      @gridx,gridy:

       组件左上角所在的位置,如上图中左侧的面板在1行0列,则gridy=1,gridx=0。读者请注意这里的行对应的是gridy,列对应的是gridx

       @gridwidth,gridheight

       组件占据的行数和列数,如最上面的那个面板占了1行2列,则gridwidth=2,gridheight=1

       @weightx,weighty

       可以简单理解为组件大小变化的增量值,如设置weightx=100,组件会随着单元格而变化,设置weightx=0时,组件大小不会发生变化。当然weightx,weighty也可以设置成其他的值,不过意义不大,就不再详细介绍。

       @fill

       组件在所处格子(分配区域)内的填充方式

       如fill= HORIZONTAL,组件就只在水平方向上填充满单元格,取fill= BOTH则会填满整个格子。

       @anchor

       组件在所处格子内的对其方式,取anchor=EAST就是指右对齐

       @ipadx,ipady

       内部填充,是指在组件首选大小的基础上x方向上加上ipadx,y方向上加上ipady,这样做就可以保证组件不会收缩到ipadx,ipady所确定的大小以下,因此我们可以用ipadx,ipady的值来指定组件的大小,而不必指定组件的大小否则会有意想不到的效果

       @insets

       外部填充,填充的区域是组件与所处格子边框之间的部分,有left,top,right,bottom四个参数,不过当组件的fill=NONE时,指定insects值是无意义的

 

3.下面就是代码实现了,先看下效果图吧

这是运行生成的界面


这是拉伸之后的界面


关键代码如下

 

Java代码  收藏代码
  1. private void addGridBagPanes() {  
  2.         //上侧的工具选择面板  
  3.         JPanel toolSelectPanel = new JPanel();  
  4.         toolSelectPanel.setBackground(Color.green);  
  5.         this.add(toolSelectPanel, new GBC(0,0,2,1).  
  6.                      setFill(GBC.BOTH).setIpad(20050).setWeight(1000));  
  7.         //左侧的具体工具面板  
  8.         JPanel toolConcretePanel = new JPanel();  
  9.         toolConcretePanel.setBackground(Color.YELLOW);  
  10.         this.add(toolConcretePanel,new GBC(0,1).  
  11.                      setFill(GBC.BOTH).setIpad(7090).setWeight(0100));  
  12.         //右侧的绘图面板  
  13.         JPanel drawPanel = new JPanel();  
  14.         drawPanel.setBackground(Color.WHITE);  
  15.         this.add(drawPanel,new GBC(1,1).setFill(GBC.BOTH));  
  16.         //下侧的颜色选择面板  
  17.         JPanel colorPanel = new JPanel();  
  18.         colorPanel.setBackground(Color.LIGHT_GRAY);  
  19.         this.add(colorPanel,new GBC(0,2,2,1).  
  20.                      setFill(GBC.BOTH).setIpad(200,50).setWeight(1000));  
  21.         //下侧的状态面板  
  22.         JPanel statePanel = new JPanel();  
  23.         statePanel.setBackground(Color.CYAN);  
  24.         this.add(statePanel,new GBC(0,3,2,1).  
  25.                       setFill(GBC.BOTH).setIpad(20020).setWeight(1000));  
  26.     }  
 

       其中的GBC类继承于GridBagConstraints这样做的目的是简化每次对GridBagConstraints对象的直接操作带来的繁琐,而GBC的各个set方法返回的都是一个GBC对象因此可以接连调用set方法。GBC类的代码如下:

 

Java代码  收藏代码
  1. public class GBC extends GridBagConstraints  
  2. {  
  3.    //初始化左上角位置  
  4.    public GBC(int gridx, int gridy)  
  5.    {  
  6.       this.gridx = gridx;  
  7.       this.gridy = gridy;  
  8.    }  
  9.   
  10.    //初始化左上角位置和所占行数和列数  
  11.    public GBC(int gridx, int gridy, int gridwidth, int gridheight)  
  12.    {  
  13.       this.gridx = gridx;  
  14.       this.gridy = gridy;  
  15.       this.gridwidth = gridwidth;  
  16.       this.gridheight = gridheight;  
  17.    }  
  18.   
  19.    //对齐方式  
  20.    public GBC setAnchor(int anchor)  
  21.    {  
  22.       this.anchor = anchor;  
  23.       return this;  
  24.    }  
  25.   
  26.    //是否拉伸及拉伸方向  
  27.    public GBC setFill(int fill)  
  28.    {  
  29.       this.fill = fill;  
  30.       return this;  
  31.    }  
  32.   
  33.    //x和y方向上的增量  
  34.    public GBC setWeight(double weightx, double weighty)  
  35.    {  
  36.       this.weightx = weightx;  
  37.       this.weighty = weighty;  
  38.       return this;  
  39.    }  
  40.   
  41.    //外部填充  
  42.    public GBC setInsets(int distance)  
  43.    {  
  44.       this.insets = new Insets(distance, distance, distance, distance);  
  45.       return this;  
  46.    }  
  47.   
  48.    //外填充  
  49.    public GBC setInsets(int top, int left, int bottom, int right)  
  50.    {  
  51.       this.insets = new Insets(top, left, bottom, right);  
  52.       return this;  
  53.    }  
  54.   
  55.    //内填充  
  56.    public GBC setIpad(int ipadx, int ipady)  
  57.    {  
  58.       this.ipadx = ipadx;  
  59.       this.ipady = ipady;  
  60.       return this;  
  61.    }  
  62. }  
 
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics