java中Swing五种常见的布局方式

 更新时间:2018年03月01日 08:15:20   作者:彬菌  
本文通过代码示例给大家详细讲解了java中Swing五种常见的布局方式,以及相关注意知识点,有兴趣的朋友参考学习下。

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();
	}
}

运行结果:

相关文章

  • Java8处理List的双层循环问题

    Java8处理List的双层循环问题

    这篇文章主要介绍了Java8处理List的双层循环问题,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2020-08-08
  • java中把汉字转换成简拼的实现代码

    java中把汉字转换成简拼的实现代码

    本篇文章是对在java中把汉字转换成简拼的实现方法进行了详细的分析介绍,需要的朋友参考下
    2013-05-05
  • Spring Boot 整合 Fisco Bcos部署、调用区块链合约的案例

    Spring Boot 整合 Fisco Bcos部署、调用区块链合约的案例

    本篇文章介绍 Spring Boot 整合 Fisco Bcos 的相关技术,最最重要的技术点,部署、调用区块链合约的工程案例,本文通过流程分析给大家介绍的非常详细,需要的朋友参考下吧
    2022-01-01
  • spring mvc中的@ModelAttribute注解示例介绍

    spring mvc中的@ModelAttribute注解示例介绍

    在Spring mvc中,注解@ModelAttribute是一个非常常用的注解,下面这篇文章主要给大家介绍了关于spring mvc中@ModelAttribute注解的相关资料,文中通过示例代码介绍的非常详细,需要的朋友可以参考下。
    2017-09-09
  • Java图像处理之RGB调色面板

    Java图像处理之RGB调色面板

    这篇文章主要为大家详细介绍了Java图像处理之RGB调色面板,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2022-06-06
  • Java Enum和String及int的相互转化示例

    Java Enum和String及int的相互转化示例

    这篇文章主要介绍了Java Enum和String及int的相互转化示例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2020-06-06
  • java自定义线程池的原理简介

    java自定义线程池的原理简介

    这篇文章主要介绍了java自定义线程池的原理详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2019-08-08
  • Clojure 与Java对比少数据结构多函数胜过多个单独类的优点

    Clojure 与Java对比少数据结构多函数胜过多个单独类的优点

    这篇文章主要介绍了Clojure 与Java对比少数据结构多函数胜过多个单独类的优点,在Clojure中,我们一次又一次地使用相同的数据结构,并在其上运行许多函,更多相关介绍需要的朋友可以参考一下下面文章内容
    2022-06-06
  • Mybatis-Plus实现SQL拦截器的示例

    Mybatis-Plus实现SQL拦截器的示例

    这篇文章主要介绍了Mybatis-Plus实现一个SQL拦截器,通过使用SQL拦截器,开发人员可以在执行SQL语句之前或之后对其进行修改或记录,从而更好地控制和优化数据库操作,对Mybatis-Plus SQL拦截器相关知识感兴趣的朋友一起看看吧
    2023-05-05
  • 一文带你深入了解Java TreeMap

    一文带你深入了解Java TreeMap

    TreeMap是Map家族中的一员,也是用来存放key-value键值对的。平时在工作中使用的可能并不多。本文将基于jdk8对其做一个讲解,感兴趣的可以了解一下
    2022-09-09

最新评论