欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

java中Swing五種常見的布局方式

 更新時間:2018年03月01日 08:15:20   作者:彬菌  
本文通過代碼示例給大家詳細(xì)講解了java中Swing五種常見的布局方式,以及相關(guān)注意知識點,有興趣的朋友參考學(xué)習(xí)下。

1、 邊界布局(BorderLayout)

2、流式布局(FlowLayout)

3、網(wǎng)格布局(GridLayout)

4、盒子布局(BoxLaYout)

5、空布局(null)

還有其他兩種布局,分別是GridBagLayout(網(wǎng)格包布局)、CardLayout(卡片布局)

注意:JFrame和JDialog默認(rèn)布局為BorderLayout,JPanel和Applet默認(rèn)布局為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)); //默認(rèn)為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();
  }
}

運行結(jié)果:

流式布局示例代碼:

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)); //默認(rèn)為居中;水平間距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();
  }
}

運行結(jié)果:

網(wǎng)格布局示例代碼:

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)); //默認(rèn)為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();
	}
}

運行結(jié)果:

盒子布局示例代碼:

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

運行結(jié)果:

空布局示例代碼:

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坐標(biāo)10,y坐標(biāo)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();
	}
}

運行結(jié)果:

相關(guān)文章

  • SpringCloud全面解析@FeignClient標(biāo)識接口的過程

    SpringCloud全面解析@FeignClient標(biāo)識接口的過程

    這篇文章主要介紹了SpringCloud全面解析@FeignClient標(biāo)識接口的過程,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-03-03
  • springboot如何去獲取前端傳遞的參數(shù)的實現(xiàn)

    springboot如何去獲取前端傳遞的參數(shù)的實現(xiàn)

    這篇文章主要介紹了springboot如何去獲取前端傳遞的參數(shù)的實現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-05-05
  • Java基礎(chǔ)詳解之集合框架工具Collections

    Java基礎(chǔ)詳解之集合框架工具Collections

    這篇文章主要介紹了Java基礎(chǔ)詳解之集合框架工具Collections,文中有非常詳細(xì)的代碼示例,對正在學(xué)習(xí)java的小伙伴們有很好地幫助,需要的朋友可以參考下
    2021-04-04
  • java 獲取對象中為null的字段實例代碼

    java 獲取對象中為null的字段實例代碼

    這篇文章主要介紹了java 獲取對象中為null的字段實例代碼,非常不錯,具有一定的參考借鑒價值,需要的朋友可以參考下
    2019-04-04
  • Docker使用 Maven 插件構(gòu)建鏡像的方法

    Docker使用 Maven 插件構(gòu)建鏡像的方法

    本篇文章主要介紹了Docker使用 Maven 插件構(gòu)建鏡像的方法,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2018-02-02
  • IDEA 中 maven 的 Lifecycle 和Plugins 的區(qū)別

    IDEA 中 maven 的 Lifecycle 和Plugins&n

    IDEA 主界面右側(cè) Maven 標(biāo)簽欄有同樣的命令,比如 install,既在 Plugins 中存在,也在 Lifecycle中存在,到底選哪個?二者又有什么區(qū)別呢?下面小編給大家介紹下IDEA 中 maven 的 Lifecycle 和Plugins 的區(qū)別,感興趣的朋友一起看看吧
    2023-03-03
  • Java使用Redis實現(xiàn)秒殺功能

    Java使用Redis實現(xiàn)秒殺功能

    這篇文章主要為大家詳細(xì)介紹了Java使用Redis實現(xiàn)秒殺功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2020-09-09
  • Java設(shè)計模式之淺談外觀模式

    Java設(shè)計模式之淺談外觀模式

    這篇文章主要介紹了Java設(shè)計模式之外觀模式的相關(guān)資料,需要的朋友可以參考下
    2022-09-09
  • 一文搞懂Mybatis-plus的分頁查詢操作

    一文搞懂Mybatis-plus的分頁查詢操作

    說起分頁機(jī)制,相信我們程序員都不陌生,今天,我就給大家分享一下Mybatis-plus的分頁機(jī)制,供大家學(xué)習(xí)和Copy,感興趣的可以了解一下
    2022-06-06
  • spring cloud學(xué)習(xí)入門之config配置教程

    spring cloud學(xué)習(xí)入門之config配置教程

    這篇文章主要給大家介紹了關(guān)于spring cloud學(xué)習(xí)入門之config配置的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家學(xué)習(xí)或者使用spring cloud具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧。
    2017-09-09

最新評論