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

Java Swing組件BoxLayout布局用法示例

 更新時間:2017年11月16日 08:55:11   作者:pzy4447  
這篇文章主要介紹了Java Swing組件BoxLayout布局用法,結(jié)合實例形式分析了Swing使用BoxLayout容器進行布局的相關(guān)方法與操作技巧,需要的朋友可以參考下

本文實例講述了Java Swing組件BoxLayout布局用法。分享給大家供大家參考,具體如下:

BoxLayout 可以把控件依次進行水平或者垂直排列布局,這是通過參數(shù) X_AXIS、Y_AXIS 來決定的。X_AXIS 表示水平排列,而 Y_AXIS 表示垂直排列。BoxLayout 的構(gòu)造函數(shù)有兩個參數(shù),一個參數(shù)定義使用該 BoxLayout 的容器,另一個參數(shù)是指定 BoxLayout 是采用水平還是垂直排列。下面是一個使用 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"));

在實際應(yīng)用中,為了在控件直接添加間隔,我們常常需要分隔器,它有以下幾種類型:

① Rigid area 是一種用戶可以定義水平和垂直尺寸的透明組件;
② strut 與 rigid area 類似,但是用戶只能定義一個方向的尺寸,即水平方向或者垂直方向,不能同時定義水平和垂直尺寸;
③ glue位于兩個控件之間時,它會盡可能的占據(jù)兩個控件之間的多余空間,從而將兩個控件擠到兩邊;
④ Filler 是 Box 的內(nèi)部類,它與 rigid area 相似,都可以指定水平或者垂直的尺寸,但是它可以設(shè)置最小,最大和優(yōu)先尺寸。

下面是一個測試用例:

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個子面板組成,在水平方向排列
    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)包含以下兩個控件
    JLabel sourceLabel;//文字標簽
    JScrollPane sourceListScroller;//滾動條
    //文字標簽
    sourceLabel = new JLabel("運動項目");
    sourceLabel.setAlignmentY(TOP_ALIGNMENT);
    sourceLabel.setBorder(BorderFactory.createEmptyBorder(4, 5, 0, 5));
    // 創(chuàng)建一個列表,包含運動項目
    DefaultListModel<String> listModel = new DefaultListModel<String>();
    listModel.addElement("100米");
    listModel.addElement("200米");
    listModel.addElement("400米");
    listModel.addElement("跳遠");
    listModel.addElement("跳高");
    listModel.addElement("鉛球");
    JList<String> sourceList = new JList<String>(listModel);
    sourceList
        .setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
    sourceList.setVisibleRowCount(5);//初始狀態(tài)保持5行可見
    //滾動條
    sourceListScroller = new JScrollPane(sourceList);
    sourceListScroller
        .setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
    sourceListScroller.setAlignmentY(TOP_ALIGNMENT);
    //開始布局主面板
    sportPanel = new JPanel();
    sportPanel.setLayout(new BoxLayout(sportPanel, BoxLayout.Y_AXIS));// 垂直布局
    sportPanel.setBorder(BorderFactory.createBevelBorder(1));
    sportPanel.add(sourceLabel);// 加入文字標簽到
    sportPanel.add(sourceListScroller);// 加入運動項目列表
  }
  private void setMiddlePanel() {
    //本函數(shù)包含2個按鈕
    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);// 添加第一個按鈕>>
    middlePanel.add(Box.createRigidArea(new Dimension(15, 15)));// 中間添加一個看不見的rigidArea
    middlePanel.add(toSourceButton);// 添加第二個按鈕<<
  }
  private void setQueryPanel() {
    //本函數(shù)包含2個控件
    JLabel targetLabel;
    JScrollPane targetListScroller;
    // 文字標簽
    targetLabel = new JLabel("查詢項目");
    targetLabel.setAlignmentY(TOP_ALIGNMENT);
    targetLabel.setBorder(BorderFactory.createEmptyBorder(4, 5, 0, 5));
    // 創(chuàng)建列表查詢項目
    DefaultListModel<String> targetListModel = new DefaultListModel<String>();
    targetListModel.addElement("100米");
    JList<String> targetList = new JList<String>(targetListModel);
    targetList
        .setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
    //滾動條
    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);//添加文字標簽
    queryPanel.add(targetListScroller);//添加滾動條
  }
  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);
  }
}

運行效果圖:

更多關(guān)于java相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Java數(shù)據(jù)結(jié)構(gòu)與算法教程》、《Java字符與字符串操作技巧總結(jié)》、《Java操作DOM節(jié)點技巧總結(jié)》、《Java文件與目錄操作技巧匯總》和《Java緩存操作技巧匯總

希望本文所述對大家java程序設(shè)計有所幫助。

相關(guān)文章

最新評論