最近在研究Eclipse中的GEF开发,在跟着GEF-whole-upload教程做一个GEF应用程序的例子时,发现Figure上的控件无法显示,谷歌了很久也没找到解决方案,最后终于发现是Layout的问题。同时发现还没有人进行过这方面的研究,于是打算写一篇文章对各种Layout进行比较。由于GEF的绘图部分使用的是Draw2d,因此本文是关于Draw2d中的Layout比较。

Draw2d中常用的Layout有BorderLayout、ToolbarLayout、FlowLayout、GridLayout、XYLayout。它们都继承于AbstractLayout,类图如下:

下面本文将对这些Layout的用法进行说明。

BorderLayout

BorderLayout是按五个区域进行布局,即上下左右中。代码如下:

protected IFigure createFigure() {
// TODO Auto-generated method stub
Figure figure = new Figure();
figure.setLayoutManager(new BorderLayout()); Label label1 = new Label();
label1.setText("test1");
figure.add(label1, BorderLayout.LEFT); Label label2 = new Label();
label2.setText("test2");
figure.add(label2, BorderLayout.RIGHT); Label label3 = new Label();
label3.setText("test3");
figure.add(label3, BorderLayout.TOP); Label label4 = new Label();
label4.setText("test4");
figure.add(label4, BorderLayout.BOTTOM); Label label5 = new Label();
label5.setText("test5");
figure.add(label5, BorderLayout.CENTER); return figure;
}

效果如下:

ToolbarLayout

ToolbarLayout顾名思义,类似于工具栏按钮的布局,可以设置控件布局的方向、间隔等,代码如下:
protected IFigure createFigure() {
// TODO Auto-generated method stub
Figure figure = new Figure(); ToolbarLayout layout = new ToolbarLayout();
layout.setMinorAlignment(ToolbarLayout.ALIGN_TOPLEFT);
layout.setStretchMinorAxis(false);
layout.setSpacing(2);
figure.setLayoutManager(layout); Label label1 = new Label();
label1.setText("test1");
figure.add(label1); Label label2 = new Label();
label2.setText("test2");
figure.add(label2); Label label3 = new Label();
label3.setText("test3");
figure.add(label3); Label label4 = new Label();
label4.setText("test4");
figure.add(label4); Label label5 = new Label();
label5.setText("test5");
figure.add(label5); return figure;
}

效果如下:



FlowLayout

FlowLayout与ToolbarLayout类似,都是继承于OrderLayout。唯一的不同是FlowLayout允许控件布局的时候换行,而ToolbarLayout只能有一行。代码如下:
protected IFigure createFigure() {
// TODO Auto-generated method stub
Figure figure = new Figure(); FlowLayout flowLayout = new FlowLayout(true);//水平
flowLayout.setMinorSpacing(20);
flowLayout.setMajorAlignment(FlowLayout.ALIGN_TOPLEFT);
figure.setLayoutManager(flowLayout); Label label1 = new Label();
label1.setText("test1");
figure.add(label1); Label label2 = new Label();
label2.setText("test2");
figure.add(label2); Label label3 = new Label();
label3.setText("test3");
figure.add(label3); Label label4 = new Label();
label4.setText("test4");
figure.add(label4); Label label5 = new Label();
label5.setText("test5");
figure.add(label5); return figure;
}

效果如下:



GridLayout

GridLayout是网格布局,即将控件按照网格的形式排列,可以通过numColumns参数指定有几列。具体每个格子的大小可以通过GridData修改。代码如下:
protected IFigure createFigure() {
// TODO Auto-generated method stub
Figure figure = new Figure(); GridLayout gridLayout = new GridLayout();
gridLayout.numColumns = 2;
figure.setLayoutManager(gridLayout); Label label1 = new Label();
label1.setText("test1");
figure.add(label1); GridData label_gd1 = new GridData();
label_gd1.widthHint = 50;
label_gd1.heightHint = 50;
gridLayout.setConstraint(label1, label_gd1); Label label2 = new Label();
label2.setText("test2");
figure.add(label2); Label label3 = new Label();
label3.setText("test3");
figure.add(label3); Label label4 = new Label();
label4.setText("test4");
figure.add(label4); Label label5 = new Label();
label5.setText("test5");
figure.add(label5); return figure;
}

效果如下:



XYLayout

XYLayout是通过绝对坐标进行定位,在代码中指定每个控件的坐标即可。代码如下:
protected IFigure createFigure() {
// TODO Auto-generated method stub
Figure figure = new Figure();
figure.setLayoutManager(new XYLayout()); Label label1 = new Label();
label1.setText("test1");
figure.add(label1, new Rectangle(0, 0, 50, 50)); Label label2 = new Label();
label2.setText("test2");
figure.add(label2, new Rectangle(25, 25, 50, 50)); Label label3 = new Label();
label3.setText("test3");
figure.add(label3, new Rectangle(50, 50, 50, 50)); Label label4 = new Label();
label4.setText("test4");
figure.add(label4, new Rectangle(75, 100, 50, 50)); Label label5 = new Label();
label5.setText("test5");
figure.add(label5, new Rectangle(40, 75, 50, 50)); return figure;
}

效果如下:


