控制台程序。

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. 【MySql】赶集网mysql开发36条军规

    [MySql]赶集网mysql开发36条军规 2012-05-14 14:02:33 分类: Linux   写在前面的话: 总是在灾难发生后,才想起容灾的重要性: 总是在吃过亏后,才记得曾经有人提醒 ...

  2. docker安装与启动

    安装docker [root@localhost /]# yum -y install docker-io     更改配置文件 [root@localhost /]# vi /etc/sysconf ...

  3. buffer overflow

    Computer Systems A Programmer's Perspective Second Edition We have seen that C does not perform any ...

  4. Advanced Packaging Tool

    https://en.wikipedia.org/wiki/Advanced_Packaging_Tool Eventually, a new team picked up the project, ...

  5. Linux的权限说明

    Linux的权限不是很细致,只有RWX三种r(Read,读取):对文件而言,具有读取文件内容的权限:对目录来说,具有浏览目录的权限.w(Write,写入):对文件而言,具有新增,修改,删除文件内容的权 ...

  6. 打造 PHP版本 1password

    以前注册很多网站密码都使用简单密码,但是由于今年频繁曝出密码不安全问题,所以要使用更加复杂的密码.但是好多个账号,密码也不能设置成一样的,防止一个被盗全部不安全了,记密码就成了意见很头疼的事情. 在手 ...

  7. C++ 虚函数畅谈

    0x01:前言 虚函数是C++里最重要的概念之一,并且是判定C++是否入门的一个热门问题.今天这篇文章简单谈谈虚函数. 0x02:虚函数简介 虚函数可以被子类实现函数所覆盖. virtual是关键字, ...

  8. Qt from Linux to Windows target

    45down voteaccepted Just use M cross environment (MXE). It takes the pain out of the whole process: ...

  9. uzqp文件的加解密

    帮朋友做的,根据python版本翻译成的java版本,记录一下代码 import java.io.File; import java.io.FileInputStream; import java.i ...

  10. 位与(&)常用编程技巧

    补充知识:1)正整数的补码与原码相同:                2)求负整数的补码:原码 符号位不变,数值位各位取反,最后整个数加1得到补码:                3)按位与& ...