1、 边界布局(BorderLayout)

2、流式布局(FlowLayout)

3、网格布局(GridLayout)

4、盒子布局(BoxLaYout)

5、空布局(null)

还有其他两种布局,分别是GridBagLayout(网格包布局)、CardLayout(卡片布局)

注意:JFrame和JDialog默认布局为BorderLayout,JPanel和Applet默认布局为FlowLayout

边界布局示例代码:

import java.awt.BorderLayout;

import javax.swing.JButton;

import javax.swing.JFrame;

public class BorderLayoutExample extends JFrame{

JButton btn1=new JButton("东");

JButton btn2=new JButton("南");

JButton btn3=new JButton("西");

JButton btn4=new JButton("北");

JButton btn5=new JButton("中");

BorderLayoutExample(){

init();

this.setTitle("边界布局");

this.setResizable(true);

this.setSize(200, 200);

this.setLocationRelativeTo(null);

this.setDefaultCloseOperation(EXIT_ON_CLOSE);

this.setVisible(true);

}

void init(){

this.setLayout(new BorderLayout(10,5)); //默认为0,0;水平间距10,垂直间距5

this.add(btn1,BorderLayout.EAST);

this.add(btn2,BorderLayout.SOUTH);

this.add(btn3,BorderLayout.WEST);

this.add(btn4,BorderLayout.NORTH);

this.add(btn5,BorderLayout.CENTER);

}

public static void main(String args[]){

new BorderLayoutExample();

}

}

运行结果:

流式布局示例代码:

import java.awt.FlowLayout;

import javax.swing.JButton;

import javax.swing.JFrame;

public class FlowLayoutExample extends JFrame{

JButton btn1=new JButton("one");

JButton btn2=new JButton("two");

JButton btn3=new JButton("three");

JButton btn4=new JButton("four");

JButton btn5=new JButton("five");

FlowLayoutExample(){

init();

this.setTitle("流式布局");

this.setResizable(true);

this.setSize(200, 200);

this.setLocationRelativeTo(null);

this.setDefaultCloseOperation(EXIT_ON_CLOSE);

this.setVisible(true);

}

void init(){

this.setLayout(new FlowLayout(FlowLayout.LEFT,10,5)); //默认为居中;水平间距10,垂直间距5

this.add(btn1);

this.add(btn2);

this.add(btn3);

this.add(btn4);

this.add(btn5);

}

public static void main(String args[]){

new FlowLayoutExample();

}

}

运行结果:

网格布局示例代码:

import java.awt.GridLayout;

import javax.swing.JButton;

import javax.swing.JFrame;

public class GridLayoutExample extends JFrame{

JButton btn1=new JButton("one");

JButton btn2=new JButton("two");

JButton btn3=new JButton("three");

JButton btn4=new JButton("four");

JButton btn5=new JButton("five");

GridLayoutExample(){

init();

this.setTitle("表格布局");

this.setResizable(true);

this.setSize(300, 200);

this.setLocationRelativeTo(null);

this.setDefaultCloseOperation(EXIT_ON_CLOSE);

this.setVisible(true);

}

void init(){

this.setLayout(new GridLayout(2,3,10,5)); //默认为1行,n列;2行3列,水平间距10,垂直间距5

this.add(btn1);

this.add(btn2);

this.add(btn3);

this.add(btn4);

this.add(btn5);

}

public static void main(String args[]){

new GridLayoutExample();

}

}

运行结果:

盒子布局示例代码:

import javax.swing.Box;

import javax.swing.BoxLayout;

import javax.swing.JButton;

import javax.swing.JFrame;

public class BoxLaYoutExample extends JFrame{

JButton btn1=new JButton("one");

JButton btn2=new JButton("two");

JButton btn3=new JButton("three");

JButton btn4=new JButton("four");

JButton btn5=new JButton("five");

BoxLaYoutExample(){

init();

this.setTitle("表格布局");

this.setResizable(true);

this.setSize(300, 200);

this.setLocationRelativeTo(null);

this.setDefaultCloseOperation(EXIT_ON_CLOSE);

this.setVisible(true);

}

void init(){

this.setLayout(new BoxLayout(this.getContentPane(),BoxLayout.X_AXIS));

//可以使用Box容器代替

//Box box = new Box(BoxLayout.Y_AXIS);box.add(btn...);box.add(creat..);

this.add(btn1);

this.add(btn2);

this.getContentPane().add(Box.createHorizontalStrut(10)); //采用x布局时,添加固定宽度组件隔开

//this.getContentPane().add(Box.createVerticalStrut(5)); //采用y布局时,添加固定高度组件隔开

this.add(btn3);

this.add(btn4);

this.add(btn5);

}

public static void main(String args[]){

new BoxLaYoutExample();

}

}

运行结果:

空布局示例代码:

import javax.swing.JButton;

import javax.swing.JFrame;

