一、复杂布局管理
箱式布局:
箱式布局比网格布局灵活一些,可以布局单行或者单列组件。对于容器Box其默认布局管理器就是箱式布局BoxLayout,也可以将JPanel设置为箱式布局。
创建一个使用箱式布局的容器
Box hBox = Box.createHorizontalBox(); //创建水平的箱式布局容器
Box vBox = Box.createVerticalBox(); //创建垂直的箱式布局容器
添加组件的方法与一般容器相同。对于水平箱子里,组件从左到右排列,垂直的箱子,组件从上到下排列。其内每个组件都有首选尺寸、最大尺寸和最小尺寸。
对于箱式布局中组件默认是没有间距的,需要间距的话可以添加不可见的填充件。有三种填充件。
1、支柱,类似一跟线添加进去限制组件间距离。
2、固定区,类似一个长方体添加进去限制组件间距离,将相邻组件分开并设置另一个方向上的最小尺寸。
3、胶水。。类似弹簧,将组件最大距离的相互弹开。
import javax.swing.*;
import java.awt.*;
public class BoxTest01
{
public static void main(String[] args)
{
BoxFrame sp = new BoxFrame();
sp.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
sp.setVisible(true);
sp.setTitle("Box");
}
}
class BoxFrame extends JFrame
{
public BoxFrame()
{
setSize(DEFAULT_WIDTH,DEFAULT_HEIGHT);
Box hBox1 = Box.createHorizontalBox();
JLabel label1 = new JLabel("标签1");
JTextField jft1 = new JTextField(20);
jft1.setMaximumSize(jft1.getPreferredSize());
hBox1.add(label1);
hBox1.add(Box.createHorizontalStrut(100));
hBox1.add(jft1);
Box hBox2 = Box.createHorizontalBox();
JLabel label2 = new JLabel("标签2");
JTextField jft2 = new JTextField(30);
jft2.setMaximumSize(jft2.getPreferredSize());
hBox2.add(label2);
hBox2.add(Box.createRigidArea(new Dimension(10,20)));
hBox2.add(jft2);
Box hBox3 = Box.createHorizontalBox();
JButton but1=new JButton("确认");
JButton but2=new JButton("取消");
hBox3.add(but1);
hBox3.add(Box.createHorizontalGlue());
hBox3.add(but2);
Box box = Box.createVerticalBox();
box.add(hBox1);
box.add(hBox2);
box.add(Box.createVerticalGlue());
box.add(hBox3);
add(box,BorderLayout.CENTER);
//setResizable(false); //设置组件大小是否可调节
}
public static final int DEFAULT_WIDTH = 650;
public static final int DEFAULT_HEIGHT = 450;
}
网格组布局:
使用网格组布局的步骤
1、建立GridBagLayout对象,不需要指定行列数。
2、将GridBagLayout对象设置为组件的布局管理器。
3、为每个组件建立GridBagConstraints类型的对象。
4、调用约束 add(component,constraints)
其中关于约束GridBagConstraints类中重要约束
1、gridx、gridy、gridwidth、gridheight约束了网格在组件中的位置。
2、增量字段(weightx,weighty)描述窗口缩放时标签大小变化,为0时标签大小始终不变。
3、fill和anchor参数,描述组件是否拉伸至整个区域,以及如果组件未填充整个区域时,指定其位置。
4、填塞,在组件周围添加附加的空白区域。(内部填塞与外部填塞之分)
import javax.swing.*;
import java.awt.*;
public class GridBagTest01
{
public static void main(String[] args)
{
GridBagFrame sp = new GridBagFrame();
sp.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
sp.setVisible(true);
sp.setTitle("GridBag");
}
}
class GridBagFrame extends JFrame
{
public GridBagFrame()
{
setSize(DEFAULT_WIDTH,DEFAULT_HEIGHT);
GridBagLayout bagLayout = new GridBagLayout();
setLayout(bagLayout);
JLabel label1 = new JLabel("标签1");
JLabel label2 = new JLabel("标签2");
JButton buton1 = new JButton("确认");
JButton buton2 = new JButton("取消");
JButton buton3 = new JButton("按键3");
JButton buton4 = new JButton("按键4");
JButton buton5 = new JButton("按键5");
JButton buton6 = new JButton("按键6");
JTextArea textArea1 = new JTextArea();
GridBagConstraints GridLabBag1 = new MyGridBagConst(0,0,30,15,100,100,GridBagConstraints.NONE,GridBagConstraints.CENTER,new Insets(2,3,1,3),3,2);
GridBagConstraints GridLabBag2 = new MyGridBagConst(0,17,30,15,100,100,GridBagConstraints.NONE,GridBagConstraints.CENTER,new Insets(2,3,1,3),3,2);
GridBagConstraints GridButBag1 = new MyGridBagConst(37,0,30,15,100,100,GridBagConstraints.BOTH,GridBagConstraints.CENTER,new Insets(2,3,1,3),3,2);
GridBagConstraints GridButBag2 = new MyGridBagConst(37,17,30,15,100,100,GridBagConstraints.BOTH,GridBagConstraints.CENTER,new Insets(2,3,1,3),3,2);
GridBagConstraints GridButBag3 = new MyGridBagConst(20,60,60,15,100,100,GridBagConstraints.BOTH,GridBagConstraints.CENTER,new Insets(2,3,1,3),3,2);
GridBagConstraints GridButBag4 = new MyGridBagConst(20,90,20,15,100,100,GridBagConstraints.BOTH,GridBagConstraints.CENTER,new Insets(2,3,1,3),3,2);
GridBagConstraints GridButBag5 = new MyGridBagConst(40,90,20,15,100,100,GridBagConstraints.BOTH,GridBagConstraints.CENTER,new Insets(2,3,1,3),3,2);
GridBagConstraints GridButBag6 = new MyGridBagConst(60,90,20,15,100,100,GridBagConstraints.BOTH,GridBagConstraints.CENTER,new Insets(2,3,1,3),3,2);
GridBagConstraints GridTxtBag1 = new MyGridBagConst(90,0,180,180,100,100,GridBagConstraints.BOTH,GridBagConstraints.CENTER,new Insets(2,3,1,3),3,2);
add(label1,GridLabBag1);
add(label2,GridLabBag2);
add(buton1,GridButBag1);
add(buton2,GridButBag2);
add(buton3,GridButBag3);
add(buton4,GridButBag4);
add(buton5,GridButBag5);
add(buton6,GridButBag6);
add(textArea1,GridTxtBag1);
}
public static final int DEFAULT_WIDTH = 250;
public static final int DEFAULT_HEIGHT = 450;
}
class MyGridBagConst extends GridBagConstraints
{
public MyGridBagConst(int gridx,int gridy,int gridwidth,int gridheight)
{
super();
this.gridx=gridx;
this.gridy=gridy;
this.gridwidth=gridwidth;
this.gridheight=gridheight;
}
public MyGridBagConst(int gridx,int gridy,int gridwidth,int gridheight,int weightx,int weighty)
{
this(gridx,gridy,gridwidth,gridheight);
this.weightx=weightx;
this.weighty=weighty;
}
public MyGridBagConst(int gridx,int gridy,int gridwidth,int gridheight,int weightx,int weighty,int fill,int anchor)
{
this(gridx,gridy,gridwidth,gridheight,weightx,weighty);
this.fill=fill;
this.anchor=anchor;
}
public MyGridBagConst(int gridx,int gridy,int gridwidth,int gridheight,int weightx,int weighty,int fill,int anchor,Insets insets,int ipadx,int ipady)
{
this(gridx,gridy,gridwidth,gridheight,weightx,weighty,fill,anchor);
this.insets=insets;
this.ipadx=ipadx;
this.ipady=ipady;
}
}