Java 布局管理器一文汇总
FlowLayout
讲解
这个布局管理器呢,是一个流式布局管理器,会使所有的组件像流水一样排列,从左至右,从上至下,如果排列不下去了就会换行,如果拉开窗口就会继续像流水一样排列。
常用方法及常量
序号 | 方法及常量 | 类型 | 描述 |
---|---|---|---|
1 | public static final int CENTER | 常量 | 居中对齐 |
2 | public static final int LEADING | 常量 | 与容器的开始端对齐方式一样 |
3 | public static final int LEFT | 常量 | 左对齐 |
4 | public static final int RIGHT | 常量 | 右对齐 |
5 | public static final int TRAILING | 常量 | 与容器的结束端对齐方式一样 |
6 | public FlowLayout() | 构造 | 构造一个新的FlowLayout, 居中对齐, 默认的水平和垂直间距是5个单位 |
7 | public FlowLayout(int align)__ | 构造 | 构造一个FlowLayout,并指定对齐方式 |
8 | public FlowLayout(int align.int bgap,int vgap) | 构造 | 指定对齐方式、水平、垂直间距 |
代码实例
package com.learnspringboot.libra;
import javax.swing.*;
import java.awt.*;
/**
* FlowLayoutTest01 类
* 日期:2021/11/22
* 时间:12:42
*
* @author 别欺负小白了好咩
*/
public class FlowLayoutTest01 {
public static void main(String[] args) {
JFrame frame = new JFrame("Welcome!");
// 后两个参数 hgap 和 vgap 是设置组件之间的间隙。
frame.setLayout(new FlowLayout(FlowLayout.CENTER,3,3));
JButton but = null;
int end = 9;
for (int i = 0; i < end; i++) {
but = new JButton("Button --> " + i);
frame.add(but);
}
Toolkit tk = Toolkit.getDefaultToolkit();
Dimension size = tk.getScreenSize();
// 设置窗口的大小
int FrameWidth = 350;
int FrameHeight = 130;
// 设置窗口的位置,通过这个计算会让其在屏幕中心
int screenHeight = size.height / 2 - FrameHeight / 2;
int screenWidth = size.width / 2 - FrameWidth / 2;
// 设置窗口的位置
frame.setLocation(screenWidth,screenHeight);
frame.setSize(FrameWidth,FrameHeight);
// 添加事件监听器,这里指的是当关闭窗口的时候程序退出,这样不用每次关闭了窗口还要去退出程序。
frame.addWindowListener(new MyWindowListener());
frame.setVisible(true);
}
}
运行结果
BorderLayout
常用方法及常量
序号 | 方法及常量 | 类型 | 描 述 |
---|---|---|---|
1 | public static final String EAST | 常量 | 将组件设置在东区域 |
2 | public static final String WEST | 常量 | 将组件设置在西区域 |
3 | public static final String SOUTH | 常量 | 将组件设置在南区域 |
4 | public static final String NORTH | 常量 | 将组件设置在北区域 |
5 | public static final String CENTER | 常量 | 将组件设置在中区域 |
6 | public Border ayout() | 构造 | 构造没有问距的布局器 |
7 | public BorderLayout(int hgap,int vgap) | 构造 | 构造有水平和垂直间距的布局器 |
讲解
这个布局管理器呢,会将一个窗体分为五个部分,分别是东西南北中,所有的组件就会被放置在这五个区域中,可以直接设置放在哪个区域内,如果说不去指定的话,默认放置在中间区域。
代码实例
package com.learnspringboot.libra;
import javax.swing.*;
import java.awt.*;
/**
* BorderLayoutTest01 类
* 日期:2021/11/22
* 时间:13:21
*
* @author 别欺负小白了好咩
*/
public class BorderLayoutTest01 {
public BorderLayoutTest01(String title){
JFrame frame = new JFrame(title);
frame.setLayout(new BorderLayout(3,3));
frame.add(new JButton("东(EAST)"),BorderLayout.EAST);
frame.add(new JButton("西(WEST)"),BorderLayout.WEST);
frame.add(new JButton("南(SOUTH)"),BorderLayout.SOUTH);
frame.add(new JButton("北(NORTH)"),BorderLayout.NORTH);
frame.add(new JButton("中(CENTER)"),BorderLayout.CENTER);
Toolkit tk = Toolkit.getDefaultToolkit();
Dimension size = tk.getScreenSize();
// 设置窗口的大小
int FrameWidth = 350;
int FrameHeight = 130;
// 设置窗口的位置,通过这个计算会让其在屏幕中心
int screenHeight = size.height / 2 - FrameHeight / 2;
int screenWidth = size.width / 2 - FrameWidth / 2;
// 设置窗口的位置
frame.setLocation(screenWidth,screenHeight);
frame.setSize(FrameWidth,FrameHeight);
// 设置窗口的位置
frame.setLocation(screenWidth,screenHeight);
frame.setSize(FrameWidth,FrameHeight);
frame.addWindowListener(new MyWindowListener());
frame.setVisible(true);
}
public BorderLayoutTest01(){
this("Hello World!");
}
}
运行结果
GridLayout
常用方法及常量
序号 | 方法 | 类型 | 描 述 |
---|---|---|---|
1 | public GridLayout(int rows,int cols) | 构造 | 构造一个指定行和列的布局管理器 |
2 | public GridLayout(int rows,int cols,int hgap,int vgap) | 构造 | 构造时指定行和列、水平和垂直问距 |
讲解
这个布局管理器是表格管理器,是将窗口分为具体的行数和列数的表格,然后每一个组件在其中占据一个小格子。
代码实例
package com.learnspringboot.libra;
import javax.swing.*;
import java.awt.*;
/**
* GridLayoutTest01 类
* 日期:2021/11/22
* 时间:13:50
*
* @author 别欺负小白了好咩
*/
public class GridLayoutTest01 {
public GridLayoutTest01(String title){
JFrame frame = new JFrame(title);
frame.setLayout(new GridLayout(3,5,3,3));
JButton but = null;
int end = 15;
for (int i = 0; i < end; i++) {
but = new JButton("Button --> " + i);
frame.add(but);
}
Toolkit tk = Toolkit.getDefaultToolkit();
Dimension size = tk.getScreenSize();
int FrameWidth = 600;
int FrameHeight = 200;
int screenHeight = size.height / 2 - FrameHeight / 2;
int screenWidth = size.width / 2 - FrameWidth / 2;
frame.setLocation(screenWidth,screenHeight);
frame.setSize(FrameWidth,FrameHeight);
frame.addWindowListener(new MyWindowListener());
frame.setVisible(true);
}
public GridLayoutTest01(){
this("Welcome!");
}
}
运行结果
计算器实例
做了一个计算器窗口
以下是代码源码
package com.learnspringboot.libra.calculator;
import javax.swing.*;
import java.awt.*;
/**
* SetFrame 类
* 日期:2021/11/24
* 时间:19:27
*
* @author 别欺负小白了好咩
*/
public class SetFrame {
public SetFrame(JFrame frame,int FrameWidth,int FrameHeight){
Toolkit tk = Toolkit.getDefaultToolkit();
Dimension size = tk.getScreenSize();
int screenHeight = size.height / 2 - FrameHeight / 2;
int screenWidth = size.width / 2 - FrameWidth / 2;
frame.setLocation(screenWidth,screenHeight);
frame.setSize(FrameWidth,FrameHeight);
}
}
package com.learnspringboot.libra.calculator;
/**
* Application 类
* 日期:2021/11/22
* 时间:13:21
*
* @author 别欺负小白了好咩
*/
public class Application {
public static void main(String[] args) {
new MyFrameDraw();
}
}
package com.learnspringboot.libra.calculator;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
/**
* MyWindowListener 类
* 日期:2021/11/22
* 时间:12:51
*
* @author 别欺负小白了好咩
*/
public class MyWindowListener implements WindowListener {
@Override
public void windowOpened(WindowEvent e) {
}
@Override
public void windowClosing(WindowEvent arg0){
System.out.println("窗口关闭");
System.exit(1);
}
@Override
public void windowClosed(WindowEvent e) {
}
@Override
public void windowIconified(WindowEvent e) {
}
@Override
public void windowDeiconified(WindowEvent e) {
}
@Override
public void windowActivated(WindowEvent e) {
}
@Override
public void windowDeactivated(WindowEvent e) {
}
}
package com.learnspringboot.libra.calculator;
import javax.swing.*;
/**
* MyPanel 类
* 日期:2021/11/24
* 时间:18:47
*
* @author 别欺负小白了好咩
*/
public class MyPanelDraw {
public MyPanelDraw(){
}
public JPanel getPanel(){
JPanel jPanel = new JPanel();
return jPanel;
}
}
package com.learnspringboot.libra.calculator;
import com.learnspringboot.libra.frame.MyWindowListener;
import javax.swing.*;
import java.awt.*;
/**
* MyFrameDraw 类
* 日期:2021/11/24
* 时间:7:59
*
* @author 别欺负小白了好咩
*/
public class MyFrameDraw {
public MyFrameDraw(int FrameWidth, int FrameHeight){
JFrame frame = new JFrame();
new SetFrame(frame,FrameWidth,FrameHeight);
frame.setLayout(null);
frame.add(getPanel01());
frame.add(getPanel02());
frame.add(getPanel03());
frame.setVisible(true);
frame.addWindowListener(new MyWindowListener());
}
public JPanel getPanel01(){
JPanel jPanel = new JPanel(new FlowLayout());
JTextField text = new JTextField(17);
JButton cleanButton = new JButton("=");
jPanel.add(text);
jPanel.add(cleanButton);
jPanel.setBounds(10,0,290,50);
return jPanel;
}
public JPanel getPanel02(){
JPanel jPanel = new JPanel(new GridLayout(3,3));
int end = 10;
for (int i = 1; i < end; i++) {
JButton but = new JButton(""+i);
jPanel.add(but);
}
jPanel.setBounds(10,50,290,100);
return jPanel;
}
public JPanel getPanel03(){
JPanel jPanel = new JPanel(new GridLayout(2,3));
jPanel.add(new Button(" 0"));
jPanel.add(new Button(" +"));
jPanel.add(new Button(" -"));
jPanel.add(new Button(" *"));
jPanel.add(new Button(" /"));
jPanel.add(new Button(" ."));
jPanel.setBounds(10,150,290,100);
return jPanel;
}
public MyFrameDraw(){
this(325,300);
}
}
小结
本文到这里就结束了,如果喜欢的话还请点赞多多支持,谢谢~