目录
一、使用方法:
二、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 自动生成的方法存根 } }