在Java Swing中,常用的布局管理器有BorderLayout、FlowLayout、GridLayout、BoxLayout和GridBagLayout。最灵活的是GridBagLayout。
基本使用如下,
borderlayout,
import javax.swing.*;
import java.awt.*;public class borderlay
{public static void main(String[] agrs){JFrame frame = new JFrame("BorderLayout 示例");frame.setLayout(new BorderLayout());frame.add(new JButton("East"), BorderLayout.EAST);frame.add(new JButton("West"), BorderLayout.WEST);frame.add(new JButton("South"), BorderLayout.SOUTH);frame.add(new JButton("North"), BorderLayout.NORTH);frame.add(new JButton("Center"), BorderLayout.CENTER);frame.setSize(300, 300);frame.setVisible(true);}
}
flowlayout,
import javax.swing.*;
import java.awt.*;public class flowlay
{public static void main(String[] agrs){JFrame frame = new JFrame("FlowLayout 示例");frame.setLayout(new FlowLayout());for (int i = 1; i <= 5; i++) {frame.add(new JButton("Button " + i));}frame.setSize(300, 300);frame.setVisible(true);}
}
gridlayout,
import javax.swing.*;
import java.awt.*;public class gridlay
{public static void main(String[] agrs){JFrame frame = new JFrame("GridLayout 示例");frame.setLayout(new GridLayout(2, 3)); // 2 rows, 3 columnsfor (int i = 1; i <= 6; i++) {frame.add(new JButton("Button " + i));}frame.setSize(300, 300);frame.setVisible(true);}
}
boxlayout,
import javax.swing.*;
import java.awt.*;public class boxlay
{public static void main(String[] agrs){JFrame frame = new JFrame("BoxLayout 示例");JPanel panel = new JPanel();panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));for (int i = 1; i <= 5; i++) {panel.add(new JButton("按钮" + i));}frame.getContentPane().add(panel);frame.setSize(100, 300);frame.setVisible(true);}
}
gridbaglay,
import javax.swing.*;
import java.awt.*;public class mygridbag {public static void main(String[] args) {JFrame jf = new JFrame("测试窗口");jf.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);GridBagLayout gridBag = new GridBagLayout(); // 布局管理器GridBagConstraints c = null; // 约束JPanel panel = new JPanel(gridBag);JButton btn01 = new JButton("按钮111111222");JButton btn02 = new JButton("按钮222222222");JButton btn03 = new JButton("按钮3");JButton btn04 = new JButton("单击我");c = new GridBagConstraints();gridBag.addLayoutComponent(btn01, c); // 内部使用的仅是 c 的副本c = new GridBagConstraints();gridBag.addLayoutComponent(btn02, c);c = new GridBagConstraints();c.gridwidth = GridBagConstraints.REMAINDER;c.fill = GridBagConstraints.BOTH;gridBag.addLayoutComponent(btn03, c);c = new GridBagConstraints();c.gridwidth = GridBagConstraints.REMAINDER;c.fill = GridBagConstraints.BOTH;gridBag.addLayoutComponent(btn04, c);panel.add(btn01);panel.add(btn02);panel.add(btn03);panel.add(btn04);jf.setContentPane(panel);jf.pack();jf.setLocationRelativeTo(null);jf.setVisible(true);}
}
看一下GridBagLayout是怎么用的;
先要new一个GridBagLayout的对象gb;
然后定义一个GridBagConstraints的对象c;
把gb关联到panel;
new一些控件;
new出c;
gb调用addLayoutComponent方法,第一个参数是控件,第二个参数是c;
如此一直添加控件;
new了c以后可以默认;也可以再设置c的属性,使控件放到不同位置;
c.gridwidth = GridBagConstraints.REMAINDER;
这样添加的控件将占据一行的剩余;
c.fill = GridBagConstraints.BOTH;
控件填充显示区域;
上面加了4个按钮,前2个按默认放置,第三个放到此行的剩余空间;
然后第4个会转到新行;
此行只有一个控件,它会占据整行;
GridBagConstraints还有其他属性设置,有时间再看;