Java基础之创建窗口——使用SpringLayout管理器(TrySpringLayout)
控制台程序。
可以把JFrame对象aWindow的内容面板的布局管理器设置为javax.swing.SpringLayout管理器。
SpringLayout类定义的布局管理器根据javax.swing.Spring对象定义的一组约束,决定容器中每个组件的位置和大小。在使用SpringLayout管理器的容器中,每个组件都有与之关联的SpringLayout.Constrains对象,Constrains对象定义了组件的4条边的位置。在访问组件对象关联的SpringLayout.Constrains对象之前,必须先把组件添加到容器中。
import javax.swing.*;
import java.awt.*; public class TrySpringLayout { 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); SpringLayout layout = new SpringLayout(); // Create a layout manager
Container content = aWindow.getContentPane(); // Get the content pane
content.setLayout(layout); // Set the container layout mgr JButton[] buttons = new JButton[6]; // Array to store buttons
SpringLayout.Constraints constr = null;
for(int i = 0; i < buttons.length; ++i) {
buttons[i] = new JButton("Press " + (i+1));
content.add(buttons[i]); // Add a Button to content pane
} Spring xSpring = Spring.constant(5,15,25); // x constraint for first button
Spring ySpring = Spring.constant(10,30, 50); // y constraint for first button // Connect x,y for first button to left and top of container by springs
constr = layout.getConstraints(buttons[0]);
constr.setX(xSpring);
constr.setY(ySpring); // Hook buttons together with springs
for(int i = 1 ; i< buttons.length ; ++i) {
constr = layout.getConstraints(buttons[i]);
layout.putConstraint(SpringLayout.WEST, buttons[i],
xSpring,SpringLayout.EAST, buttons[i-1]);
layout.putConstraint(SpringLayout.NORTH, buttons[i],
ySpring,SpringLayout.SOUTH, buttons[i-1]);
}
// Uncomment the following code to constrain the content pane
/*
SpringLayout.Constraints constraint = layout.getConstraints(content);
constraint.setConstraint(SpringLayout.EAST,
Spring.sum(constr.getConstraint(SpringLayout.EAST),
Spring.constant(15)));
constraint.setConstraint(SpringLayout.SOUTH,
Spring.sum(constr.getConstraint(SpringLayout.SOUTH),
Spring.constant(10)));
aWindow.pack();
*/
aWindow.setVisible(true); // Display the window
} public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
createWindow();
}
});
}
}
SpringLayout管理器非常灵活,如果对组件设置了合适的约束,就可以完成许多其他布局管理器的工作。
Java基础之创建窗口——使用SpringLayout管理器(TrySpringLayout)的更多相关文章
- Java基础之创建窗口——使用GridBagLayout管理器(TryGridBagLayout)
控制台程序. java.awt.GridBagLayout管理器比前面介绍的其他布局管理器灵活得多,因此使用起来也比较复杂.基本机制就是在随意的矩形网格中布局组件,但网格的行和列不一定拥有相同的高度和 ...
- Java基础之创建窗口——使用BoxLayout管理器(TryBoxLayout4)
控制台程序. javax.swing.BoxLayout类定义的布局管理器在单行或单列中布局组件.创建BoxLayout对象时,需要指定是在行还是列中布局组件. 对于行,组件是从左到右地添加:对于列, ...
- Java基础之创建窗口——使用流布局管理器(TryFlowLayout)
控制台程序. FlowLayout把组件放在容器的连续行中,使每一行都放置尽可能多的组件.如果某行已满,就放在下一行.工作方式类似于文本处理器把单词放在行中.主要用途是放置按钮,但也可以用来放置其他组 ...
- Java基础之创建窗口——使用网格布局管理器(TryGridLayout)
控制台程序. 网格布局管理器可以在容器的矩形网格中布局组件. import javax.swing.*; import java.awt.*; import javax.swing.border.Et ...
- Java基础之创建窗口——使用卡片布局管理器(TryCardLayout)
控制台程序. 卡片布局管理器会生成一叠组件——一个组件放在另一个组件的上面.添加到容器中的第一个组件在堆栈的顶部,因此是可见的,添加的最后一个组件在堆栈的底部.使用默认的构造函数CardLayout( ...
- Java基础之创建窗口——使用边界布局管理器(TryBorderLayout)
控制台程序. 边界布局管理器最多能在容器中放置5个组件.在这种布局管理器中,可以把组件放在容器的任意一个边界上,也可以把组件放在容器的中心.每个位置只能放置一个组件.如果把组件放置在已被占用的边界上, ...
- Java基础之创建窗口——向窗口中添加菜单(Sketcher)
控制台程序. JMenuBar对象表示放在窗口顶部的菜单栏.可以为JMenuBar对象添加JMenu或JMenuItem对象,它们都显示在菜单栏上.JMenu对象是带有标签的菜单,单击就可以显示一列菜 ...
- Java基础之创建窗口——颜色和光标(TryWindow4)
控制台程序. java.awt包中把SystemColor类定义为Color类的子类.SystemColor类封装了本机操作系统用于显示各种组件的标准颜色.如果要比较SystemColor值和Colo ...
- Java基础之创建窗口——使窗口在屏幕居中(TryWindow2/TryWindow3)
控制台程序. 1.使用ToolKit对象在屏幕的中心显示窗口,将窗口的宽度和高度设置为屏幕的一半: import javax.swing.JFrame; import javax.swing.Swin ...
随机推荐
- 省略nslog打印
//#if #endif宏定义的意思就是如果定义了DEBUG,那么就使用NSLog输出:否则这段代码直接忽略.有人会疑问这个DEBUG和_DEBUG来自哪里,这个其实不用担心,这个来自于Xcode的默 ...
- Gmail 启用 POP 标准配置说明:
接收邮件 (POP3) 服务器 - 要求 SSL:pop.gmail.com使用 SSL:是端口:995 发送邮件 (SMTP) 服务器 - 要求 TLS 或 SSL:smtp.gmail.com使用 ...
- debug和release转载
Debug和Release区别 转自草原和大树 VC下Debug和Release区别 最近写代码过程中,发现 Debug 下运行正常,Release 下就会出现问题,百思不得其解,而Release 下 ...
- 256 terabytes random-access memory
Computer Systems A Programmer's Perspective Second Edition As we will discuss, the extension of IA32 ...
- javascript小实例,PC网页里的拖拽
几年前,我参与设计开发一个房产网的项目,我负责前端工作,由于项目经理要求比较高,参考了很多房产类网站比较优秀的功能,想把别人比较优秀的设计和想法集合到一起,那时的设计稿和功能实现,简直就是改了又改,今 ...
- FW Docker为容器分配指定物理网段的静态IP
官方有关于网桥和IP配置的文档地址:https://docs.docker.com/articles/networking/ 1.宿主机(系统采用ubuntu-14.04.1-server-amd64 ...
- 安装SQL Server出现在创建窗口句柄之前,不能在控件上调用 Invoke 或 BeginInvoke的错误解决办法
以下是错误报告: 标题: SQL Server 安装程序失败. ------------------------------ SQL Server 安装程序遇到以下错误: 在创建窗口句柄之 ...
- SQL 编辑
局部变量: DECLARE @variable_name Datatype Variable_naem为局部变量的名称,Datatype为数据名称. 例如: DECLARE @name varchar ...
- AOP 底层技术比较
表 1. AOP 底层技术比较 AOP 底层技术 功能 性能 面向接口编程 编程难度 直接改写 class 文件 完全控制类 无明显性能代价 不要求 高,要求对 class 文件结构和 Java 字节 ...
- C++ 编译器内存错误 after Normal block。。。
解决 after Normal block(#908) at 0x399EC0. CRT detected that the application wrote to memory after end ...