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

gridBagLayout简明例子

    博客分类:
  • java
 
阅读更多

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;
	}
	

}
分享到:
评论

相关推荐

    Java中GridBagLayout的用法

    ### Java中GridBagLayout的用法详解 #### 一、GridBagLayout简介 GridBagLayout是一种高度灵活且功能强大的布局管理器,在Java Swing图形用户界面编程中被广泛使用。相较于其他布局管理器如FlowLayout或...

    GridBagLayout布局器的使用

    Java Swing GridBagLayout,很详细的Demo,对GridBagLayout如何使用做全面的讲解

    网袋布局管理器--GridBagLayout使用介绍

    在Java Swing中,`GridBagLayout`是一种非常灵活的布局管理器,允许开发者精确控制组件在容器中的位置和大小。由于其灵活性,`GridBagLayout`成为创建复杂、自定义布局的理想选择,但它的确比其他布局管理器更复杂。...

    GridBagLayout布局详解

    GridBagLayout 布局详解 GridBagLayout 是 Java Swing 编程中的一种强大且灵活的布局管理器,它可以实现几乎所有你想要的布局。通过设置 GridBagConstraints 对象的参数,可以控制组件在显示区域中的位置、大小和...

    GridBagLayout && JTable 排序

    在Java Swing中,`GridBagLayout`是一种灵活的布局管理器,它允许开发者精确地控制组件在容器中的位置和大小。而`JTable`是Swing提供的一种用于展示表格数据的组件,通常用于数据的显示、编辑和排序。这篇博客文章...

    java网格包GridBagLayout布局管理器专题

    Java中的GridBagLayout布局管理器是Java Swing中用于组织组件的一种高级布局策略,它提供了最大的灵活性,能够处理不同大小的组件并允许它们跨多个网格单元,甚至部分重叠。GridBagLayout通过GridBagConstraints对象...

    Swing的GridBagLayout布局应用详解(附Java源代码)

    本文通过代码示例的方式介绍了Swing的GridBagLayout布局样式的应用,不拘泥于JDK文档教条方式的陈述,图形效果图与文字相结合,讲解直观而浅显易懂,另外,示例代码中还包含了BorderLayout、FlowLayout等布局的应用...

    GridBagLayout布局管理器的运用

    这是为初初学者提供练习的代码,具有很强的指导效果哈~~~

    Java中GridBagLayout管理器实例.pdf

    在Java GUI编程中,`GridBagLayout`是一个强大的布局管理器,它允许开发者精确地控制组件在容器中的位置和大小。与其他布局管理器不同,`GridBagLayout`提供了更大的灵活性,使得开发者能够创建复杂的界面布局。以下...

    matlab开发-GridBagLayout

    在MATLAB编程环境中,`GridBagLayout`是一个用于组织和管理图形用户界面(GUI)组件的强大工具。这个布局管理器允许开发者以灵活的方式排列和调整`uicontrols`(如按钮、文本框等)、`uipanels`和`uicontainers`的...

    matlab开发-GridBagLayout.zip

    GridBagLayout是Java Swing布局管理器之一,但MATLAB的 GUIDE工具(GUI Development Environment)同样支持它,使得我们可以利用GridBagLayout来创建复杂且灵活的布局。GridBagLayout允许在GUI窗口中动态地排列组件...

    java网格包GridBagLayout布局管理器专题借鉴.pdf

    Java中的GridBagLayout布局管理器是Java Swing中最复杂但也是最灵活的布局管理工具,它允许容器中的组件具有不同的大小,可以跨越多个网格,并且组件之间可以部分重叠。理解GridBagLayout的关键在于掌握它的网格单元...

    Java 最重要布局管理器GridBagLayout的使用方法

    在这个例子中,`j4`的`weightx`被设置为非零值,意味着它会在水平方向上拉伸。如果`j4`的`gridwidth`设置为1,而`j6`的`weightx`设置为1,则`j6`所在的列也会拉伸,导致`j4`不会拉伸。因此,要让`j4`跟随`j6`拉伸,...

    java---swing自学例子(有50个例子)

    在 Swing 中,容器(如JFrame)用于承载组件,你可以使用不同的布局管理器(如FlowLayout、BorderLayout、GridLayout、CardLayout和GridBagLayout)来控制组件的排列方式。这些例子会展示如何根据需要选择和使用不同...

    java网格包GridBagLayout布局管理器专题的分析.pdf

    Java中的GridBagLayout布局管理器是Java Swing中最复杂但也是最灵活的布局管理工具,它允许组件在容器中以不同的大小存在,甚至可以跨多个网格并部分重叠。这种布局管理器将容器划分为一系列网格单元,每个组件可以...

    java网格包GridBagLayout布局管理器专题.pdf

    Java 网格包 GridBagLayout 布局管理器专题 Java 网格包 GridBagLayout 布局管理器是 Java 语言中最复杂和灵活的布局管理器之一。与网格布局管理器不同的是,网格包布局管理器允许容器中各个组件的大小各不相同,还...

    Java Swing GridBagLayout网格袋布局的实现

    Java Swing GridBagLayout网格袋布局的实现 Java Swing GridBagLayout网格袋布局是Java Swing中一种常用的布局管理器,能够灵活地管理组件的布局。GridBagLayout通过将组件安排在网格单元格中来管理布局,每个组件...

    java swing布局管理器实例之gridbaglayout,实现左右选择框.doc

    以下是一个简单的例子,展示了如何使用`GridBagLayout`创建一个左右选择框: ```java import javax.swing.*; import java.awt.*; public class GridBagLayoutExample { public static void main(String[] args) {...

    java中GridBagLayout布局管理器的详细讲解.doc

    ### Java中GridBagLayout布局管理器的详细讲解 #### 一、引言 在Java图形用户界面(GUI)设计中,界面布局是非常重要的一个环节。合理的布局不仅能够提高应用程序的美观性,还能够提升用户体验。Java提供了多种...

Global site tag (gtag.js) - Google Analytics