1、绝对布局管理器
硬性指定组件在容器中的位置和大小
使用绝对布局的步骤:
Container.setLayout(null);// 取消布局管理器
Component.setBounds();//设置组件的大小位置
package Eleven;
import javax.swing.JFrame;
import java.awt.Container;
import javax.swing.JButton;
import javax.swing.WindowConstants;
public class AbsoluteLayout extends JFrame {
public AbsoluteLayout(){
setTitle("本窗体使用绝对布局");
getContentPane().setLayout(null);
setBounds(0,0,200,200);
/*JFrame窗体类包含一个容器类,所有放置在窗体上的组件其实都是放置在这个容器类中的,通过
* getContentPane()方法获取*/
Container container = getContentPane();
JButton b1 = new JButton("按钮1");
JButton b2 = new JButton("按钮2");
//void java.awt.Component.setBounds(int x, int y, int width, int height)
b1.setBounds(60, 70, 100, 20);
b2.setBounds(10, 30, 80, 30);
container.add(b1);
container.add(b2);
setVisible(true);
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);//退出程序,关闭按钮
}
public static void main(String[] args){
new AbsoluteLayout();
}
}
2、流布局管理器FlowLayout
像流一样按指定方向摆放组件,直到占据了这一行的所有空间再往下移一行。
package Eleven;
import javax.swing.JFrame;
import javax.swing.WindowConstants;
import java.awt.Container;
import java.awt.FlowLayout;
import javax.swing.JButton;
public class FlowLayoutPosition extends JFrame{
public FlowLayoutPosition(){
setTitle("FlowLayout");
Container container = getContentPane();
/*Flow layouts are typically used to arrange buttons in a panel.
* It arranges buttons horizontally until no more buttons fit on
* the same line. The line alignment is determined by the align property. The possible values are:
LEFT
RIGHT
CENTER
LEADING
TRAILING
*/
/*
* public FlowLayout(int alignment,int horizGap,int vertGap)
* alignment:FlowLayout.LEFT(单行中左对齐)/FlaoLayout.CENTER/FlowLayout.RIGHT
* horizGap/vertGap:以像素为单位指定组件之间的水平和垂直间隔*/
//Container javax.swing.JFrame.getContentPane()获取窗体的组件容器
getContentPane().setLayout(new FlowLayout(FlowLayout.LEFT,10,10));
for(int i = 0;i
//Component java.awt.Container.add(Component comp)
container.add(new JButton("Button"+i));
}
setSize(300,200);//设置窗体大小
setVisible(true);
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);//设置窗体关闭方式
}
public static void main(String[] args){
new FlowLayoutPosition();
}
}
3、网格布局管理器GridLayout
网格布局中每个组件大小都相同,单元格数由行数列数决定
public GridLayout(int rows,int colums);
public GridLayout(int rows,int colums,int horizGap,int vertGap); //horizGap,vertGap组件之间的间距
package Eleven;
import javax.swing.JFrame;
import javax.swing.WindowConstants;
import java.awt.GridLayout;
import javax.swing.JButton;
public class GridLayoutPosition extends JFrame{
public GridLayoutPosition(){
/*public GridLayout(int rows,int columns,int horizGap,int vertGap)
* 行数、列数可以有一个为0,表示这一行、列可以排列任意多组件*/
final GridLayout gridLayout = new GridLayout(0,3,2,2);
getContentPane().setLayout(gridLayout);
setTitle("GridLayout");
final JButton button1 = new JButton();
button1.setText("B1");
getContentPane().add(button1);
final JButton button2 = new JButton();
button2.setText("B2");
getContentPane().add(button2);
final JButton button3 = new JButton();
button3.setText("B3");
getContentPane().add(button3);
final JButton button4 = new JButton();
button4.setText("B4");
getContentPane().add(button4);
final JButton button5 = new JButton();
button5.setText("B5");
getContentPane().add(button5);
final JButton button6 = new JButton();
button6.setText("B6");
getContentPane().add(button6);
setSize(253,189);
setVisible(true);
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String [] args){
new GridLayoutPosition();
}
}