随时随地阅读更多技术实战干货,获取项目源码、学习资料,请关注源代码社区公众号(ydmsq666)
package com;
import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
public class LayoutFrame extends JFrame{
public LayoutFrame(){
//1.将布局管理器设置为绝对布局。组件的定位通过位置和大小定位
// this.setLayout(null);
// JButton b=new JButton("查找");
// b.setBounds(400, 50, 60, 50);
// this.add(b);
//2.将布局管理器设置为边框布局
//边框布局特点:将窗体分为东、西、南、北、中五个部分,每个部分放置一个组件,组件大小随窗体的变化而变化
//边框布局是JFrame和Frame默认的布局管理器
// this.setLayout(new BorderLayout());
// JButton j1=new JButton("中");
// this.add(j1);
// JButton j2=new JButton("东");
// this.add(j2,BorderLayout.EAST);
// JButton j3=new JButton("西");
// this.add(j3,BorderLayout.WEST);
// JButton j4=new JButton("南");
// this.add(j4,BorderLayout.SOUTH);
// JButton j5=new JButton("北");
// this.add(j5,BorderLayout.NORTH);
//3.将窗体布局方式设置为流式布局
//流式布局特点:将组件从上到下,从左到右进行排列,以组件内容定位组件大小,超出屏幕组件自动换行
//流式布局是JPane默认的布局管理器
// this.setLayout(new FlowLayout());
// this.add(new JButton("1"));
// this.add(new JButton("2"));
// this.add(new JButton("3"));
// this.add(new JButton("4"));
// this.add(new JButton("5"));
//
//4.将布局管理器设置为网格布局
//网格布局特点:将窗体等分为大小相等的几个部分,每个部分放置一个组件。组件大小随屏幕大小变化而变化
this.setLayout(new GridLayout(3,3));
this.add(new JButton("1"));
this.add(new JButton("2"));
this.add(new JButton("3"));
this.add(new JButton("4"));
this.add(new JButton("5"));
this.add(new JButton("6"));
this.add(new JButton("7"));
this.add(new JButton("8"));
this.add(new JButton("9"));
this.setSize(340,270);
this.setVisible(true);
this.setDefaultCloseOperation(3);
this.setLocationRelativeTo(null);
this.setResizable(true);
}
public static void main(String[] args) {
LayoutFrame s=new LayoutFrame();
}
}