前文中介绍了FlowLayout和BorderLayout 本文我们将会继续介绍java中的布局方式

(3)GridLayout 网格布局

这种布局会将整个容器划分成M行*N列的网格。

如下图:

                                                                                                     
       
       
       
       
       
       
       

由模型图我们可以知道这种布局,类似于我们常见的扫雷、计算器等软件的布局。

这种布局的构造函数有三种

 GridLayout()  //一行一列 

 GridLayout(int rows, int cols) 

 GridLayout(int rows, int cols, int hgap, int vgap)//hgap 水平间距, vgap垂直间距

在向这种容器中添加控件时,会以向左后右,先上后下的顺序添加(防盗连接:本文首发自http://www.cnblogs.com/jilodream/ )控件。 而不能在指定的位置中添加控件,换言之控件之间不能留有空白。 下面我们来看代码

 import java.awt.*;
import javax.swing.*; class GridFrame extends JFrame
{
JPanel panel=new JPanel(new GridLayout(4,4,3,3));//构造指定布局的容器
String str[]={"7","8","9","/","4","5","6","*","1","2","3","-","0",".","=","+"};
public GridFrame(String name)
{
super(name);
setLayout(new BorderLayout());
JButton btnArray[];
btnArray=new JButton[str.length];
for(int i=0;i<str.length;i++)
{
btnArray[i]=new JButton(str[i]);
panel.add(btnArray[i]);
}
setVisible(true);
setSize(250,200);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
} public static void main(String[] args)
{
GridFrame gf=new GridFrame("网格布局计算机!");
} }

显示效果如下图

(4)GridBagLayout

GridBagLayout也是一种表格,但是可以通过设定规则更自由的将控件绑定到容器上:

如下图:

将容器切割为若干个小的格子Cell,然后向这些Cell中添加控件,同时可以在某一个(防盗连接:本文首发自http://www.cnblogs.com/jilodream/ )方向上连续的几个Cell进行拼接,组成一个大的Cell放置控件。

操作步骤如下:

(1)new GridBagLayout() 设置到指定容器上。

(2)GridBagConstraint  gbc 新增这样的一个容器规则。(注意Constraint是规则、约束、条件的意思)

设定的参数规则如下

gridx  gridy 索引

gridwidth gridheight 跨越的索引

fill  是否动态扩充

weightx、weighty 扩大的权重

(3)gb.setConstraints(控件,gbc)  将控件和规则绑定起来

(4)constainer.add(c) 添加容器,然后将控件添加到容器上。

同时gbc 可以重用,反复的设定骨子额,绑定规则,添加容器

重复 2、3、4步

请参照 如下代码 ,重点注意 addComponent()方法:

 public class NumberPad {
private static final Insets insets = new Insets(0, 0, 0, 0);
public static void main(final String args[]) {
final JFrame frame = new JFrame("NumberPad");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//窗口操作
frame.setLayout(new GridBagLayout());//框架布局
JButton button;
//下面利用设立的类对按键进行布局
//第一行
button = new JButton("Num");
addComponent(frame, button, 0, 0, 1, 1, GridBagConstraints.CENTER, GridBagConstraints.BOTH);
button = new JButton("/");
addComponent(frame, button, 1, 0, 1, 1, GridBagConstraints.CENTER, GridBagConstraints.BOTH);
button = new JButton("*");
addComponent(frame, button, 2, 0, 1, 1, GridBagConstraints.CENTER, GridBagConstraints.BOTH);
button = new JButton("-");
addComponent(frame, button, 3, 0, 1, 1, GridBagConstraints.CENTER, GridBagConstraints.BOTH);
//第二行
button = new JButton("1");
addComponent(frame, button, 0, 1, 1, 1, GridBagConstraints.CENTER, GridBagConstraints.BOTH);
button = new JButton("2");
addComponent(frame, button, 1, 1, 1, 1, GridBagConstraints.CENTER, GridBagConstraints.BOTH);
button = new JButton("3");
addComponent(frame, button, 2, 1, 1, 1, GridBagConstraints.CENTER, GridBagConstraints.BOTH);
button = new JButton("+");
addComponent(frame, button, 3, 1, 1, 2, GridBagConstraints.CENTER, GridBagConstraints.BOTH);
// 第三行
button = new JButton("4");
addComponent(frame, button, 0, 2, 1, 1, GridBagConstraints.CENTER, GridBagConstraints.BOTH);
button = new JButton("5");
addComponent(frame, button, 1, 2, 1, 1, GridBagConstraints.CENTER, GridBagConstraints.BOTH);
button = new JButton("6");
addComponent(frame, button, 2, 2, 1, 1, GridBagConstraints.CENTER, GridBagConstraints.BOTH);
//第四行
button = new JButton("7");
addComponent(frame, button, 0, 3, 1, 1, GridBagConstraints.CENTER, GridBagConstraints.BOTH);
button = new JButton("8");
addComponent(frame, button, 1, 3, 1, 1, GridBagConstraints.CENTER, GridBagConstraints.BOTH);
button = new JButton("9");
addComponent(frame, button, 2, 3, 1, 1, GridBagConstraints.CENTER, GridBagConstraints.BOTH);
button = new JButton("Enter");
addComponent(frame, button, 3, 3, 1, 2, GridBagConstraints.CENTER, GridBagConstraints.BOTH);
//第五行
button = new JButton("0");
addComponent(frame, button, 0, 4, 2, 1, GridBagConstraints.CENTER, GridBagConstraints.BOTH);
button = new JButton(".");
addComponent(frame, button, 2, 4, 1, 1, GridBagConstraints.CENTER, GridBagConstraints.BOTH);
frame.setSize(250,250);
frame.setVisible(true);
} private static void addComponent(Container container, Component component, int gridx, int gridy,
int gridwidth, int gridheight, int anchor, int fill) {
GridBagConstraints gbc = new GridBagConstraints(gridx, gridy, gridwidth, gridheight, 1.0, 1.0,
anchor, fill, insets, 0, 0);//建立网格包对象
container.add(component, gbc);//添加到容器中
}

ps 此代码来自http://blog.sina.com.cn/s/blog_6cea57330100pwvq.html

这里有几点要注意的

(1)控制动态扩充的fill参数,有四个值分别为 水平扩充、垂直扩充、水平垂直扩充、不扩充。大家根据自己的实际情况来选择

(2)weightx、weighty 是扩大的权重,这个权重表示的是,当横向(防盗连接:本文首发自http://www.cnblogs.com/jilodream/ )或纵向扩大N的倍数后,那么他占的比重是多少。这里要小心,就是权重大的话,如果往大拖容器size,那么这个控件增长的快,同时当size变小时,权重大的也缩小的更快。所以如果要想让权重大的控件始终大时,需要将几个控件的初始size设定的非常小,这样所有的控件都是在拖大的状态了。

java布局学习 (二)的更多相关文章

  1. Java开发学习(二十二)----Spring事务属性、事务传播行为

    一.事务配置 上面这些属性都可以在@Transactional注解的参数上进行设置. readOnly:true只读事务,false读写事务,增删改要设为false,查询设为true. timeout ...

  2. Java开发学习(二十四)----SpringMVC设置请求映射路径

    一.环境准备 创建一个Web的Maven项目 参考Java开发学习(二十三)----SpringMVC入门案例.工作流程解析及设置bean加载控制中环境准备 pom.xml添加Spring依赖 < ...

  3. Java开发学习(二十五)----使用PostMan完成不同类型参数传递

    一.请求参数 请求路径设置好后,只要确保页面发送请求地址和后台Controller类中配置的路径一致,就可以接收到前端的请求,接收到请求后,如何接收页面传递的参数? 关于请求参数的传递与接收是和请求方 ...

  4. Java开发学习(二十六)----SpringMVC返回响应结果

    SpringMVC接收到请求和数据后,进行了一些处理,当然这个处理可以是转发给Service,Service层再调用Dao层完成的,不管怎样,处理完以后,都需要将结果告知给用户. 比如:根据用户ID查 ...

  5. Java开发学习(二十七)----SpringMVC之Rest风格解析及快速开发

    一.REST简介 REST(Representational State Transfer),表现形式状态转换,它是一种软件架构风格 当我们想表示一个网络资源的时候,可以使用两种方式: 传统风格资源描 ...

  6. Java开发学习(二十八)----拦截器(Interceptor)详细解析

    一.拦截器概念 讲解拦截器的概念之前,我们先看一张图: (1)浏览器发送一个请求会先到Tomcat的web服务器 (2)Tomcat服务器接收到请求以后,会去判断请求的是静态资源还是动态资源 (3)如 ...

  7. java布局学习(新)

    坚持学习java一段时间,最近自己需要做一个小型的系统,所以需要自己将自己的AWT知识巩固一下. 一.4大布局管理器. 1.边界布局BorderLayout 是JFrame和JDialog的默认布局方 ...

  8. java布局学习 (一)

    Java 程序通过jvm可以很好的移植到其他平台上,但是java 生成的图形界面样式,在不使用布局的情况下,往往需要重新设定大小,才能在新的平台上调整到最佳样式.这是由于组件的最佳大小 往往是与平台相 ...

  9. JAVA NIO学习二:通道(Channel)与缓冲区(Buffer)

    今天是2018年的第三天,真是时光飞逝,2017年的学习计划还没有学习完成,因此继续开始研究学习,那么上一节我们了解了NIO,那么这一节我们进一步来学习NIO相关的知识.那就是通道和缓冲区.Java ...

随机推荐

  1. 邮箱、手机号、中文 js跟php正则验证

    邮箱正则: jS: var regEmail = /^([a-zA-Z0-9_-])+@([a-zA-Z0-9_-])+(\.[a-zA-Z0-9_-])+/; //验证 if(regEmail.te ...

  2. php 开启socket配置

    我在Windows命令行输入命令:C:\wamp\bin\php\php5.2.6\php.exe getXml.php以执行php文件. 出现错误:Fatal error: Call to unde ...

  3. Node.js的学习路线

    http://www.admin10000.com/document/4624.html 顺便关注一下博客:http://blog.fens.me/series-nodejs/ php socket框 ...

  4. 使用MS Test进行单元测试

    MS Test也可以方便的进行单元测试,可以通过Visual Studio很方便的建立单元测试. 添加对待测试工程的引用,即可方便的开始单元测试. 最基本的一些测试使用如下: using System ...

  5. matlab处理图像代码

    1.图像的读取MATLAB中从图像文件中读取数据用函数imread(),这个函数的作用就是将图像文件的数据读入矩阵中,此外还可以用imfinfo()函数查看图像文件的信息(见例1)%例1:图像数据及图 ...

  6. Unity学习疑问记录之触屏

    当将Unity游戏运行到ios或android设备上时,桌面系统中的鼠标左键操作可以自动变为手机屏幕上的触屏操作,但鼠标操作无法实现一些特有的触屏操作,比如多点触屏. 触控对于Android移动设备来 ...

  7. 本地测试SMTP服务器

    一年前看计算机网络里的电子邮件协议的时候,想自己建一个本地SMTP服务器,然后用telnet发送消息,后来是没找到.这次又翻看了一下,找了个Windows平台下的hMailServer,挺方便的.步骤 ...

  8. [转载] 自定义百度网盘分享密码 (Javascript)

    压缩版 javascript:require(["function-widget-1:share/util/service/createLinkShare.js"]).protot ...

  9. jquety

    .html() //能够获取元素里的内容包括标签: .text()  //只能获取到元素里的内容 .val()  //可以得到元素的value值 $(this).index()  //得到点时间发生的 ...

  10. 自动化测试工具——JMeter

    Apache JMeter是Apache组织开发的基于Java的压力测试工具.用于对软件做压力测试,它最初被设计用于Web应用测试,但后来扩展到其他测试领域. 它可以用于测试静态和动态资源,例如静态文 ...