日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > java >内容正文

java

java layoutmanager_Java Swing 探索(一)LayoutManager

發布時間:2023/12/10 java 33 豆豆
生活随笔 收集整理的這篇文章主要介紹了 java layoutmanager_Java Swing 探索(一)LayoutManager 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

BorderLayout? FlowLayout? GridLayout? GridBagLayout? CardLayout? BoxLayout

1.BorderLayout

java.lang.Object

--java.awt.BorderLayout

將版面劃分成東、西、南、北、中五個區域,將添加的組件按指定位置放置。

BorderLayout.EAST? ? BorderLayout.WEST? ? BorderLayout.SOUTH? ? BorderLayout.NORTH? ? BorderLayout.CENTER

構造函數:

BorderLayout()

建立組件間無間距的BorderLayout

BorderLayout(int hgap,int vgap)

建立組件間水平間距為hgap,垂直間距為vgap的BorderLayout

例一:

[java]

view plain

copy

import java.awt.BorderLayout;? import javax.swing.JFrame;? import javax.swing.JButton;? ? /**? *? * @author Chel? */? public class BorderLayoutDemo {? ? ? ? public static void main(String[] args) {? ? ? ? ? //建立一個JFrame,JFrame的默認LayoutManager為BorderLayout? ? ? ? ? JFrame f=new JFrame("BorderLayout");? ? ? ? ? JButton btn=new JButton("BorderLayout.NORTH");? ? ? ? ? f.add(btn,BorderLayout.NORTH);? ? ? ? ? btn=new JButton("BorderLayout.SOUTH");? ? ? ? ? f.add(btn,BorderLayout.SOUTH);? ? ? ? ? btn=new JButton("BorderLayout.EAST");? ? ? ? ? f.add(btn,BorderLayout.EAST);? ? ? ? ? btn=new JButton("BorderLayout.West");? ? ? ? ? f.add(btn,BorderLayout.WEST);? ? ? ? ? btn=new JButton("BorderLayout.CENTER");? ? ? ? ? f.add(btn,BorderLayout.CENTER);? ? ? ? ? f.pack();? ? ? ? ? f.setVisible(true);? ? ? ? ? f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);? ? ? }? ? }

運行結果:

在上例代碼的第13,14行之間插入以下代碼

[java]

view plain

copy

f.setLayout(new BorderLayout(10,10));

運行結果:

2.FlowLayout

java.lang.Object

--java.awt.FlowLayout

組件按從左到右而后從上到下的順序依次排列,一行不能放完則折到下一行。

構造函數:

FlowLayout()

建立一個默認為居中對齊,組件彼此有5單位的水平與垂直間距的FlowLayout

FlowLayout(int align)

建立一個可設置排列方式且組件彼此有5單位的水平與垂直間距的FlowLayout

FlowLayout(int align,int hgap,int vgap)

建立一個可設置排列方式與組件間距的FlowLayout

例二:

[java]

view plain

copy

import java.awt.FlowLayout;? import javax.swing.JFrame;? import javax.swing.JButton;? ? /**? *? * @author Chel? */? public class FlowLayoutDemo {? ? ? ? public static void main(String[] args) {? ? ? ? ? JFrame f=new JFrame("FlowLayout");? ? ? ? ? f.setLayout(new FlowLayout());? ? ? ? ? for(int i=0;i<7;i++){? ? ? ? ? ? ? JButton btn=new JButton("Button"+i);? ? ? ? ? ? ? f.add(btn);? ? ? ? ? }? ? ? ? ? f.setSize(300,150);? ? ? ? ? f.setVisible(true);? ? ? ? ? f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);? ? ? }? ? }

運行結果:

3.GridLayout

java.lang.Object

--java.awt.GridLayout

矩形網格形式對容器的組件進行布置

構造函數:

GridLayout()

建立一個默認為一行一列的GridLayout

GridLayout(int rows,int cols)

建立一個指定行(rows)和列(cols)的GridLayout

GridLayout(int rows,int cols,int hgap,int vgap)

建立一個指定行(rows)和列(cols),且組件間水平間距為hgap、垂直間距為vgap的GridLayout

例三:

[java]

view plain

copy

import java.awt.GridLayout;? import javax.swing.JFrame;? import javax.swing.JButton;? ? /**? *? * @author Chel? */? public class GridLayoutDemo {? ? ? ? public static void main(String[] args) {? ? ? ? ? JFrame f=new JFrame("GridLayout");? ? ? ? ? //設置f的布局管理器為3行3列的GridLayout,組件間水平與垂直間距為5? ? ? ? ? f.setLayout(new GridLayout(3,3,5,5));? ? ? ? ? for(int i=1;i<10;i++){? ? ? ? ? ? ? JButton btn=new JButton(String.valueOf(i));? ? ? ? ? ? ? f.add(btn);? ? ? ? ? }? ? ? ? ? f.pack();? ? ? ? ? f.setVisible(true);? ? ? ? ? f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);? ? ? }? ? }

運行結果:

4.GridBagLayout

java.lang.Object

--java.awt.GridBagLayout

GridBagLayout以表格形式布置容器內的組件,將每個組件放置在每個單元格內,而一個單元格可以跨越多個單元格合并成一個單元格,即多個單元格可以組合成一個單元格,從而實現組件的自由布局。

構造函數:

GridBagLayout()

建立一個默認的GridBagLayout

每一個單元格都有各自的屬性,而這些屬性由GridBagConstrainsts類的成員變量來定義,且GridBagConstriaints中的所有成員變量都是public的。

java.lang.Object

--java.awt.GridBagConstratints

構造函數:

GridBagConstraints()

建立一個默認的GridBagConstraints

GridBagConstraints(intgridx,int gridy,int gridwidth,int gridheight,double weightx,double weighty,int anchor,int fill,Insets insets,int ipadx,int ipady)

建立一個指定其參數值的GridBagConstraints

GridBagConstraints的成員變量:

int gridx

int gridy

int gridwidth

int gridheight

double weightx

double weighty

int anchor

int fill

Insets insets

int ipadx

int ipady

gridx,gridy:設置組件所處行與列的起始坐標。例如gridx=0,gridy=0表示將組件放置在0行0列單元格內。

gridwidth,gridheight:設置組件橫向與縱向的單元格跨越個數。

可以通過GridBagConstraints的RELETIVE,和REMAINDER來進行指定,它的用法是:

當把gridx值設置為GridBagConstriants.RELETIVE時,所添加的組件將被放置在前一個組件的右側。同理,對gridy 值設置為GridBagConstraints.RELETIVE時,所添加的組件將被放置在前一個組件的下方,(這是一種根據前一個組件而決定當前組 件的相對放置方式)

對gridweight和gridheight也可以應用GridBagConstraints的REMAINDER方式,創建的組件會從創建的起點位置 開始一直延伸到容器所能允許的界限為止。該功能使得你可以創建跨越某些行或列的組件,從而改變相應方向上組件的數目,即使其后在布局的其他地方添加額外的組件也是如此。

weightx,weighty:設置窗口變大時的縮放比例。

anchor:設置組件在單元格中的對齊方式。由以下常量來定義

GridBagConstraints.CENTER

GridBagConstraints.EAST

GridBagConstraints.WEST

GridBagConstraints.SOUTH

GridBagConstraints.NORTH

GridBagConstraints.SOUTHEAST

GrisBagConstraints.SOUTHWEST

GridBagConstraints.NORTHEAST

GridBagConstraints.NORTHWEST

fill:當某個組件未能填滿單元格時,可由此屬性設置橫向、縱向或雙向填滿。由以下常量來定義

GridBagConstraints.NONE

GridBagConstraints.HORIZONTAL

GridBagConstraints.VERTICAL

GridBagConstraints.BOTH

insets:設置單元格的間距。

java.lang.Object

--java.awt.Insets

Insets(int top,int left,int bottom,int right)

ipadx,ipady:將單元格內的組件的最小尺寸橫向或縱向擴大。若一個組件的尺寸為30*10像素,ipadx=2,ipady=3,則單元格內的組件最小尺寸為34*16像素

例四:

[java]

view plain

copy

import java.awt.GridBagLayout;? import java.awt.GridBagConstraints;? import java.awt.Insets;? import javax.swing.JFrame;? import javax.swing.JButton;? ? /**? *? * @author Chel? */? public class GridBagLayoutDemo {? ? ? ? public static void main(String[] args) {? ? ? ? ? JFrame f=new JFrame("GridBagLayout");? ? ? ? ? f.setLayout(new GridBagLayout());? ? ? ? ? JButton btn=new JButton("first");? ? ? ? ? GridBagConstraints gbc=new GridBagConstraints();? ? ? ? ? //設定第一個單元格的屬性值? ? ? ? ? gbc.gridx=0;? ? ? ? ? gbc.gridy=0;? ? ? ? ? gbc.gridwidth=1;? ? ? ? ? gbc.gridheight=1;? ? ? ? ? gbc.weightx=0;? ? ? ? ? gbc.weighty=0;? ? ? ? ? gbc.anchor=GridBagConstraints.NORTHWEST;? ? ? ? ? gbc.fill=GridBagConstraints.NONE;? ? ? ? ? gbc.insets=new Insets(0,0,0,0);? ? ? ? ? gbc.ipadx=0;? ? ? ? ? gbc.ipady=0;? ? ? ? ? f.add(btn,gbc);? ? ? ? ? ? //設定第二個單元格屬性值? ? ? ? ? gbc.gridx=1;? ? ? ? ? gbc.gridy=0;? ? ? ? ? gbc.gridwidth=GridBagConstraints.REMAINDER;? ? ? ? ? gbc.gridheight=1;? ? ? ? ? gbc.weightx=1;? ? ? ? ? gbc.weighty=0;? ? ? ? ? gbc.anchor=GridBagConstraints.CENTER;? ? ? ? ? gbc.fill=GridBagConstraints.HORIZONTAL;? ? ? ? ? gbc.insets=new Insets(5,5,5,5);? ? ? ? ? gbc.ipadx=0;? ? ? ? ? gbc.ipady=0;? ? ? ? ? btn=new JButton("second");? ? ? ? ? f.add(btn,gbc);? ? ? ? ? ? //設定第三個單元格屬性值? ? ? ? ? gbc.gridx=0;? ? ? ? ? gbc.gridy=1;? ? ? ? ? gbc.gridwidth=1;? ? ? ? ? gbc.gridheight=GridBagConstraints.REMAINDER;? ? ? ? ? gbc.weightx=0;? ? ? ? ? gbc.weighty=1;? ? ? ? ? gbc.anchor=GridBagConstraints.CENTER;? ? ? ? ? gbc.fill=GridBagConstraints.VERTICAL;? ? ? ? ? gbc.insets=new Insets(0,0,0,0);? ? ? ? ? gbc.ipadx=10;? ? ? ? ? gbc.ipady=10;? ? ? ? ? btn=new JButton("three");? ? ? ? ? f.add(btn,gbc);? ? ? ? ? f.pack();? ? ? ? ? f.setVisible(true);? ? ? ? ? f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);? ? ? }? ? }

運行結果:

將窗口變大后的效果:

5.CardLayout

java.lang.Object

--java.awt.CardLayout

以層疊的方式布置組件,如同很多張卡片疊在一起,從而只能看到最上面的那一張卡片。

構造函數:

CardLayout()

建立一個無間距的CardLayout

CardLayout(int hgap,int vgap)

建立一個水平間距為hgap、垂直間距為vgap的CardLayout

例五:

[java]

view plain

copy

import java.awt.BorderLayout;? import java.awt.CardLayout;? import java.awt.event.ActionEvent;? import java.awt.event.ActionListener;? import javax.swing.JFrame;? import javax.swing.JPanel;? import javax.swing.JLabel;? import javax.swing.JButton;? ? /**? *? * @author Chel? */? public class CardLayoutDemo {? ? ? ? private static JPanel p;? ? ? public static void main(String[] args) {? ? ? ? ? JFrame f=new JFrame("CardLayout");? ? ? ? ? p=new JPanel();? ? ? ? ? //將JPanel p的LayoutManager設置為CardLayout? ? ? ? ? p.setLayout(new CardLayout());? ? ? ? ? ? //新建兩個JPanel? ? ? ? ? JPanel p1=new JPanel();? ? ? ? ? JPanel p2=new JPanel();? ? ? ? ? JLabel lb=new JLabel("first panel");? ? ? ? ? p1.add(lb);? ? ? ? ? lb=new JLabel("second panel");? ? ? ? ? p2.add(lb);? ? ? ? ? ? //將新建的兩個JPanel p1,p2添加到p中? ? ? ? ? p.add(p1,"frist");? ? ? ? ? p.add(p2,"second");? ? ? ? ? ? //設置默認顯示first所對應的JPanel p1? ? ? ? ? ((CardLayout)p.getLayout()).show(p, "frist");? ? ? ? ? ? JButton cbtn=new JButton("Change");? ? ? ? ? cbtn.addActionListener(new ActionListener(){? ? ? ? ? ? ? ? public void actionPerformed(ActionEvent e) {? ? ? ? ? ? ? ? ? //當按下Change按鈕時,顯示second 對應的JPanel p2? ? ? ? ? ? ? ? ? ((CardLayout)p.getLayout()).show(p, "second");? ? ? ? ? ? ? }? ? ? ? ? ? ? ? ? ? ? ? });? ? ? ? ? f.add(cbtn,BorderLayout.NORTH);? ? ? ? ? f.add(p,BorderLayout.CENTER);? ? ? ? ? f.setSize(400,150);? ? ? ? ? f.setVisible(true);? ? ? ? ? f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);? ? ? }? ? }

運行結果:

按下Change按鈕后的結果:

6.BoxLayout

java.lang.Object

--javax.swing.BoxLayout

以嵌套式盒子來管里容器的布局,通過將組件放入水平或垂直形盒子以多層嵌套的方式進行布局。

構造函數:

BoxLayout(Container target,int axis)

建立一個水平或垂直的BoxLayout,BoxLayout提供兩個常數X_AXIS和Y_AXIS來表示水平或垂直排列。

說到BoxLayout,就不得不提到Box這個Container,Box這個Container默認的Layout為BoxLayout,而它只能使用這個Layout,否則編譯時會有Error產生。

java.lang.Object

--javax.swing.Box

Box有水平的和垂直的兩種形式。

構造函數:

Box(int axis)

建立一個Box Container(容器),并指定組件的排列方式,通過使用BoxLayout提供的兩個常數X_AXIS和Y_AXIS來指定。

方法:

public static Box createHorizontalBox()

構造一個水平排列的Box組件。

[java]

view plain

copy

import javax.swing.Box;? import javax.swing.JFrame;? import javax.swing.JButton;? ? /**? *? * @author Chel? */? public class BoxLayoutDemo {? ? ? ? public static void main(String[] args) {? ? ? ? ? JFrame f=new JFrame("BoxLayout");? ? ? ? ? //創建水平Box組件? ? ? ? ? Box box=Box.createHorizontalBox();? ? ? ? ? JButton btnA=new JButton("A");? ? ? ? ? JButton btnB=new JButton("B");? ? ? ? ? box.add(btnA);? ? ? ? ? box.add(btnB);? ? ? ? ? f.add(box);? ? ? ? ? f.pack();? ? ? ? ? f.setVisible(true);? ? ? ? ? f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);? ? ? }? ? ? ? }

運行結果:

public static Component createHorizontalGlue()

構造一個Glue組件可向水平方向延伸。

在上例17,18行間插入以下代碼

[c-sharp]

view plain

copy

box.add(Box.createHorizontalGlue());

運行結果:

將窗口變大后的效果:

public static Component createHorizontalStrut(int width)

構造一個水平指定寬度的Strut組件。

將上例代碼改成以下代碼

[java]

view plain

copy

box.add(Box.createHorizontalStrut(50));

運行結果:

public static Component createRigidArea(Dimension d)

構造一個指定長寬的二維Rigid組件。

將上例代碼改成以下代碼

[java]

view plain

copy

box.add(Box.createRigidArea(new Dimension(50,50)));

運行結果:

public static Box createVerticalBox()

構造一個垂直排列的Box組件。

public static Component createVerticalGlue()

構造一個垂直的Glue組件。

public static Component createVerticalStrut(int height)

構造一個垂直的Strut組件。

public static Component createGlue()

構造一個Glue組件可向水平方向延伸。

Box.Fillter

Fillter是Box的inner class(內部類),它的功能與Rigid相似,都可以指定長寬的大小限制,且Fillter可以指定最大、較佳、最小的長寬大小。

http://www.5678520.com/kaiwangdian/130.html

http://www.5678520.com/kaiwangdian/129.html

http://www.5678520.com/kaiwangdian/128.html

http://www.5678520.com/kaiwangdian/127.html

http://www.5678520.com/kaiwangdian/126.html

http://www.lianzhiwei.com/News/389/20122116.html

http://www.lianzhiwei.com/News/389/20122115.html

http://www.lianzhiwei.com/News/389/20122114.html

http://www.lianzhiwei.com/News/389/20122113.html

http://www.lianzhiwei.com/News/389/20122112.html

總結

以上是生活随笔為你收集整理的java layoutmanager_Java Swing 探索(一)LayoutManager的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。