布局
- FlowLayout 流式布局
- BorderLayout 东西南北中
- GridLayout 表格布局
package com.Swing;
import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
public class TestFlowLayout {
public static void main(String[] args) {
Frame frame = new Frame("FlowLayout");
frame.setLayout(new FlowLayout(FlowLayout.CENTER));
Button button1 = new Button("button1");
Button button2 = new Button("button2");
Button button3 = new Button("button3");
frame.add(button1);
frame.add(button2);
frame.add(button3);
frame.setVisible(true);
frame.setSize(500,500);
frame.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
System.exit(1);
}
});
}
}
class TestBorderLayout{
public static void main(String[] args) {
Frame frame = new Frame("BorderLayout");
frame.setLayout(new BorderLayout());
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,BorderLayout.CENTER);
frame.add(button2,BorderLayout.EAST);
frame.add(button3,BorderLayout.WEST);
frame.add(button4,BorderLayout.SOUTH);
frame.add(button5,BorderLayout.NORTH);
frame.setVisible(true);
frame.setSize(500,500);
frame.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
System.exit(1);
}
});
}
}
class TestGridLayout{
public static void main(String[] args) {
Frame frame = new Frame("BorderLayout");
frame.setLayout(new GridLayout(3,2));
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.setVisible(true);
frame.setSize(500,500);
frame.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
System.exit(1);
}
});
}
}