注:本文內容源於http://www.java3z.com/cwbwebhome/article/article20/200016.html?id=4797;細節內容根據筆者理解有修改。html
BoxLayout 能夠把控件依次進行水平或者垂直排列布局,這是經過參數 X_AXIS、Y_AXIS 來決定的。X_AXIS 表示水平排列,而 Y_AXIS 表示垂直排列。BoxLayout 的構造函數有兩個參數,一個參數定義使用該 BoxLayout 的容器,另外一個參數是指定 BoxLayout 是採用水平仍是垂直排列。下面是一個使用 BoxLayout排列控件的例子:java
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"));
在實際應用中,爲了在控件直接添加間隔,咱們經常須要分隔器,它有如下幾種類型:
下面是一個測試用例:web
BoxLayoutDemo.java
import java.awt.BorderLayout; 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; 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"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setContentPane(new BoxLayoutDemo()); frame.pack(); // frame.repaint(); frame.setLocationRelativeTo(null); frame.setVisible(true); } }
運行效果圖:函數