控制台程序。

javax.swing.BoxLayout类定义的布局管理器在单行或单列中布局组件。创建BoxLayout对象时,需要指定是在行还是列中布局组件。

对于行,组件是从左到右地添加;对于列,组件是从上到下地添加。行或列已满时,给定行或列中的组件不会放到下一行或列中。当添加的组件数超出行或列的可用空间时,布局管理器会减小组件的大小,甚至能根据需要剪切组件,使它们都放在单行或单列中。有了一行组件后,方框布局管理器就会尝试使所有的组件高度都相同,或者尝试使一列组件的宽度相同。

 import javax.swing.*;
import java.awt.*;
import javax.swing.border.*; public class TryBoxLayout4 { public static void createWindow(){
JFrame aWindow = new JFrame("This is the Window Title");
Toolkit theKit = aWindow.getToolkit(); // Get the window toolkit
Dimension wndSize = theKit.getScreenSize(); // Get screen size // Set the position to screen center & size to half screen size
aWindow.setSize(wndSize.width/2, wndSize.height/2); // Set window size
aWindow.setLocationRelativeTo(null); // Center window
aWindow.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // Create left column of radio buttons
Box left = Box.createVerticalBox();
left.add(Box.createVerticalStrut(30)); // Starting space
ButtonGroup radioGroup = new ButtonGroup(); // Create button group
JRadioButton rbutton; // Stores a button
radioGroup.add(rbutton = new JRadioButton("Red")); // Add to group
left.add(rbutton); // Add to Box
left.add(Box.createVerticalStrut(30)); // Space between
radioGroup.add(rbutton = new JRadioButton("Green"));
left.add(rbutton);
left.add(Box.createVerticalStrut(30)); // Space between
radioGroup.add(rbutton = new JRadioButton("Blue"));
left.add(rbutton);
left.add(Box.createVerticalStrut(30)); // Space between
radioGroup.add(rbutton = new JRadioButton("Yellow"));
left.add(rbutton);
left.add(Box.createGlue()); // Glue at the end // Create a panel with a titled border to hold the left Box container
JPanel leftPanel = new JPanel(new BorderLayout());
leftPanel.setBorder(new TitledBorder(
new EtchedBorder(), // Border to use
"Line Color")); // Border title
leftPanel.add(left, BorderLayout.CENTER); // Create right columns of checkboxes
Box right = Box.createVerticalBox();
right.add(Box.createVerticalStrut(30)); // Starting space
right.add(new JCheckBox("Dashed"));
right.add(Box.createVerticalStrut(30)); // Space between
right.add(new JCheckBox("Thick"));
right.add(Box.createVerticalStrut(30)); // Space between
right.add(new JCheckBox("Rounded"));
right.add(Box.createGlue()); // Glue at the end // Create a panel with a titled border to hold the right Box container
JPanel rightPanel = new JPanel(new BorderLayout());
rightPanel.setBorder(new TitledBorder(
new EtchedBorder(), // Border to use
"Line Properties")); // Border title
rightPanel.add(right, BorderLayout.CENTER); // Create top row to hold left and right
Box top = Box.createHorizontalBox();
top.add(leftPanel);
top.add(Box.createHorizontalStrut(5)); // Space between vertical boxes
top.add(rightPanel); // Create bottom row of buttons
JPanel bottomPanel = new JPanel();
bottomPanel.setBorder(new CompoundBorder(
BorderFactory.createLineBorder(Color.black, 1), // Outer border
BorderFactory.createBevelBorder(BevelBorder.RAISED))); // Inner border
Border edge = BorderFactory.createRaisedBevelBorder(); // Button border
JButton button;
Dimension size = new Dimension(80,20);
bottomPanel.add(button = new JButton("Defaults"));
button.setBorder(edge);
button.setPreferredSize(size);
bottomPanel.add(button = new JButton("OK"));
button.setBorder(edge);
button.setPreferredSize(size);
bottomPanel.add(button = new JButton("Cancel"));
button.setBorder(edge);
button.setPreferredSize(size); // Add top and bottom panel to content pane
Container content = aWindow.getContentPane(); // Get content pane
BoxLayout box = new BoxLayout(content, BoxLayout.Y_AXIS);
// Vertical for content pane
content.setLayout(box); // Set box layout manager
content.add(top);
content.add(bottomPanel); aWindow.pack(); // Size for components
aWindow.setVisible(true); // Display the window
} public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
createWindow();
}
});
}
}

Box类包含的静态方法可以创建不可见的组件,叫做“支柱(strut)"。垂直支柱有给定的高度(单位是像素),宽度为0。水平支柱有给定的宽度(单位是像素),高度为0。这些支柱的作用是允许在组件之间插入水平或垂直空格。
Box类中的createVerticalStrut()方法把垂直支柱返回为Component类型的对象。方法的参数指定了支柱的高度(单位为像素)。

使用"胶“(glue)可以控制Box对象中的剩余空间如何分配。胶是不可见的组件,唯一作用就是占据Box容器的剩余空间。尽管"胶"这个名称给人的印象是把组件捆绑在一起,但实际上,胶提供了两个组件之间的弹性连接器,这种连接器可以根据需要拉长或缩短,所以更像是弹簧。可以把胶组件放在Box中的组件之间,也可以放在Box对象的一端或两端。放置好组件后,剩余空间会在胶组件之间分配。

最新文章

  1. hibernate与Struts框架结合编写简单针对修改练习
  2. spring设置全局异常处理器
  3. Android抓包方法(一)之Fiddler代理
  4. Code笔记之:CSS块级元素、内联元素概念
  5. replace和replaceAll的区别
  6. toast 防止一直不停弹出,累积显示
  7. POJ 2153 Rank List (map映射)
  8. phpcms 源码分析七: 模板引擎实现
  9. 常见sql语句及复杂sql语句记录
  10. Debug编辑通过转Release找不到命名空间
  11. ASP.NET AJAX简明教程
  12. RFC Transactional RFC (tRFC) queue RFC(qRFC) 概念
  13. [刷题]算法竞赛入门经典(第2版) 4-4/UVa253 - Cube painting
  14. Android简易实战教程--第四十四话《ScrollView和HorizontalScrollView简单使用》
  15. 彪悍开源的分析数据库-ClickHouse
  16. C++的find函数使用小技巧
  17. nginx配置反向代理CAS单点登录应用
  18. Windows邮件客户端
  19. mysql内容总体回顾
  20. #ifndef#define#endif的用法(整理)

热门文章

  1. coreDate 简化版
  2. 性能测试工具JMeter
  3. P1371 NOI元丹
  4. 删除下标为n的数组值
  5. ubuntu 64bit arm-linux-gcc: No such file or directory 解决
  6. 使用NSTimer过程中最大的两个坑
  7. node.js express的安装过程
  8. Nodejs电影建站开发实例(下)
  9. [Stanford 2011] Ordinary Calculator(By myself)
  10. 解决父类加载iframe,src参数过大导致加载失败