可在构造方法中设置不同的组件间距,行距即对齐方式。
JPanel panel=new JPanel(new FlowLayout(FlowLayout.RIGHT,20,40));
右对齐,组件之间水平间距20个像素,数值间距40个像素
JPanel panel=new JPanel(new FlowLayout(FlowLayout.LEFT));
左对齐,水平和竖直间距为缺省值(默认值)5
JPanel panel=new JPanel(new FlowLayout());
默认方式,居中对齐,水平和竖直间距值均为默认值5
import javax.swing.*; import java.awt.*; public class TestFlowLayout extends JFrame { /**首先定义8个按钮用来测试*/ JButton[] jButtons=new JButton[8]; /**关于布局的定义方法有很多,这里展示最常见的一种方法*/ JPanel panel=new JPanel(new FlowLayout()); public TestFlowLayout(){ for (int i=0;i<jButtons.length;i++){ jButtons[i]=new JButton("按钮"+i); panel.add(jButtons[i]); } this.add(panel); this.setTitle("流式布局测试"); this.setSize(400,300); this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); this.setVisible(true); } public static void main(String[] args){ new TestFlowLayout(); } }
import javax.swing.*; import java.awt.*; public class TestBorderLayout { public static void main(String[] args) { JFrame jf = new JFrame("BorderLayout测试管理"); jf.setSize(350, 250); jf.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); // 创建内容面包容器,指定使用 边界布局 JPanel panel = new JPanel(new java.awt.BorderLayout()); JButton btnN = new JButton("North"); JButton btnS = new JButton("South"); JButton btnW = new JButton("West"); JButton btnE = new JButton("East"); JButton btnC = new JButton("Center"); // 把 5 个按钮添加到容器中的 5 个方位,注意后面的一定要大写 panel.add(btnN, java.awt.BorderLayout.NORTH); panel.add(btnS, java.awt.BorderLayout.SOUTH); panel.add(btnW, java.awt.BorderLayout.WEST); panel.add(btnE, java.awt.BorderLayout.EAST); panel.add(btnC, java.awt.BorderLayout.CENTER); jf.setContentPane(panel); jf.setLocationRelativeTo(null); jf.setVisible(true); } }
import javax.swing.*; import java.awt.*; public class TestGridLayout extends JFrame { /**首先定义8个按钮用来测试*/ JButton[] jButtons=new JButton[8]; /**关于布局的定义方法有很多,这里展示最常见的一种方法*/ JPanel panel=new JPanel(new GridLayout(2,4)); public TestGridLayout(){ for (int i=0;i<jButtons.length;i++){ jButtons[i]=new JButton("按钮"+i); panel.add(jButtons[i]); } this.add(panel); this.setTitle("表格布局测试"); this.setSize(400,300); this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); this.setVisible(true); } public static void main(String[] args){ new TestGridLayout(); } }
布局名称 | 说明 | 注意 |
---|---|---|
CardLayout | 能够帮助用户处理两个以致更多的成员共享同一显示空间,就好像一叠卡片叠在一起。 | 在一张卡片中只能显示一个组件,因此可以使用容器嵌套的方法显示多个组件 |
GridBagLayout | 最灵活,最复杂的布局管理器,各组件所占空间可以不相同且灵活规定 | |
绝对布局 | 流程:
|
import javax.swing.*; import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; public class TestCardLayout { public static void main(String[] args) { JFrame jf = new JFrame("卡片布局"); jf.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); jf.setSize(300, 200); // 创建卡片布局,卡片间水平和竖直间隔为 10 final java.awt.CardLayout layout = new java.awt.CardLayout(10, 10); // 创建内容面板容器,指定布局管理器 final JPanel panel = new JPanel(layout); JButton btn01 = new JButton("哈哈"); JButton btn02 = new JButton("嘿嘿"); JButton btn03 = new JButton("呼呼"); panel.add(btn01, "btn01"); panel.add(btn02, "btn02"); panel.add(btn03, "btn03"); // 先显示第二个 layout.show(panel, "btn02"); jf.setContentPane(panel); jf.setLocationRelativeTo(null); jf.setVisible(true); // 每间隔2秒切换显示下一个 new Timer(2000, new ActionListener() { @Override public void actionPerformed(ActionEvent e) { layout.next(panel); } }).start(); } }
import javax.swing.*; import java.awt.*; public class GridBagLayout { public static void main(String[] args) { JFrame jf = new JFrame("测试窗口"); jf.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); java.awt.GridBagLayout gridBag = new java.awt.GridBagLayout(); GridBagConstraints c = null; // 约束 JPanel panel = new JPanel(gridBag); JButton btn01 = new JButton("1"); JButton btn02 = new JButton("2"); JButton btn03 = new JButton("3"); JButton btn04 = new JButton("4"); JButton btn05 = new JButton("5"); JButton btn06 = new JButton("6"); JButton btn07 = new JButton("7"); JButton btn08 = new JButton("8"); JButton btn09 = new JButton("9"); JButton btn10 = new JButton("0"); /* 添加 组件 和 约束 到 布局管理器 */ // Button01 c = new GridBagConstraints(); gridBag.addLayoutComponent(btn01, c); // 内部使用的仅是 c 的副本 // Button02 c = new GridBagConstraints(); gridBag.addLayoutComponent(btn02, c); // Button03 c = new GridBagConstraints(); gridBag.addLayoutComponent(btn03, c); // Button04 显示区域占满当前行剩余空间(换行),组件填充显示区域 c = new GridBagConstraints(); c.gridwidth = GridBagConstraints.REMAINDER; c.fill = GridBagConstraints.BOTH; gridBag.addLayoutComponent(btn04, c); // Button05 显示区域独占一行(换行),组件填充显示区域 c = new GridBagConstraints(); c.gridwidth = GridBagConstraints.REMAINDER; c.fill = GridBagConstraints.BOTH; gridBag.addLayoutComponent(btn05, c); // Button06 显示区域占到当前尾倒车第二个单元格(下一个组件后需要手动换行),组件填充显示区域 c = new GridBagConstraints(); c.gridwidth = GridBagConstraints.RELATIVE; c.fill = GridBagConstraints.BOTH; gridBag.addLayoutComponent(btn06, c); // Button07 放置在当前行最后一个单元格(换行) c = new GridBagConstraints(); c.gridwidth = GridBagConstraints.REMAINDER; gridBag.addLayoutComponent(btn07, c); // Button08 显示区域占两列,组件填充显示区域 c = new GridBagConstraints(); c.gridheight = 2; c.fill = GridBagConstraints.BOTH; gridBag.addLayoutComponent(btn08, c); // Button09 显示区域占满当前行剩余空间(换行),组件填充显示区域 c = new GridBagConstraints(); c.gridwidth = GridBagConstraints.REMAINDER; c.fill = GridBagConstraints.BOTH; gridBag.addLayoutComponent(btn09, c); // Button10 显示区域占满当前行剩余空间(换行),组件填充显示区域 c = new GridBagConstraints(); c.gridwidth = GridBagConstraints.REMAINDER; c.fill = GridBagConstraints.BOTH; gridBag.addLayoutComponent(btn10, c); /* 添加 组件 到 内容面板 */ panel.add(btn01); panel.add(btn02); panel.add(btn03); panel.add(btn04); panel.add(btn05); panel.add(btn06); panel.add(btn07); panel.add(btn08); panel.add(btn09); panel.add(btn10); jf.setContentPane(panel); //自动调整大小 jf.pack(); jf.setLocationRelativeTo(null); jf.setVisible(true); } }