网格组布局管理器

目录

一、使用方法:

 二、gridx、gridy

三、gridwidth、gridheight

 四、fill属性

 四、anchor属性

五、insets属性

​ 六、ipadx、ipady


一、使用方法:

 

 

 二、gridx、gridy

package 网格布局管理器;

import java.awt.Container;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;

import javax.swing.*;

import Swing程序设计.GildLayout;

public class gridxgridy {
	JFrame f=new JFrame();
	Container c;
	void createFrame() {
		 c=f.getContentPane();
		c.setLayout(new GridBagLayout());
		f.setSize(800,600);
		f.setLocationRelativeTo(null);//默认在窗体的中间
		f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		
	}
	void init() {
		GridBagConstraints g1=new GridBagConstraints();
		c.add(new JButton("组件1"),g1);
		GridBagConstraints g2=new GridBagConstraints();
		g2.gridx=1;
		g2.gridy=1;
		c.add(new JButton("组件2"),g2);
		GridBagConstraints g3=new GridBagConstraints();
		g3.gridx=3;
		g3.gridy=3;
		c.add(new JButton("组件3"),g3);
		
	}
	public static void main(String[] args) {
		gridxgridy d=new gridxgridy();
		d.createFrame();
		d.init();
		d.f.setVisible(true);
		// TODO 自动生成的方法存根

	}

}

三、gridwidth、gridheight

package 网格布局管理器;

import java.awt.Container;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;

import javax.swing.*;

import Swing程序设计.GildLayout;

public class gridwidthgridheigth {
	JFrame f=new JFrame();
	Container c;
	void createFrame() {
		 c=f.getContentPane();
		c.setLayout(new GridBagLayout());
		f.setSize(800,600);
		f.setLocationRelativeTo(null);//默认在窗体的中间
		f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		
	}
	void init() {
		GridBagConstraints g1=new GridBagConstraints();
		c.add(new JButton("组件1"),g1);
		GridBagConstraints g2=new GridBagConstraints();
		g2.gridx=1;
		g2.gridy=1;
		c.add(new JButton("组件2"),g2);
		GridBagConstraints g3=new GridBagConstraints();
		g3.gridx=3;
		g3.gridy=3;
		c.add(new JButton("组件3"),g3);
	}
     void creatButton(){
    	 for(int i=0;i<=9;i++) {
		GridBagConstraints g1=new GridBagConstraints();
		g1.gridx=i;
		g1.gridy=0;
		c.add(new JButton("组件1"), g1);
		GridBagConstraints g2=new GridBagConstraints();
		
		g2.gridx=0;
		g2.gridy=i;
		c.add(new JButton("组件2"), g2);
//		GridBagConstraints g1=new GridBagConstraints();
//		g1.gridx=1;
//		g1.gridy=1;
//		c.add(new JButton("组件"), g1);
	}
     }
	
	public static void main(String[] args) {
		gridwidthgridheigth d=new gridwidthgridheigth();
		d.createFrame();
		d.creatButton();
		d.f.setVisible(true);
		// TODO 自动生成的方法存根

	}

}

 

package 网格布局管理器;

import java.awt.Container;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;

import javax.swing.*;

import Swing程序设计.GildLayout;

