學習視窗的三種佈局
阿新 • • 發佈:2021-12-12
//設定為流式佈局
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();
//元件--按鈕
Button button1 = new Button("button1");
Button button2 = new Button("button2");
Button button3 = new Button("button3");
//frame.setLayout(new FlowLayout(FlowLayout.RIGHT));
frame.setLayout(new FlowLayout(FlowLayout.LEFT));
frame.setSize(200,300);
//把按鈕新增上去
frame.add(button1);
frame.add(button2);
frame.add(button3);
frame.setVisible(true);
frame.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}
}
=======================================================================================
//東西南北中佈局
public class TestBorderLayout {
public static void main(String[] args) {
Frame frame = new Frame("東西南北中佈局");
Button east = new Button("east");
Button west = new Button("west");
Button south = new Button("south");
Button north = new Button("north");
Button center = new Button("center");
frame.add(east,BorderLayout.EAST);
frame.add(west,BorderLayout.WEST);
frame.add(south,BorderLayout.SOUTH);
frame.add(north,BorderLayout.NORTH);
frame.add(center,BorderLayout.CENTER);
frame.setSize(400,500);
frame.setVisible(true);
frame.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}
============================================================================================
//表格佈局
public class TestGridLayout {
public static void main(String[] args) {
Frame frame = new Frame("表格佈局");
Button bt1 = new Button("bt1");
Button bt2 = new Button("bt2");
Button bt3 = new Button("bt3");
Button bt4 = new Button("bt4");
Button bt5 = new Button("bt5");
Button bt6 = new Button("bt6");
frame.setLayout(new GridLayout(2,3));
frame.add(bt1);
frame.add(bt2);
frame.add(bt3);
frame.add(bt4);
frame.add(bt5);
frame.add(bt6);
frame.pack();
frame.setVisible(true);
frame.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}
}