Java Swing組件BoxLayout布局用法示例
本文實(shí)例講述了Java Swing組件BoxLayout布局用法。分享給大家供大家參考,具體如下:
BoxLayout 可以把控件依次進(jìn)行水平或者垂直排列布局,這是通過(guò)參數(shù) X_AXIS、Y_AXIS 來(lái)決定的。X_AXIS 表示水平排列,而 Y_AXIS 表示垂直排列。BoxLayout 的構(gòu)造函數(shù)有兩個(gè)參數(shù),一個(gè)參數(shù)定義使用該 BoxLayout 的容器,另一個(gè)參數(shù)是指定 BoxLayout 是采用水平還是垂直排列。下面是一個(gè)使用 BoxLayout排列控件的例子:
JPanel panel=new JPanel();
BoxLayout layout=new BoxLayout(panel, BoxLayout.X_AXIS);
panel.setLayout(layoout);
panel.add(new JButton("hello"));
panel.add(new JButton("wolrd"));
在實(shí)際應(yīng)用中,為了在控件直接添加間隔,我們常常需要分隔器,它有以下幾種類型:
① Rigid area 是一種用戶可以定義水平和垂直尺寸的透明組件;
② strut 與 rigid area 類似,但是用戶只能定義一個(gè)方向的尺寸,即水平方向或者垂直方向,不能同時(shí)定義水平和垂直尺寸;
③ glue位于兩個(gè)控件之間時(shí),它會(huì)盡可能的占據(jù)兩個(gè)控件之間的多余空間,從而將兩個(gè)控件擠到兩邊;
④ Filler 是 Box 的內(nèi)部類,它與 rigid area 相似,都可以指定水平或者垂直的尺寸,但是它可以設(shè)置最小,最大和優(yōu)先尺寸。
下面是一個(gè)測(cè)試用例:
BoxLayoutDemo.java
package awtDemo;
import java.awt.Dimension;
import javax.swing.BorderFactory;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.DefaultListModel;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.ListSelectionModel;
@SuppressWarnings("serial")
public class BoxLayoutDemo extends JPanel {
JPanel sportPanel;
JPanel queryPanel;
JPanel middlePanel;
public BoxLayoutDemo() {
// 主面板由3個(gè)子面板組成,在水平方向排列
setLayout(new BoxLayout(this, BoxLayout.X_AXIS));
this.setSportPanel();
this.setMiddlePanel();
this.setQueryPanel();
this.add(sportPanel);
this.add(middlePanel);
this.add(queryPanel);
}
private void setSportPanel() {
System.out.println("setSportPanel called");
//本函數(shù)內(nèi)包含以下兩個(gè)控件
JLabel sourceLabel;//文字標(biāo)簽
JScrollPane sourceListScroller;//滾動(dòng)條
//文字標(biāo)簽
sourceLabel = new JLabel("運(yùn)動(dòng)項(xiàng)目");
sourceLabel.setAlignmentY(TOP_ALIGNMENT);
sourceLabel.setBorder(BorderFactory.createEmptyBorder(4, 5, 0, 5));
// 創(chuàng)建一個(gè)列表,包含運(yùn)動(dòng)項(xiàng)目
DefaultListModel<String> listModel = new DefaultListModel<String>();
listModel.addElement("100米");
listModel.addElement("200米");
listModel.addElement("400米");
listModel.addElement("跳遠(yuǎn)");
listModel.addElement("跳高");
listModel.addElement("鉛球");
JList<String> sourceList = new JList<String>(listModel);
sourceList
.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
sourceList.setVisibleRowCount(5);//初始狀態(tài)保持5行可見(jiàn)
//滾動(dòng)條
sourceListScroller = new JScrollPane(sourceList);
sourceListScroller
.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
sourceListScroller.setAlignmentY(TOP_ALIGNMENT);
//開(kāi)始布局主面板
sportPanel = new JPanel();
sportPanel.setLayout(new BoxLayout(sportPanel, BoxLayout.Y_AXIS));// 垂直布局
sportPanel.setBorder(BorderFactory.createBevelBorder(1));
sportPanel.add(sourceLabel);// 加入文字標(biāo)簽到
sportPanel.add(sourceListScroller);// 加入運(yùn)動(dòng)項(xiàng)目列表
}
private void setMiddlePanel() {
//本函數(shù)包含2個(gè)按鈕
JButton toTargetButton = new JButton(">>");
JButton toSourceButton = new JButton("<<");
//布局主面板
middlePanel = new JPanel();
middlePanel.setBorder(BorderFactory.createBevelBorder(1));
middlePanel.setLayout(new BoxLayout(middlePanel, BoxLayout.Y_AXIS));//主面板為垂直布局
middlePanel.add(toTargetButton);// 添加第一個(gè)按鈕>>
middlePanel.add(Box.createRigidArea(new Dimension(15, 15)));// 中間添加一個(gè)看不見(jiàn)的rigidArea
middlePanel.add(toSourceButton);// 添加第二個(gè)按鈕<<
}
private void setQueryPanel() {
//本函數(shù)包含2個(gè)控件
JLabel targetLabel;
JScrollPane targetListScroller;
// 文字標(biāo)簽
targetLabel = new JLabel("查詢項(xiàng)目");
targetLabel.setAlignmentY(TOP_ALIGNMENT);
targetLabel.setBorder(BorderFactory.createEmptyBorder(4, 5, 0, 5));
// 創(chuàng)建列表查詢項(xiàng)目
DefaultListModel<String> targetListModel = new DefaultListModel<String>();
targetListModel.addElement("100米");
JList<String> targetList = new JList<String>(targetListModel);
targetList
.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
//滾動(dòng)條
targetListScroller = new JScrollPane(targetList);
targetListScroller
.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
targetListScroller.setAlignmentY(TOP_ALIGNMENT);
//設(shè)置主面板布局
queryPanel = new JPanel();
queryPanel.setLayout(new BoxLayout(queryPanel, BoxLayout.Y_AXIS));// 垂直布局
queryPanel.setBorder(BorderFactory.createBevelBorder(1));
queryPanel.add(targetLabel);//添加文字標(biāo)簽
queryPanel.add(targetListScroller);//添加滾動(dòng)條
}
public static void main(String[] args) {
JFrame frame = new JFrame("BoxlayoutDemo - www.dbjr.com.cn");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setContentPane(new BoxLayoutDemo());
frame.pack();
// frame.repaint();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
}
運(yùn)行效果圖:

更多關(guān)于java相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Java數(shù)據(jù)結(jié)構(gòu)與算法教程》、《Java字符與字符串操作技巧總結(jié)》、《Java操作DOM節(jié)點(diǎn)技巧總結(jié)》、《Java文件與目錄操作技巧匯總》和《Java緩存操作技巧匯總》
希望本文所述對(duì)大家java程序設(shè)計(jì)有所幫助。
相關(guān)文章
SpringBoot修改子模塊Module的jdk版本的方法 附修改原因
這篇文章主要介紹了SpringBoot修改子模塊Module的jdk版本的方法 附修改原因,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-04-04
Java基礎(chǔ)之位運(yùn)算知識(shí)總結(jié)
最近接觸到了java位運(yùn)算,之前對(duì)位運(yùn)算的了解僅僅停留在表現(xiàn)結(jié)果上,乘2除以2,對(duì)背后的原理并不了解,現(xiàn)在學(xué)習(xí)記錄一下,需要的朋友可以參考下2021-05-05
spring batch 讀取多個(gè)文件數(shù)據(jù)導(dǎo)入數(shù)據(jù)庫(kù)示例
本篇文章主要介紹了spring batch 讀取多個(gè)文件數(shù)據(jù)導(dǎo)入數(shù)據(jù)庫(kù)示例,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-03-03
SpringBoot + thymeleaf 實(shí)現(xiàn)讀取視頻列表并播放視頻功能
這篇文章主要介紹了SpringBoot + thymeleaf 實(shí)現(xiàn)讀取視頻列表并播放視頻功能,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-04-04
Java聊天室之實(shí)現(xiàn)運(yùn)行服務(wù)器與等待客戶端連接
這篇文章主要為大家詳細(xì)介紹了Java簡(jiǎn)易聊天室之實(shí)現(xiàn)運(yùn)行服務(wù)器程序與等待客戶端程序連接功能,文中的示例代碼講解詳細(xì),需要的可以了解一下2022-10-10
新手小白學(xué)JAVA 日期類Date SimpleDateFormat Calendar(入門(mén))
本文主要介紹了JAVA 日期類Date SimpleDateFormat Calendar,文中通過(guò)示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-10-10
Spring?Boot監(jiān)控SQL運(yùn)行情況的全過(guò)程
這篇文章主要給大家介紹了關(guān)于Spring?Boot監(jiān)控SQL運(yùn)行情況的相關(guān)資料,文中通過(guò)實(shí)例代碼介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用SpringBoot具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2022-02-02
spring學(xué)習(xí)之@SessionAttributes實(shí)例解析
這篇文章主要介紹了spring學(xué)習(xí)之@SessionAttributes實(shí)例解析,分享了相關(guān)代碼示例,小編覺(jué)得還是挺不錯(cuò)的,具有一定借鑒價(jià)值,需要的朋友可以參考下2018-02-02

