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 ...
随机推荐
- [收藏] 关于解决“进程com.android.phone意外停止”的方法 (未尝试)
很多机油反应有这个情况,本人费劲九牛20虎之力终于克服之,这个现象一般出现在刚刷完系统会出现,甚至你怎么刷ROM这个现象依旧存在(崩溃不?)~~~有位机油刷了这个系统也出现了http://samsun ...
- Android开发的七大环节
Android开发的七大环节 浏览:25 发布日期:2015/10/27 分类:职场感悟 一个完整的Android 开发流程主要包括策划.软件.交互.视觉.测试.运营维护这七大环节,其中的每一个环 ...
- Linux提示no crontab for root的解决办法
Linux提示no crontab for root的解决办法 安装crontab:yum install crontabs 说明:/sbin/service crond start //启动服务/s ...
- solr4.7中文分词器(ik-analyzer)配置
solr本身对中文分词的处理不是太好,所以中文应用很多时候都需要额外加一个中文分词器对中文进行分词处理,ik-analyzer就是其中一个不错的中文分词器. 一.版本信息 solr版本:4.7.0 需 ...
- Bluetooth SDP介绍
目录 1. 概念 2. 服务记录(Service Record) 3. 服务属性(Service Attribute) 4. 服务类(Service Class) 5. 服务查找 5.1 UUID 5 ...
- Naming Service 与 Zookeeper
命名服务是指通过指定的名字来获取资源或者服务的地址,提供者的信息.利用Zookeeper很容易创建一个全局的路径,而这个路径就可以作为 一个名字,它可以指向集群中的集群,提供的服务的地址,远程对象 ...
- Android Handler简单示例
package com.firstapp.foo.firstapp; import android.os.Handler; import android.os.Message; import andr ...
- C# Dictionary几种遍历方式
class Program { static void Main(string[] args) { Dictionary<string, string> myDictionary = ne ...
- 20145211 《Java程序设计》实验报告二:Java面向对象程序设计
实验要求 初步掌握单元测试和TDD 理解并掌握面向对象三要素:封装.继承.多态 初步掌握UML建模 熟悉S.O.L.I.D原则 了解设计模式 实验内容 单元测试 面向对象三要素 设计模式初步 练习 实 ...
- 判断cdn上的图片可以正常访问到
昨天晚上cdn宕机1小时,要对上传的资检查,写了个简单的小脚本来实现上传过的资源都是正常的(其实非必须),就是练手防止生疏. arr.each do |a | res = Net::HTTP.get_ ...