Draw2d中的布局管理器Layout比较的更多相关文章

  1. 第六章 Qt布局管理器Layout

    第六章 Qt布局管理器Layout 大家有没有发现一个现象,我们放置一个组件,给组件最原始的定位是给出这个控件的坐标和宽高值,这样Qt就知道这个组件的位置.当用户改变窗口的大小,组件还静静地呆在原来的 ...

  2. JAVA中GridBagLayout布局管理器应用详解

    很多情况下,我们已经不需要通过编写代码来实现一个应用程序的图形界面,而是通过强大的IDE工具通过拖拽辅以简单的事件处理代码即可很轻松的完成.但是我们不得不面对这样操作存在的一些问题,有时候我们希望能够 ...

  3. 【java】浅析java组件中的布局管理器

    这篇博文笔者介绍一下java组件中,常用的布局管理器.java组件中的布局方式有好几十种,所有的这些布局管理器都实现了java.awt.LayoutManager接口.接下来笔者介绍一下常用的5种布局 ...

  4. Qt中的布局管理器

    1. 布局管理器提供相关的类对界面组件进行布局管理,能够自动排列窗口中的界面组件,窗口变化后能自动更新界面组件的大小. 2. QLayout是Qt布局管理器的抽象基类,通过继承QLayout实现了功能 ...

  5. GridBagLayout:网格包布局管理器

    GridBagLayout:网格包布局管理器   GridBagLayout可以说是布局管理器Layout中最复杂的一个,其中涉及到的参数也比较得多,比如说: GridBagConstraints g ...

  6. 【Android 应用开发】AndroidUI设计之 布局管理器 - 详细解析布局实现

    写完博客的总结 : 以前没有弄清楚的概念清晰化 父容器与本容器属性 : android_layout...属性是本容器的属性, 定义在这个布局管理器的LayoutParams内部类中, 每个布局管理器 ...

  7. Swing——布局管理器

    前言 在编写图形界面时,总是需要考虑的就是组件放在哪里,组件怎么大才合适.在Swing中就有现成的布局管理器帮我们做这些事情,我们不必写代码去一一安排.下面将介绍什么是布局管理器.Swing中常用布局 ...

  8. AndroidUI设计之 布局管理器 - 详细解析布局实现

    写完博客的总结 : 以前没有弄清楚的概念清晰化 父容器与本容器属性 : android_layout...属性是本容器的属性, 定义在这个布局管理器的LayoutParams内部类中, 每个布局管理器 ...

  9. 二、Android应用的界面编程(二)布局管理器

    一.线性布局所有布局都可以作为容器类使用,因此可以调用多个重载的addView()向布局管理器中添加组件.实际上,我们完全可以用一个布局管理器嵌套到其他布局管理器中---因为布局管理器也继承了View ...

随机推荐

  1. 解决Sublime-Text-3在ubuntu下中文输入的问题

    在ubuntu下使用ST这神器已经一段日子了,但是一直有个纠结的问题,就是中文输入非常坑爹,曾经一段时间,使用inputHelper这个插件来解决, 但是……每次都要按个快捷键,弹出一个小小小框来输入 ...

  2. php每秒输出一次

    首先说到php.ini中的两个配置 output_buffering配置•Off: 表示关闭PHP输出缓存•On: 打开无限大的输出缓存•4096: 打开大小为4096Byte的输出缓存(默认) im ...

  3. R语言的数据结构

    首先声明,R语言对大小写敏感. 一.向量 vector类型可以存储数字.字符和逻辑类型.构建函数为C(): > a <- c(1,2,3,4) > b <- c('dau',' ...

  4. 深入理解Azure自动扩展集VMSS(1)

    前文中已经详细介绍了如何配置和部署Azure的虚拟机扩展集VMSS进行自动扩展,但在实际使用过程当中,用户会出现更进一步使用的一些问题,VMSS基本扩展原理及怎么简单调试?如何进行手动扩展?怎么使用自 ...

  5. [bzoj 1001][Beijing2006]狼抓兔子 (最小割+对偶图+最短路)

    Description 现在小朋友们最喜欢的"喜羊羊与灰太狼",话说灰太狼抓羊不到,但抓兔子还是比较在行的, 而且现在的兔子还比较笨,它们只有两个窝,现在你做为狼王,面对下面这样一 ...

  6. cf B. Little Dima and Equation

    http://codeforces.com/contest/460/problem/B import java.util.*; import java.math.*; public class Mai ...

  7. hdu 1111 Secret Code

    http://acm.hdu.edu.cn/showproblem.php?pid=1111 复数除法: #include <cstdio> #include <cstring> ...

  8. 从头到尾彻底理解KMP(2014年8月22日版)

    http://blog.csdn.net/v_july_v/article/details/7041827

  9. key转成pvf

    https://www.godaddy.com/help/converting-an-exported-pfx-code-signing-file-to-pvk-and-spc-files-using ...

  10. 《Programming WPF》翻译 第6章 3.二进制资源

    原文:<Programming WPF>翻译 第6章 3.二进制资源 尽管ResourceDictionary和系统级别的资源适合于作为数据存在于对象中,然而,并不是所有的资源都能很好的满 ...