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 ...
随机推荐
- 字符数组char
数组做sizeof的参数不退化,传递给strlen就退化为指针: #include<stdio.h> #include<stdlib.h> #include<strin ...
- concurrency parallel 并发 并行
Computer Systems A Programmer's Perspective Second Edition The general phenomenon of multiple flows ...
- 【php学习】字符串操作
关于字符串的处理,基本上就是那几种操作:字符串长度.查找子字符串的位置.替换字符串.截取字符串.拆分合并字符串 ... 字符串的定义:直接 $str = "abcd"; 或者 $s ...
- 蓝牙的L2CAP协议
1.概述 L2CAP能向上层提供面向连接的或者无连接的数据服务,拥有multiplexing capability and segmentation and reassembly operat ...
- Apache的HBase与cdh的hue集成(不建议不同版本之间的集成)
1.修改hue的配置文件hue.ini [hbase] # Use full hostname with security. hbase_clusters=(Cluster|linux-hadoop3 ...
- ZooKeeper Recipes and Solutions 翻译
ZooKeeper 秘诀 与解决方案 A Guide to Creating Higher-level Constructs with ZooKeeper Out of the Box Applica ...
- php--yii框架中的ajax分页与yii框架自带的分页
要想使用Yii分页类 第一步:在控制器层加载分页类 use yii\data\Pagination; 第二步: 使用model层查询数据,并用分分页,限制每页的显示条数 $data = Zhao::f ...
- Mac终端命令行提示符格式更改方法
内容提要: 主要是通过~/.bash_profile文件更改环境变量PS1,修改命令行提示符的显示格式,并展示不同颜色. 本文介绍了默认设置的缺陷,以及需要用到的基础知识,最后介绍了更改命令行提示符格 ...
- LightOj1203 - Guarding Bananas(凸包求多边形中的最小角)
题目链接:http://lightoj.com/volume_showproblem.php?problem=1203 题意:给你一个点集,求凸包中最小的角:模板题,但是刚开始的时候模板带错了,错的我 ...
- Oracle 11G INDEX FULL SCAN 和 INDEX FAST FULL SCAN 对比分析
SQL> drop table test; 表已删除. SQL> create table test as select * from dba_objects where 1!=1; 表已 ...