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==null){
			jspHistory=new JScrollPane(this.getJtaHistory());
			
		}
		return jspHistory;
	}

	public JScrollPane getJspSendMsg() {
		if(jspSendMsg==null){
			jspSendMsg=new JScrollPane(this.getJtaSendMsg());
		}
		return jspSendMsg;
	}

	public JTextArea getJtaHistory() {
		if(jtaHistory==null){
			jtaHistory=new JTextArea("History:");
			jtaHistory.setEditable(false);
		}
		return jtaHistory;
	}

	public JTextArea getJtaSendMsg() {
		if(jtaSendMsg==null){
			jtaSendMsg=new JTextArea();
		}
		return jtaSendMsg;
	}
	
	public JPanel getJpUpHeadImg() {
		if(jpUpHeadImg==null){
			jpUpHeadImg=new JPanel();
			jpUpHeadImg.setLayout(new BorderLayout());
			jpUpHeadImg.add(this.getJlUpHeadImg());
//			jpUpHeadImg.setBackground(new Color(125,125,125));
		}
		return jpUpHeadImg;
	}

	
	public JPanel getJpDownHeadImg() {
		if(jpDownHeadImg==null){
			jpDownHeadImg=new JPanel();
			jpDownHeadImg.setLayout(new BorderLayout());
			jpDownHeadImg.add(this.getJlDownHeadImg());
//			jpDownHeadImg.setBackground(Color.blue);
		}
		return jpDownHeadImg;
	}

	public JLabel getJlUpHeadImg() {
		jlUpHeadImg=new JLabel(new ImageIcon("image/djwanHead.jpg"));
		return jlUpHeadImg;
	}

	public JLabel getJlDownHeadImg() {
		jlDownHeadImg=new JLabel(new ImageIcon("image/xiaoXinXinHead2.jpg"));
		return jlDownHeadImg;
	}

	public JButton getJbSendMsg() {
		if(jbSendMsg==null){
			jbSendMsg=new JButton("send");
		}
		return jbSendMsg;
	}
	

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值