public class gridwidthgridheigth {
	JFrame f=new JFrame();
	Container c;
	void createFrame() {
		 c=f.getContentPane();
		c.setLayout(new GridBagLayout());
		f.setSize(800,600);
		f.setLocationRelativeTo(null);//默认在窗体的中间
		f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		
	}
	void init() {
		GridBagConstraints g1=new GridBagConstraints();
		c.add(new JButton("组件1"),g1);
		GridBagConstraints g2=new GridBagConstraints();
		g2.gridx=1;
		g2.gridy=1;
		c.add(new JButton("组件2"),g2);
		GridBagConstraints g3=new GridBagConstraints();
		g3.gridx=3;
		g3.gridy=3;
		c.add(new JButton("组件3"),g3);
	}
     void creatButton(){
    	 for(int i=0;i<=9;i++) {
		GridBagConstraints g1=new GridBagConstraints();
		g1.gridx=i;
		g1.gridy=0;
		c.add(new JButton("组件1"), g1);
		GridBagConstraints g2=new GridBagConstraints();
		
		g2.gridx=0;
		g2.gridy=i;
		c.add(new JButton("组件2"), g2);
//		GridBagConstraints g1=new GridBagConstraints();
//		g1.gridx=1;
//		g1.gridy=1;
//		c.add(new JButton("组件"), g1);
	}
     }
     void init2() {
 		GridBagConstraints g1=new GridBagConstraints();
 		c.add(new JButton("组件1"),g1);
 		GridBagConstraints g2=new GridBagConstraints();
 		g2.gridx=1;
 		g2.gridy=1;
 		g2.gridwidth=2;
 		g2.gridheight=1;
 		c.add(new JButton("组件2"),g2);
 		GridBagConstraints g3=new GridBagConstraints();
 		g3.gridx=3;
 		g3.gridy=3;
 		g3.gridwidth=4;
 		g3.gridheight=2;
 		c.add(new JButton("组件3"),g3);
 	}
	
	public static void main(String[] args) {
		gridwidthgridheigth d=new gridwidthgridheigth();
		d.createFrame();
		d.creatButton();
		d.init2();
		d.f.setVisible(true);
		// TODO 自动生成的方法存根

	}

}

 

 四、fill属性

package 网格布局管理器;

import java.awt.Container;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;

import javax.swing.JButton;
import javax.swing.JFrame;



public class fill {
		JFrame f=new JFrame();
		Container c;
		void createFrame() {
			 c=f.getContentPane();
			c.setLayout(new GridBagLayout());
			f.setSize(800,600);
			f.setLocationRelativeTo(null);//默认在窗体的中间
			f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
			
		}
		void init() {
			GridBagConstraints g1=new GridBagConstraints();
			
			g1.gridx=1;
			g1.gridy=1;
			g1.gridwidth=2;
	 		g1.gridheight=2;
	 		c.add(new JButton("组件1"),g1);
	 		
	 		GridBagConstraints g2=new GridBagConstraints();
			g2.gridx=3;
			g2.gridy=3;
			g2.gridwidth=2;
	 		g2.gridheight=2;
			g2.fill=GridBagConstraints.HORIZONTAL;
			c.add(new JButton("组件2"),g2);
			
			GridBagConstraints g3=new GridBagConstraints();
			g3.gridx=5;
			g3.gridy=5;
			g3.gridwidth=2;
	 		g3.gridheight=2;
	 		g3.fill=GridBagConstraints.VERTICAL;
			c.add(new JButton("组件3"),g3);
			
			GridBagConstraints g4=new GridBagConstraints();
			g4.gridx=7;
			g4.gridy=7;
			g4.gridwidth=2;
	 		g4.gridheight=2;
	 		g4.fill=GridBagConstraints.BOTH;
			c.add(new JButton("组件4"),g4);
		}
	     void creatButton(){
	    	 for(int i=0;i<=9;i++) {
			GridBagConstraints g1=new GridBagConstraints();
			g1.gridx=i;
			g1.gridy=0;
			c.add(new JButton("组件1"), g1);
			GridBagConstraints g2=new GridBagConstraints();
			
			g2.gridx=0;
			g2.gridy=i;
			c.add(new JButton("组件2"), g2);
//			GridBagConstraints g1=new GridBagConstraints();
//			g1.gridx=1;
//			g1.gridy=1;
//			c.add(new JButton("组件"), g1);
		}
	     }

	public static void main(String[] args) {
		fill d=new fill();
		d.createFrame();
		d.creatButton();
		d.init();
		d.f.setVisible(true);
		// TODO 自动生成的方法存根

	}

}

 

 四、anchor属性

五、insets属性 六、ipadx、ipady

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小郭同学忒骚了

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值