GridLayout是一种非常简单的布局,它的每个组件的大小都相等。这里不许多说,直接上代码和图片说明
/* * GridLayoutDemo.java requires no other files. */ import java.awt.Container; import java.awt.GridBagConstraints; import java.awt.GridBagLayout; import java.awt.GridLayout; import java.awt.Insets; import javax.swing.JButton; import javax.swing.JFrame; public class GridLayouts extends JFrame{ private JButton btn1 = new JButton("Button1"); private JButton btn2 = new JButton("Button2"); private JButton btn3 = new JButton("Button3 what a fine day"); private JButton btn4 = new JButton("Button4 what a fine da"); private JButton btn5 = new JButton("Button5 what a fine d"); private JButton btn6 = new JButton("Button6 what a fines"); GridLayouts() { super(); initComponent(); } private void initComponent() { Container container = this.getContentPane(); container.setLayout(new GridLayout(2,3)); container.add(btn1); container.add(btn2); container.add(btn3); container.add(btn4); container.add(btn5); container.add(btn6); } public static void main(String[] args) { GridLayouts frame = new GridLayouts(); frame.pack(); frame.setVisible(true); } }