控制台程序。
javax.swing.BoxLayout类定义的布局管理器在单行或单列中布局组件。创建BoxLayout对象时,需要指定是在行还是列中布局组件。
对于行,组件是从左到右地添加;对于列,组件是从上到下地添加。行或列已满时,给定行或列中的组件不会放到下一行或列中。当添加的组件数超出行或列的可用空间时,布局管理器会减小组件的大小,甚至能根据需要剪切组件,使它们都放在单行或单列中。有了一行组件后,方框布局管理器就会尝试使所有的组件高度都相同,或者尝试使一列组件的宽度相同。
1 import javax.swing.*; 2 import java.awt.*; 3 import javax.swing.border.*; 4 5 public class TryBoxLayout4 { 6 7 public static void createWindow(){ 8 JFrame aWindow = new JFrame("This is the Window Title"); 9 Toolkit theKit = aWindow.getToolkit(); // Get the window toolkit 10 Dimension wndSize = theKit.getScreenSize(); // Get screen size 11 12 // Set the position to screen center & size to half screen size 13 aWindow.setSize(wndSize.width/2, wndSize.height/2); // Set window size 14 aWindow.setLocationRelativeTo(null); // Center window 15 aWindow.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 16 17 // Create left column of radio buttons 18 Box left = Box.createVerticalBox(); 19 left.add(Box.createVerticalStrut(30)); // Starting space 20 ButtonGroup radioGroup = new ButtonGroup(); // Create button group 21 JRadioButton rbutton; // Stores a button 22 radioGroup.add(rbutton = new JRadioButton("Red")); // Add to group 23 left.add(rbutton); // Add to Box 24 left.add(Box.createVerticalStrut(30)); // Space between 25 radioGroup.add(rbutton = new JRadioButton("Green")); 26 left.add(rbutton); 27 left.add(Box.createVerticalStrut(30)); // Space between 28 radioGroup.add(rbutton = new JRadioButton("Blue")); 29 left.add(rbutton); 30 left.add(Box.createVerticalStrut(30)); // Space between 31 radioGroup.add(rbutton = new JRadioButton("Yellow")); 32 left.add(rbutton); 33 left.add(Box.createGlue()); // Glue at the end 34 35 // Create a panel with a titled border to hold the left Box container 36 JPanel leftPanel = new JPanel(new BorderLayout()); 37 leftPanel.setBorder(new TitledBorder( 38 new EtchedBorder(), // Border to use 39 "Line Color")); // Border title 40 leftPanel.add(left, BorderLayout.CENTER); 41 42 // Create right columns of checkboxes 43 Box right = Box.createVerticalBox(); 44 right.add(Box.createVerticalStrut(30)); // Starting space 45 right.add(new JCheckBox("Dashed")); 46 right.add(Box.createVerticalStrut(30)); // Space between 47 right.add(new JCheckBox("Thick")); 48 right.add(Box.createVerticalStrut(30)); // Space between 49 right.add(new JCheckBox("Rounded")); 50 right.add(Box.createGlue()); // Glue at the end 51 52 // Create a panel with a titled border to hold the right Box container 53 JPanel rightPanel = new JPanel(new BorderLayout()); 54 rightPanel.setBorder(new TitledBorder( 55 new EtchedBorder(), // Border to use 56 "Line Properties")); // Border title 57 rightPanel.add(right, BorderLayout.CENTER); 58 59 // Create top row to hold left and right 60 Box top = Box.createHorizontalBox(); 61 top.add(leftPanel); 62 top.add(Box.createHorizontalStrut(5)); // Space between vertical boxes 63 top.add(rightPanel); 64 65 // Create bottom row of buttons 66 JPanel bottomPanel = new JPanel(); 67 bottomPanel.setBorder(new CompoundBorder( 68 BorderFactory.createLineBorder(Color.black, 1), // Outer border 69 BorderFactory.createBevelBorder(BevelBorder.RAISED))); // Inner border 70 Border edge = BorderFactory.createRaisedBevelBorder(); // Button border 71 JButton button; 72 Dimension size = new Dimension(80,20); 73 bottomPanel.add(button = new JButton("Defaults")); 74 button.setBorder(edge); 75 button.setPreferredSize(size); 76 bottomPanel.add(button = new JButton("OK")); 77 button.setBorder(edge); 78 button.setPreferredSize(size); 79 bottomPanel.add(button = new JButton("Cancel")); 80 button.setBorder(edge); 81 button.setPreferredSize(size); 82 83 // Add top and bottom panel to content pane 84 Container content = aWindow.getContentPane(); // Get content pane 85 BoxLayout box = new BoxLayout(content, BoxLayout.Y_AXIS); 86 // Vertical for content pane 87 content.setLayout(box); // Set box layout manager 88 content.add(top); 89 content.add(bottomPanel); 90 91 aWindow.pack(); // Size for components 92 aWindow.setVisible(true); // Display the window 93 } 94 95 public static void main(String[] args) { 96 SwingUtilities.invokeLater(new Runnable() { 97 public void run() { 98 createWindow(); 99 } 100 }); 101 } 102 }
Box类包含的静态方法可以创建不可见的组件,叫做“支柱(strut)"。垂直支柱有给定的高度(单位是像素),宽度为0。水平支柱有给定的宽度(单位是像素),高度为0。这些支柱的作用是允许在组件之间插入水平或垂直空格。
Box类中的createVerticalStrut()方法把垂直支柱返回为Component类型的对象。方法的参数指定了支柱的高度(单位为像素)。
使用"胶“(glue)可以控制Box对象中的剩余空间如何分配。胶是不可见的组件,唯一作用就是占据Box容器的剩余空间。尽管"胶"这个名称给人的印象是把组件捆绑在一起,但实际上,胶提供了两个组件之间的弹性连接器,这种连接器可以根据需要拉长或缩短,所以更像是弹簧。可以把胶组件放在Box中的组件之间,也可以放在Box对象的一端或两端。放置好组件后,剩余空间会在胶组件之间分配。