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

gridBagLayout简明例子

GridBagLayout比较难懂。

?

写了个简单示例 ,如下,有时间再来解释:

?

把它放入ECLIPSE里,运行一下,自己感受感受,用到的图片自己随便找个,10*10的的就行。

?

package sh.xjh.invoke;
import javax.swing.*;

import java.awt.*;
public class TwoColTwoRowPanel extends JFrame {
	private JPanel jContentPane = null;
	private JButton jb1;
	private JButton jb2;
	private JButton jb3;
	private JButton jb4;
	GridBagConstraints c;
	GridBagLayout gridBagLayout;
	
	//sub panel
	private JPanel jpUpHeadImg;
	private JPanel jpDownHeadImg;
	
	//subScrollPane
	private JScrollPane jspHistory;
	private JScrollPane jspSendMsg;
	
	//jtextArea
	private JTextArea jtaHistory;
	private JTextArea jtaSendMsg;
	
	//jlabel
	private JLabel jlUpHeadImg;
	private JLabel jlDownHeadImg;
	
	//jb
	JButton jbSendMsg;
	
	public TwoColTwoRowPanel(){
		
		initilize();
		
		//set size
		this.setSize(800,600);		
		//set location
		this.setLocation(200, 100);
		//set visible
		this.setVisible(true);
		//set default close operation
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		
		
	}
	
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		TwoColTwoRowPanel mgbl=new TwoColTwoRowPanel();
	}
	
	private void initilize(){
		this.setContentPane(this.getContentPane());
	}

	@Override
	public JPanel getContentPane() {
		// TODO Auto-generated method stub
		if(jContentPane==null){
			jContentPane=new JPanel();
			
			//set layout
			 gridBagLayout=new GridBagLayout();
			jContentPane.setLayout(gridBagLayout);
			
			
			c=new GridBagConstraints();
			//add jspHistory
			c.fill=GridBagConstraints.BOTH;
			c.gridx=0;
//			c.gridy=0;
			c.gridwidth=1;
			c.gridheight=1;
			c.weightx=0.9;
			c.weighty=0.8;
//			makeButton(jContentPane,this.getJb1(),c,gridBagLayout);
			this.createSubComp(jContentPane, this.getJspHistory(), c, gridBagLayout);
			
			//add jpUpHeadImg
//			c.fill=GridBagConstraints.BOTH;
			c.gridx=1;
//			c.gridy=0;
			c.gridwidth=1;
			c.weightx=0.1;
			c.weighty=0.2;
//			makeButton(jContentPane,this.getJb2(),c,gridBagLayout);
			this.createSubComp(jContentPane, this.getJpUpHeadImg(), c, gridBagLayout);
			
			
			//add JspSendMsg
//			c.fill=GridBagConstraints.BOTH;
			c.gridx=0;
			c.gridy=1;
			c.gridwidth=1;
			c.gridheight=1;
			c.weightx=0.9;
			c.weighty=0.8;
//			makeButton(jContentPane,this.getJb3(),c,gridBagLayout);
			this.createSubComp(jContentPane, this.getJspSendMsg(), c, gridBagLayout);
			
			//add jpDownHeadImg
			c.gridx=1;
			c.gridy=1;
//			c.fill=GridBagConstraints.NONE;
			c.gridwidth=1;
			
			
			c.weightx=0.1;
			c.weighty=0.2;
//			makeButton(jContentPane,this.getJb4(),c,gridBagLayout);
			this.createSubComp(jContentPane, this.getJpDownHeadImg(), c, gridBagLayout);
			
			
			//add jbSendMsg
			c.gridx=1;
			c.gridy=2;
			c.gridwidth=1;
			this.createSubComp(jContentPane, this.getJbSendMsg(), c, gridBagLayout);
			
		}
		
		return jContentPane;
	}

	public JButton getJb1() {
		if(jb1==null){
			jb1=new JButton("Button1");
		}
		return jb1;
	}

	public JButton getJb2() {
		if(jb2==null){
			jb2=new JButton("Button2");
		}
		return jb2;
	}

	public JButton getJb3() {
		if(jb3==null){
			jb3=new JButton("Button3");
		}
		return jb3;
	}

	public JButton getJb4() {
		if(jb4==null){
			jb4=new JButton("Button4");
		}
		return jb4;
	}
	
	/**
	 * makeButton with constraints
	 * @param jpanel
	 * @param jb
	 * @param c
	 */
	private void makeButton(JPanel jpanel
			,JButton jb
			,GridBagConstraints c
			,GridBagLayout gridBagLayout){
		gridBagLayout.setConstraints(jb,c);
		jpanel.add(jb);
	}
	
	
	/**
	 * createSubPanel
	 * @param jpanel
	 * @param subJpanel
	 * @param c
	 * @param gridBagLayout
	 */
	private void createSubComp(JPanel jpanel
			,JComponent subPanel
			,GridBagConstraints c
			,GridBagLayout gridBagLayout){
		gridBagLayout.setConstraints(subPanel,c);
		jpanel.add(subPanel);
		
	}

	

	

	public JScrollPane getJspHistory() {
		if(jspHistory==