📄 layoutdemo.java
字号:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
// 边框布局管理器---------------------------------------------------------------
class BorderLayoutDemo extends JFrame
{
private Container pane;
private int width,height;
private JButton east,south,west,north,center;
private BorderLayout layout;
public BorderLayoutDemo(int w,int h)
{
super("边界布局示例");
width = w;
height = h;
init();
}
private void init()
{
Font f = new Font("宋体",Font.BOLD,24);
layout = new BorderLayout(); // 创建边框布局管理对象
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
pane = this.getContentPane();
pane.setLayout(layout); // 设置内容窗格的布局
east = new JButton("东");
east.setFont(f);
pane.add(east,BorderLayout.EAST);
south = new JButton("南");
south.setFont(f);
pane.add(south,BorderLayout.SOUTH);
west = new JButton("西");
west.setFont(f);
pane.add(west,BorderLayout.WEST);
north = new JButton("北");
north.setFont(f);
pane.add(north,BorderLayout.NORTH);
center = new JButton("中");
center.setFont(f);
pane.add(center,BorderLayout.CENTER);
this.setSize(width,height);
this.setVisible(true);
}
}
// 流式布局管理器---------------------------------------------------------------
class FlowLayoutDemo extends JFrame
{
private Container pane;
private int width,height;
private FlowLayout layout;
public FlowLayoutDemo(int w,int h)
{
super("流式布局示例");
width = w;
height = h;
init();
}
private void init()
{
Font f = new Font("宋体",Font.BOLD,24);
JButton b;
layout = new FlowLayout(FlowLayout.LEFT);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
pane = this.getContentPane();
pane.setLayout(layout);
for(int i=1;i<=10;i++)
{
b = new JButton(Integer.toString(i));
b.setFont(f);
pane.add(b);
}
this.setSize(width,height);
this.setVisible(true);
}
}
// 网格布局管理器---------------------------------------------------------------
class GridLayoutDemo extends JFrame
{
private Container pane;
private int width,height;
private GridLayout layout;
private int rows,cols;
public GridLayoutDemo(int w,int h,int r,int c)
{
super("网格布局示例");
width = w;
height = h;
rows = r;
cols = c;
init();
}
private void init()
{
Font f = new Font("宋体",Font.BOLD,24);
JButton b;
layout = new GridLayout(rows,cols);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
pane = this.getContentPane();
pane.setLayout(layout);
for(int i=1;i<=21;i++)
{
b = new JButton(Integer.toString(i));
b.setFont(f);
pane.add(b);
}
this.setSize(width,height);
this.setVisible(true);
}
}
// BOX布局管理器---------------------------------------------------------------
class BoxLayoutDemo extends JFrame
{
private Container pane;
private int width,height;
private BoxLayout layout;
public BoxLayoutDemo(int w,int h)
{
super("网格布局示例");
width = w;
height = h;
init();
}
private void init()
{
Font f = new Font("宋体",Font.BOLD,24);
JButton b;
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
pane = this.getContentPane();
layout = new BoxLayout(pane,BoxLayout.X_AXIS);
pane.setLayout(layout);
for(int i=1;i<=10;i++)
{
b = new JButton(Integer.toString(i));
b.setFont(f);
pane.add(b);
}
this.setSize(width,height);
this.setVisible(true);
}
}
public class LayoutDemo
{
public static void main(String[] args)
{
//new BorderLayoutDemo(400,300);
//new FlowLayoutDemo(400,300);
//new GridLayoutDemo(400,300,5,6);
new BoxLayoutDemo(400,300);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -