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中多个@Scheduled定时器不执行的解决方法

    java中多个@Scheduled定时器不执行的解决方法

    在应用开发中经常需要一些周期性的操作,比如每5分钟执行某一操作等,这篇文章主要给大家介绍了关于java中多个@Scheduled定时器不执行的解决方法,需要的朋友可以参考下
    2023-04-04
  • Java调用第三方接口封装实现

    Java调用第三方接口封装实现

    本文主要介绍了Java调用第三方接口封装实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2023-02-02
  • Java中documentHelper解析xml获取想要的数据

    Java中documentHelper解析xml获取想要的数据

    本文主要介绍了Java中documentHelper解析xml获取想要的数据,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2023-02-02
  • Java连接数据库oracle中文乱码解决方案

    Java连接数据库oracle中文乱码解决方案

    这篇文章主要介绍了Java连接数据库oracle中文乱码解决方案,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2020-05-05
  • SpringBoot超详细讲解@Enable*注解和@Import

    SpringBoot超详细讲解@Enable*注解和@Import

    这篇文章主要介绍了SpringBoot @Enable*注解和@Import,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2022-07-07
  • 老生常谈java数组中的常见异常

    老生常谈java数组中的常见异常

    数组是用来存储一系列数据,但它往往被认为是一系列相同类型的变量,异常是程序中的一些错误,但并不是所有的错误都是异常,并且错误有时候是可以避免的,接下来让我们详细的了解吧
    2022-03-03
  • IDEA插件之快速删除Java代码中的注释

    IDEA插件之快速删除Java代码中的注释

    这篇文章主要介绍了IDEA插件之快速删除Java代码中的注释,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2021-02-02
  • spring mvc+localResizeIMG实现HTML5端图片压缩上传

    spring mvc+localResizeIMG实现HTML5端图片压缩上传

    这篇文章主要为大家详细介绍了使用spring mvc+localResizeIMG实现HTML5端图片压缩上传,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2017-04-04
  • SpringCloud中的Feign远程调用接口传参失败问题

    SpringCloud中的Feign远程调用接口传参失败问题

    这篇文章主要介绍了SpringCloud中的Feign远程调用接口传参失败问题,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2022-03-03
  • java集合类ArrayList和Vector的区别面试精讲

    java集合类ArrayList和Vector的区别面试精讲

    这篇文章主要为大家介绍了java集合类ArrayList和Vector的区别面试全面讲解,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2023-10-10

最新评论