public class NullLayoutExample extends JFrame{

JButton btn1=new JButton("one");

JButton btn2=new JButton("two");

JButton btn3=new JButton("three");

JButton btn4=new JButton("four");

JButton btn5=new JButton("five");

NullLayoutExample(){

init();

this.setTitle("空布局");

this.setResizable(true);

this.setSize(300, 300);

this.setLocationRelativeTo(null);

this.setDefaultCloseOperation(EXIT_ON_CLOSE);

this.setVisible(true);

}

void init(){

this.setLayout(null);

btn1.setBounds(10, 0, 100, 50); //x坐标10,y坐标0,组件宽100,高50

btn2.setBounds(20, 50, 100, 50);

btn3.setBounds(30, 100, 100, 50);

btn4.setBounds(40, 150, 100, 50);

btn5.setBounds(50, 200, 100, 50);

this.add(btn1);

this.add(btn2);

this.add(btn3);

this.add(btn4);

this.add(btn5);

}

public static void main(String args[]){

new NullLayoutExample();

}

}

运行结果:

布局 java swing_java中Swing五种常见的布局方式相关推荐

  1. Java swing五种常见的布局方式【转载】

    Java swing五种常见的布局方式 1. 边界布局(BorderLayout) 2.流式布局(FlowLayout) 3.网格布局(GridLayout) 4.盒子布局(BoxLaYout) 5. ...

  2. 五种常见的加密方式及常用的加解密工具

    如果你是互联网公司的信息安全从业者,那么你可能会经常需要处理撞库事件,撞库是黑客的无聊"恶作剧".黑客收集已经在互联网上泄露的用户和密码信息,生成对应的字典表,并尝试批量登录其他网 ...

  3. 技术分析中的五种常见指标

    目录 相对强弱指数(RSI) 移动平均线(MA) 平滑异同移动平均线(MACD) 随机相对强弱指数(StochRSI) 布林线指标(BB) 简介 市场交易者通过对技术指标的分析来确定资产价格的未来走势 ...

  4. java简述常见的布局极其特点_请简要说明 Android 中五种常见布局的特点。_学小易找答案...

    [简答题]请简要说明有序广播和无序广播的区别 [简答题]请简要说明 Android 程序结构中 AndroidManifest.xml 的功能? [简答题]简述李村站人工办理进路的作业过程. [简答题 ...

  5. [Android Studio]掌握Android Studio的五种常见控件和五种常见布局

    目录 一.View和ViewGroup 二.Android的五种常见控件 2.1 文本控件 2.1.1 TextView 2.1.2 EditText 2.2 按钮控件 2.2.1 Button 2. ...

  6. Java数组去重的多种方法,[Java教程]JavaScript常见的五种数组去重的方式

    [Java教程]JavaScript常见的五种数组去重的方式 0 2016-12-14 15:00:17 ▓▓▓▓▓▓ 大致介绍 JavaScript的数组去重问题在许多面试中都会遇到,现在做个总结 ...

  7. java中的五种排序方法_用Java排序的五种有用方法

    java中的五种排序方法 Java排序快速概述: 正常的列表: private static List VEGETABLES = Arrays.asList("apple", &q ...

  8. 五种常见的PHP设计模式

    2019独角兽企业重金招聘Python工程师标准>>> 五种常见的PHP设计模式 设计模式 一书将设计模式引入软件社区,该书的作者是 Erich Gamma.Richard Helm ...

  9. Ajax 和 XML: 五种常见 Ajax 模式

    Asynchronous JavaScript + XML(Ajax)无疑是 2006 年最热门的技术术语,且有望在 2007 得到进一步发展.但是对您的应用程序来说它究竟有什么意义呢?Ajax 应用 ...

最新文章

  1. linux redhat、ubuntu系统 docker启动、停止命令
  2. Effective C++ -- 零散知识点整理
  3. kafka-0.10.2.1:Producer生产时无法自动创建Topic
  4. 文本相似度几种计算方法及代码python实现
  5. 入手腾龙SP AF90mm MACRO
  6. java字符串转语音文件_java - Java文字转语音(Spring Boot) - 堆栈内存溢出
  7. hibernate jpa_JPA /Hibernate刷新策略初学者指南
  8. 【Druid IO】Druid的imply方式安装
  9. “数学不好,干啥都不行!”高级开发:方向不对,努力也白费!
  10. 龙芯ejtag 单步调试pmon
  11. 计算机桌面通过网络在拼接屏上显示,一种多个拼接屏实时同步显示方法与流程...
  12. c#Word模板转PDF,c#word模板生成新的word
  13. 如何使用cpolar内网穿透群晖NAS套件
  14. Matlab 中类的定义
  15. java检测kafka是否连接成功,Kafka 消费者失败检测
  16. Android,保存图片并通知系统更新相册
  17. 【目标检测论文解读复现NO.20】基于改进Yolov5的地铁隧道附属设施与衬砌表观病害检测方法
  18. ppt画图输出pdf
  19. 【Java刷题】04_二叉树的左右视图
  20. 基于GLSL的聚光灯效果的绘制

热门文章

  1. 记一次计算机课作文,记一次课堂活动作文
  2. Unity中关于 Mipmap
  3. Redis 6.2 执行hset 操作(源码)
  4. 红米3 android 版本,红米3四款手机有什么不同?红米3系列手机各版本区别详解
  5. vite预构建源码梳理
  6. 模拟手指触摸 Android,手势模拟(Hand Draw 3D Pose Tool FREE)免费版
  7. 基SkyWalking实现对k8s集群中微服务的链路追踪
  8. 坦克大战C++无敌版
  9. c语言语句ppt,C语言的语句.ppt
  10. 真实的生活比戏剧更残酷