最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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

 更新時(shí)間:2018年03月01日 08:15:20   作者:彬菌  
本文通過代碼示例給大家詳細(xì)講解了java中Swing五種常見的布局方式,以及相關(guān)注意知識(shí)點(diǎ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();
  }
}

運(yùn)行結(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();
  }
}

運(yùn)行結(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();
	}
}

運(yùn)行結(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布局時(shí),添加固定寬度組件隔開
		//this.getContentPane().add(Box.createVerticalStrut(5)); //采用y布局時(shí),添加固定高度組件隔開
		this.add(btn3);
		this.add(btn4);
		this.add(btn5);
	}
	public static void main(String args[]){
		new BoxLaYoutExample();
	}
}

運(yùn)行結(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();
	}
}

運(yùn)行結(jié)果:

相關(guān)文章

  • Java8處理List的雙層循環(huán)問題

    Java8處理List的雙層循環(huán)問題

    這篇文章主要介紹了Java8處理List的雙層循環(huán)問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧
    2020-08-08
  • java中把漢字轉(zhuǎn)換成簡拼的實(shí)現(xiàn)代碼

    java中把漢字轉(zhuǎn)換成簡拼的實(shí)現(xiàn)代碼

    本篇文章是對(duì)在java中把漢字轉(zhuǎn)換成簡拼的實(shí)現(xiàn)方法進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下
    2013-05-05
  • Spring?Boot?整合?Fisco?Bcos部署、調(diào)用區(qū)塊鏈合約的案例

    Spring?Boot?整合?Fisco?Bcos部署、調(diào)用區(qū)塊鏈合約的案例

    本篇文章介紹?Spring?Boot?整合?Fisco?Bcos?的相關(guān)技術(shù),最最重要的技術(shù)點(diǎn),部署、調(diào)用區(qū)塊鏈合約的工程案例,本文通過流程分析給大家介紹的非常詳細(xì),需要的朋友參考下吧
    2022-01-01
  • spring mvc中的@ModelAttribute注解示例介紹

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

    在Spring mvc中,注解@ModelAttribute是一個(gè)非常常用的注解,下面這篇文章主要給大家介紹了關(guān)于spring mvc中@ModelAttribute注解的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),需要的朋友可以參考下。
    2017-09-09
  • Java圖像處理之RGB調(diào)色面板

    Java圖像處理之RGB調(diào)色面板

    這篇文章主要為大家詳細(xì)介紹了Java圖像處理之RGB調(diào)色面板,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2022-06-06
  • Java Enum和String及int的相互轉(zhuǎn)化示例

    Java Enum和String及int的相互轉(zhuǎn)化示例

    這篇文章主要介紹了Java Enum和String及int的相互轉(zhuǎn)化示例,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-06-06
  • java自定義線程池的原理簡介

    java自定義線程池的原理簡介

    這篇文章主要介紹了java自定義線程池的原理詳解,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2019-08-08
  • Clojure?與Java對(duì)比少數(shù)據(jù)結(jié)構(gòu)多函數(shù)勝過多個(gè)單獨(dú)類的優(yōu)點(diǎn)

    Clojure?與Java對(duì)比少數(shù)據(jù)結(jié)構(gòu)多函數(shù)勝過多個(gè)單獨(dú)類的優(yōu)點(diǎn)

    這篇文章主要介紹了Clojure?與Java對(duì)比少數(shù)據(jù)結(jié)構(gòu)多函數(shù)勝過多個(gè)單獨(dú)類的優(yōu)點(diǎn),在Clojure中,我們一次又一次地使用相同的數(shù)據(jù)結(jié)構(gòu),并在其上運(yùn)行許多函,更多相關(guān)介紹需要的朋友可以參考一下下面文章內(nèi)容
    2022-06-06
  • Mybatis-Plus實(shí)現(xiàn)SQL攔截器的示例

    Mybatis-Plus實(shí)現(xiàn)SQL攔截器的示例

    這篇文章主要介紹了Mybatis-Plus實(shí)現(xiàn)一個(gè)SQL攔截器,通過使用SQL攔截器,開發(fā)人員可以在執(zhí)行SQL語句之前或之后對(duì)其進(jìn)行修改或記錄,從而更好地控制和優(yōu)化數(shù)據(jù)庫操作,對(duì)Mybatis-Plus?SQL攔截器相關(guān)知識(shí)感興趣的朋友一起看看吧
    2023-05-05
  • 一文帶你深入了解Java?TreeMap

    一文帶你深入了解Java?TreeMap

    TreeMap是Map家族中的一員,也是用來存放key-value鍵值對(duì)的。平時(shí)在工作中使用的可能并不多。本文將基于jdk8對(duì)其做一個(gè)講解,感興趣的可以了解一下
    2022-09-09

最新評(píng)論

西乌珠穆沁旗| 东乡| 江都市| 大石桥市| 牟定县| 金堂县| 辽阳县| 平陆县| 福清市| 嘉荫县| 玛纳斯县| 应城市| 阳西县| 水城县| 赤壁市| 义马市| 江津市| 长汀县| 台北县| 沁阳市| 尚义县| 平果县| 精河县| 萝北县| 大新县| 肥乡县| 汉阴县| 龙海市| 柳林县| 塔河县| 姚安县| 卢龙县| 四会市| 昭觉县| 贵定县| 洮南市| 东山县| 郓城县| 宜昌市| 汝城县| 贺州市|