控制台程序。

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对象的一端或两端。放置好组件后,剩余空间会在胶组件之间分配。

Java基础之创建窗口——使用BoxLayout管理器(TryBoxLayout4)的更多相关文章

  1. Java基础之创建窗口——使用GridBagLayout管理器(TryGridBagLayout)

    控制台程序. java.awt.GridBagLayout管理器比前面介绍的其他布局管理器灵活得多,因此使用起来也比较复杂.基本机制就是在随意的矩形网格中布局组件,但网格的行和列不一定拥有相同的高度和 ...

  2. Java基础之创建窗口——使用SpringLayout管理器(TrySpringLayout)

    控制台程序. 可以把JFrame对象aWindow的内容面板的布局管理器设置为javax.swing.SpringLayout管理器. SpringLayout类定义的布局管理器根据javax.swi ...

  3. Java基础之创建窗口——使用流布局管理器(TryFlowLayout)

    控制台程序. FlowLayout把组件放在容器的连续行中,使每一行都放置尽可能多的组件.如果某行已满,就放在下一行.工作方式类似于文本处理器把单词放在行中.主要用途是放置按钮,但也可以用来放置其他组 ...

  4. Java基础之创建窗口——使用网格布局管理器(TryGridLayout)

    控制台程序. 网格布局管理器可以在容器的矩形网格中布局组件. import javax.swing.*; import java.awt.*; import javax.swing.border.Et ...

  5. Java基础之创建窗口——使用卡片布局管理器(TryCardLayout)

    控制台程序. 卡片布局管理器会生成一叠组件——一个组件放在另一个组件的上面.添加到容器中的第一个组件在堆栈的顶部,因此是可见的,添加的最后一个组件在堆栈的底部.使用默认的构造函数CardLayout( ...

  6. Java基础之创建窗口——使用边界布局管理器(TryBorderLayout)

    控制台程序. 边界布局管理器最多能在容器中放置5个组件.在这种布局管理器中,可以把组件放在容器的任意一个边界上,也可以把组件放在容器的中心.每个位置只能放置一个组件.如果把组件放置在已被占用的边界上, ...

  7. Java基础之创建窗口——向窗口中添加菜单(Sketcher)

    控制台程序. JMenuBar对象表示放在窗口顶部的菜单栏.可以为JMenuBar对象添加JMenu或JMenuItem对象,它们都显示在菜单栏上.JMenu对象是带有标签的菜单,单击就可以显示一列菜 ...

  8. Java基础之创建窗口——颜色和光标(TryWindow4)

    控制台程序. java.awt包中把SystemColor类定义为Color类的子类.SystemColor类封装了本机操作系统用于显示各种组件的标准颜色.如果要比较SystemColor值和Colo ...

  9. Java基础之创建窗口——使窗口在屏幕居中(TryWindow2/TryWindow3)

    控制台程序. 1.使用ToolKit对象在屏幕的中心显示窗口,将窗口的宽度和高度设置为屏幕的一半: import javax.swing.JFrame; import javax.swing.Swin ...

随机推荐

  1. ios_常用关键字

    一.关键字说明 1. @synthesize关键字:  根据@property设置,自动生成成员变量相应的存取方法,从而可以使用点操作符来方便的存取该成员变量 . 2. @implementation ...

  2. 页面静态化3 --- 伪静态技术之Apache的rewrite机制

      Apache的rewrite机制: 意思就是,你发送的地址,比如:http://localhost/news-id67.html会被Apache改写成http://localhost/news.p ...

  3. HAProxy安装及初步使用

    1.yum安装wget http://dl.fedoraproject.org/pub/epel/6/x86_64/epel-release-6-8.noarch.rpmrpm -ivh epel-r ...

  4. 百度Ueditor编辑器的Html模式自动替换样式的解决方法

    百度的Ueditor编辑器出于安全性考虑,用户在html模式下粘贴进去的html文档会自动被去除样式和转义.虽然安全的,但是非常不方便. 做一下修改把这个功能去掉. 一.打开ueditor.all.j ...

  5. MySQL字段之集合(set)枚举(enum)

    MySQL字段之集合(set)枚举(enum) (2008-12-23 13:51:23) 标签:it  分类:MySQL 集合 SET mysql> create table jihe(f1 ...

  6. 转:java.sql.SQLException: [Microsoft][ODBC 驱动程序管理器] 未发现数据源名称并且未指定默认驱动程序

    在Win7 64位系统下,使用Java+Access数据库编程,用Java连数据库时,出现错误提示,如下: Java java.sql.SQLException: [Microsoft][ODBC 驱 ...

  7. Ubuntu apparmor何方神圣

    AppArmor 是一款与SeLinux类似的安全框架/工具,其主要作用是控制应用程序的各种权限,例如对某个目录/文件的读/写,对网络端口的打开/读/写等等. 来之Novell网站的引用: AppAr ...

  8. maven--composer---setting.xml(updatepolicy)---mvn install , mvn deploy

    场景:最近再整系统的自动部署流程,由于公司的jar包在svn以及mvn的仓库上都存在,开发人员在开发的过程中都依赖mvn仓库中的Jar 包,在jar上线的时候,配置管理人员把jar 从svn管理的工作 ...

  9. 【Java 基础篇】【第八课】package包

    学习Java以来,第一次感觉稍微有点不好理解的,下面说说吧. 存在意义: 包的存在就是为更好的组织结构 包的创建: 只要在程序的最前面加入 Package ******,就可以了,一般都喜欢写 com ...

  10. socketserve及其应用

    1.cmd命令 利用socket socket服务端 import socket import subprocess s = socket.socket() s.bind(("127.0.0 ...