SWT(JFace)体验之FormLayout布局

 更新时间:2009年06月25日 11:22:38   作者:  
SWT(JFace)体验之FormLayout布局示例代码。
测试代码如下:
复制代码 代码如下:

package swt_jface.demo2;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.FormAttachment;
import org.eclipse.swt.layout.FormData;
import org.eclipse.swt.layout.FormLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
public class FormLayoutSample {
Display display = new Display();
Shell shell = new Shell(display);
public FormLayoutSample() {
shell.setLayout(new FormLayout());
Button button1 = new Button(shell, SWT.PUSH);
button1.setText("button1");
FormData formData = new FormData();
formData.left = new FormAttachment(20);
formData.top = new FormAttachment(20);
button1.setLayoutData(formData);


Button button2 = new Button(shell, SWT.PUSH);
button2.setText("button number 2");

formData = new FormData();
formData.left = new FormAttachment(button1, 0, SWT.CENTER);
formData.top = new FormAttachment(button1, 0, SWT.CENTER);
button2.setLayoutData(formData);

// Button button3 = new Button(shell, SWT.PUSH);
// button3.setText("3");
//
// formData = new FormData();
// formData.top = new FormAttachment(button2, 10);
// formData.left = new FormAttachment(button2, 0, SWT.LEFT);
// button3.setLayoutData(formData);
shell.pack();
//shell.setSize(500, 600);
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch()) {
display.sleep();
}
}
display.dispose();
}
public static void main(String[] args) {
new FormLayoutSample();
}
}

再看一个例子:
复制代码 代码如下:

package swt_jface.demo2;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.layout.FormAttachment;
import org.eclipse.swt.layout.FormData;
import org.eclipse.swt.layout.FormLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.List;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Shell;
public class Main {
public static void main(String[] args) {
Display display = new Display();
final Shell shell = new Shell(display);
Label label = new Label(shell, SWT.WRAP);
label
.setText("This is a long text string that will wrap when the dialog is resized.");
List list = new List(shell, SWT.BORDER | SWT.H_SCROLL | SWT.V_SCROLL);
list.setItems(new String[] { "Item 1", "Item2" });
Button button1 = new Button(shell, SWT.PUSH);
button1.setText("Ok");
Button button2 = new Button(shell, SWT.PUSH);
button2.setText("Cancel");
final int insetX = 4, insetY = 4;
FormLayout formLayout = new FormLayout();
formLayout.marginWidth = insetX;
formLayout.marginHeight = insetY;
shell.setLayout(formLayout);
Point size = label.computeSize(SWT.DEFAULT, SWT.DEFAULT);
final FormData labelData = new FormData(size.x, SWT.DEFAULT);
labelData.left = new FormAttachment(0, 0);
labelData.right = new FormAttachment(100, 0);
label.setLayoutData(labelData);
shell.addListener(SWT.Resize, new Listener() {
public void handleEvent(Event e) {
Rectangle rect = shell.getClientArea();
labelData.width = rect.width - insetX * 2;
shell.layout();
}
});
FormData button2Data = new FormData();
button2Data.right = new FormAttachment(100, -insetX);
button2Data.bottom = new FormAttachment(100, 0);
button2.setLayoutData(button2Data);
FormData button1Data = new FormData();
button1Data.right = new FormAttachment(button2, -insetX);
button1Data.bottom = new FormAttachment(100, 0);
button1.setLayoutData(button1Data);
FormData listData = new FormData();
listData.left = new FormAttachment(0, 0);
listData.right = new FormAttachment(100, 0);
listData.top = new FormAttachment(label, insetY);
listData.bottom = new FormAttachment(button2, -insetY);
list.setLayoutData(listData);
shell.pack();
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch())
display.sleep();
}
display.dispose();
}
}

相关文章

  • Java爬虫爬取漫画示例

    Java爬虫爬取漫画示例

    这篇文章主要介绍了Java爬虫爬取漫画示例,大部分的爬虫入门教学都是爬取图片的,本文就来测试一下爬取网站的漫画,需要的朋友可以参考下
    2023-04-04
  • Springboot Thymeleaf字符串对象实例解析

    Springboot Thymeleaf字符串对象实例解析

    这篇文章主要介绍了Springboot Thymeleaf字符串对象实例解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2007-09-09
  • javacv ffmpeg使用原生ffmpeg命令方式

    javacv ffmpeg使用原生ffmpeg命令方式

    在使用javacv集成ffmpeg进行视频处理时,发现使用ffmpeg-6.0-1.5.9版本出现原生命令执行失败的问题,通过降级至ffmpeg-5.1.2-1.5.8版本,问题得到解决,此外,ffprobe可以用于获取视频属性,需确保视频片段属性一致性
    2024-11-11
  • Java使用freemarker实现word下载方式

    Java使用freemarker实现word下载方式

    文章介绍了如何使用FreeMarker实现Word文件下载,包括引用依赖、创建Word模板、将Word文件存为XML格式、更改后缀为FTL模板、处理图片和代码实现
    2025-02-02
  • java方法重载和参数类型自动提升方式

    java方法重载和参数类型自动提升方式

    这篇文章主要介绍了java方法重载和参数类型自动提升方式,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2022-02-02
  • JavaMe开发绘制文本框TextEdit

    JavaMe开发绘制文本框TextEdit

    在JavaMe连载(3)-也说MVC设计模式 一文中提到了一个TextEdit类,但没有给出具体实现,TextEdit是采用GameCanvas绘制的文本编辑器。本文结合实例给出实现的方法。
    2015-09-09
  • Java读写锁ReadWriteLock的创建使用及测试分析示例详解

    Java读写锁ReadWriteLock的创建使用及测试分析示例详解

    这篇文章主要为大家介绍了Java读写锁ReadWriteLock的创建使用及测试分析示例详解,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2023-01-01
  • JAVA 16位ID生成工具类含16位不重复的随机数数字+大小写

    JAVA 16位ID生成工具类含16位不重复的随机数数字+大小写

    这篇文章主要介绍了JAVA 16位ID生成工具类含16位不重复的随机数数字+大小写,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2020-02-02
  • 深入理解Java中的IOUtils(示例演示)

    深入理解Java中的IOUtils(示例演示)

    Java中的IOUtils是一个工具类,用于简化文件和流的操作,它提供了一些常用的方法,如复制文件、读取文件、写入文件等,这篇文章主要介绍了深入理解Java中的IOUtils(示例演示),需要的朋友可以参考下
    2023-08-08
  • 解决idea中svn提交时performing vcs refresh时间很长的问题

    解决idea中svn提交时performing vcs refresh时间很长的问题

    这篇文章主要介绍了解决idea中svn提交时performing vcs refresh时间很长的问题,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2020-09-09

最新评论