本文實例講述了Java Swing組件BoxLayout布局用法。分享給大家供大家參考,具體如下:
BoxLayout 可以把控件依次進行水平或者垂直排列布局,這是通過參數 X_AXIS、Y_AXIS 來決定的。X_AXIS 表示水平排列,而 Y_AXIS 表示垂直排列。BoxLayout 的構造函數有兩個參數,一個參數定義使用該 BoxLayout 的容器,另一個參數是指定 BoxLayout 是采用水平還是垂直排列。下面是一個使用 BoxLayout排列控件的例子:
1
2
3
4
5
|
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" )); |
在實際應用中,為了在控件直接添加間隔,我們常常需要分隔器,它有以下幾種類型:
① Rigid area 是一種用戶可以定義水平和垂直尺寸的透明組件;
② strut 與 rigid area 類似,但是用戶只能定義一個方向的尺寸,即水平方向或者垂直方向,不能同時定義水平和垂直尺寸;
③ glue位于兩個控件之間時,它會盡可能的占據兩個控件之間的多余空間,從而將兩個控件擠到兩邊;
④ Filler 是 Box 的內部類,它與 rigid area 相似,都可以指定水平或者垂直的尺寸,但是它可以設置最小,最大和優先尺寸。
下面是一個測試用例:
BoxLayoutDemo.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
|
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" ); //本函數內包含以下兩個控件 JLabel sourceLabel; //文字標簽 JScrollPane sourceListScroller; //滾動條 //文字標簽 sourceLabel = new JLabel( "運動項目" ); sourceLabel.setAlignmentY(TOP_ALIGNMENT); sourceLabel.setBorder(BorderFactory.createEmptyBorder( 4 , 5 , 0 , 5 )); // 創建一個列表,包含運動項目 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 ); //初始狀態保持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() { //本函數包含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() { //本函數包含2個控件 JLabel targetLabel; JScrollPane targetListScroller; // 文字標簽 targetLabel = new JLabel( "查詢項目" ); targetLabel.setAlignmentY(TOP_ALIGNMENT); targetLabel.setBorder(BorderFactory.createEmptyBorder( 4 , 5 , 0 , 5 )); // 創建列表查詢項目 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); //設置主面板布局 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.jfrwli.cn" ); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setContentPane( new BoxLayoutDemo()); frame.pack(); // frame.repaint(); frame.setLocationRelativeTo( null ); frame.setVisible( true ); } } |
運行效果圖:
希望本文所述對大家java程序設計有所幫助。
原文鏈接:http://www.cnblogs.com/pzy4447/p/4631585.html