1.流式布局(默认) Flowlayout
package GUI;
import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
// 三种布局
public class Test05_FlowLayout {
public static void main(String[] args) {
// 流式布局
Frame frame=new Frame("三种布局");
frame.setVisible(true);
// frame.setLayout(new FlowLayout()); // 默认为居中
frame.setLayout(new FlowLayout(FlowLayout.LEFT));
frame.setSize(200,100);
Button button1=new Button("1");
Button button2=new Button("2");
Button button3=new Button("3");
frame.add(button1);
frame.add(button2);
frame.add(button3);
System.out.println("詹");
frame.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
System.exit(0); // 强制退出程序
}
});
2.区块布局 BorderLayout
package GUI;
import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
public class Test05_BorderLayout {
public static void main(String[] args) {
Frame frame=new Frame("BorderLayout");
frame.setSize(200,200);
frame.setVisible(true);
Button e=new Button("e");
Button s=new Button("s");
Button w=new Button("w");
Button n=new Button("n");
Button c=new Button("c");
frame.add(e,BorderLayout.EAST);
frame.add(s,BorderLayout.SOUTH);
frame.add(w,BorderLayout.WEST);
frame.add(n,BorderLayout.NORTH);
frame.add(c,BorderLayout.CENTER);
frame.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
System.exit(0); // 强制退出程序
}
});
}
}
3.表格布局 GridLayout
package GUI;
import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
public class Test05_BorderLayout {
public static void main(String[] args) {
Frame frame=new Frame("BorderLayout");
frame.setSize(200,200);
frame.setVisible(true);
Button e=new Button("e");
Button s=new Button("s");
Button w=new Button("w");
Button n=new Button("n");
Button c=new Button("c");
frame.add(e,BorderLayout.EAST);
frame.add(s,BorderLayout.SOUTH);
frame.add(w,BorderLayout.WEST);
frame.add(n,BorderLayout.NORTH);
frame.add(c,BorderLayout.CENTER);
frame.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
System.exit(0); // 强制退出程序
}
});
}
}
标签:java,管理器,05,frame,BorderLayout,add,三种,new,Button
From: https://www.cnblogs.com/zhanjianhai/p/17533589.html