1. 流式布局(FlowLayout)
定义:
通俗地说,流式布局就是根据窗口大小,自动改变窗口内组件的位置。例如:原窗口大小一行可以容纳10个BUTTON,但将窗口缩小后,每行仅能容纳5个BUTTON,此时原先的10个BUTTON中的五个就会自动排列到下一行。
package 布局管理;
import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
public class Layout { //流式布局
public static void main(String[] args) {
Frame frame = new Frame(); //创建一个窗口
frame.setLayout(new FlowLayout(FlowLayout.LEFT)); //将窗口布局设置为流式布局,并设置向左对齐
Button button1 = new Button("button1"); //创建若干个按钮
Button button2 = new Button("button2");
Button button3 = new Button("button3");
Button button4 = new Button("button4");
Button button5 = new Button("button5");
frame.add(button1); //将按钮添加进窗口中
frame.add(button2);
frame.add(button3);
frame.add(button4);
frame.add(button5);
frame.setBounds(200,200,500,500); //设置窗口的位置与大小
frame.setVisible(true); //设置窗口可见性
frame.addWindowListener(new WindowAdapter() { //事件监听:关闭窗口
2. 东西南北中布局(BorderLayout)
定义:
东西南北中布局,顾名思义。将窗口分为东西南北中四个“块儿”,也可以称作上下左右中布局,便于理解。
示例:(省略panel的使用)
package 布局管理;
import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
public class BorderLayout { //东西南北中布局
public static void main(String[] args) {
Frame frame = new Frame();
frame.setLayout(new java.awt.BorderLayout());
Button east = new Button("East");
east.setBackground(new Color(140, 172, 51));
Button west = new Button("West");
west.setBackground(new Color(140, 172, 51));
Button north = new Button("North");
north.setBackground(new Color(38, 222, 135));
Button south = new Button("South");
south.setBackground(new Color(38, 222, 135));
Button center = new Button("Center");
frame.add(east, java.awt.BorderLayout.EAST);
frame.add(west, java.awt.BorderLayout.WEST);
frame.add(north, java.awt.BorderLayout.NORTH);
frame.add(south, java.awt.BorderLayout.SOUTH);
frame.add(center, java.awt.BorderLayout.CENTER);
frame.setBounds(200,200,500,500);
frame.setVisible(true);
frame.addWindowListener(new WindowAdapter() {
3. 表格式布局
定义:
将窗口拆分为若干个表格(拆分为自己需要的表格),再往里添加组件。
示例:
GUI经常使用的“登录”、“注册”界面
package 布局管理;
import javafx.scene.control.PasswordField;
import javafx.scene.layout.Pane;
import sun.security.util.Password;
import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.nio.charset.Charset;
public class GridLayout {
public static void main(String[] args) {
// System.out.println("file.encoding=" + System.getProperties().getProperty("file.encoding"));
// System.out.println("默认是:"+ Charset.defaultCharset().name());
Frame frame = new Frame("表格布局");
frame.setLayout(new java.awt.GridLayout(4,1)); //设置行列数
Panel panel1 = new Panel();
frame.add(panel1);
Panel panel2 = new Panel();
frame.add(panel2);
Panel panel3 = new Panel();
frame.add(panel3);
Panel panel4 = new Panel();
frame.add(panel4);
Label label = new Label("welcome to *** system");
label.setFont(new Font("宋体", Font.PLAIN, 26));
Label label1 = new Label("Account: ");
TextField textField = new TextField();
textField.setColumns(20);
Label label2 = new Label("Password: ");
TextField textField1 = new TextField(); //AWT没有passwordField
textField1.setColumns(20);
textField1.setEchoChar('*');
Button button = new Button("Login");
panel1.add(label);
panel2.add(label1);
panel2.add(textField);
panel3.add(label2);
panel3.add(textField1);
panel4.add(button);
frame.setBounds(200,200,500,250);
frame.setVisible(true);
frame.addWindowListener(new WindowAdapter() {