Java Swing布局管理器GridLayout简单示例 |
package com.utstar.factory; import java.awt.*; import java.awt.event.*; import javax.swing.*; import javax.swing.event.*; /** * GridLayout布局治理器是设定行*列的布局,中途 可以增加行或列数,依照增加控件的顺序从左至右从上至下来增加 * 可以设定整体行列中间的 间隔,不能跨行跨列, 实用于控件布局 类似棋盘的 款式 。 * @author HZ20232 * */ public class TestGridLayout extends JFrame{ private static final long serialVersionUID = 6819222900970457455L; private JButton button1; private JButton button2; private JButton button3; private JButton button4; private JButton button5; private JButton button6; public TestGridLayout(){ this.setSize(600,400); this.setTitle("测试"); init(); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); this.setVisible(true); } public void init(){ button1 = new JButton("NORTH"); button2 = new JButton("SOUTH"); button3 = new JButton("EAST"); button4 = new JButton("WEST"); button5 = new JButton("CENTER"); button6 = new JButton("CENTER6"); GridLayout myLayout = new GridLayout(2,3); myLayout.setHgap(10); myLayout.setVgap(10); this.setLayout(myLayout); this.add(button1); this.add(button2); this.add(button3); this.add(button4); this.add(button5); this.add(button6); } public static void main(String args[]){ TestGridLayout test = new TestGridLayout(); } } |