免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 2321 | 回复: 6
打印 上一主题 下一主题

SWT&Jface权威指南---SWT中布局管理器 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2005-03-30 19:37 |只看该作者 |倒序浏览
/**
*大家一起来学习
*本例由于和AWT  Swing类似,所以没有多作解释,如友疑问,请回帖
*作者:李天泉
*/
import org.eclipse.swt.widgets.*;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.SWT;

public class FillLayoutHorizontal {
  public static void main(String[] args) {
    Display display = new Display();
    Shell shell = new Shell(display);
    shell.setLayout(new FillLayout(SWT.HORIZONTAL));//设置排列方式
    new Button(shell, SWT.PUSH).setText("按鈕一");//在shell上新建三个按钮
    new Button(shell, SWT.PUSH).setText("按鈕二");
    new Button(shell, SWT.PUSH).setText("按鈕三");
    shell.setText("順天科技--勤學博思製作");//设置窗口标题
    shell.open();//使窗口可见
    while (!shell.isDisposed()) {
      if (!display.readAndDispatch()) {//监听事件
        display.sleep();
      }
    }
    display.dispose();//释放资源
  }
}

论坛徽章:
0
2 [报告]
发表于 2005-03-30 19:39 |只看该作者

SWT&Jface权威指南---SWT中布局管理器

运行的图片

FillLayoutHorizontal.jpg (15.43 KB, 下载次数: 26)

FillLayoutHorizontal.jpg

论坛徽章:
0
3 [报告]
发表于 2005-03-30 19:43 |只看该作者

SWT&Jface权威指南---SWT中布局管理器

import org.eclipse.swt.widgets.*;
import org.eclipse.swt.layout.RowLayout;
import org.eclipse.swt.SWT;

public class RowLayoutHorizontal {
  public static void main(String[] args) {
    Display display = new Display();
    Shell shell = new Shell(display);
    shell.setLayout(new RowLayout(SWT.HORIZONTAL));
    new Button(shell, SWT.PUSH).setText("one";
    new Button(shell, SWT.PUSH).setText("two";
    new Button(shell, SWT.PUSH).setText("three";
    new Button(shell, SWT.PUSH).setText("four";
    new Button(shell, SWT.PUSH).setText("five";
    new Button(shell, SWT.PUSH).setText("six";
    new Button(shell, SWT.PUSH).setText("seven";
    shell.setText("RowLayoutHorizontal 的練習--順天科技製作";
    shell.open();
    while (!shell.isDisposed()) {
      if (!display.readAndDispatch()) {
        display.sleep();
      }
    }
    display.dispose();
  }
}

RowLayoutHorizontal.jpg (8.45 KB, 下载次数: 23)

RowLayoutHorizontal.jpg

论坛徽章:
0
4 [报告]
发表于 2005-03-30 19:51 |只看该作者

SWT&Jface权威指南---SWT中布局管理器

import org.eclipse.swt.widgets.*;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.SWT;

public class GridLayout2x2 {
  public static void main(String[] args) {
    Display display = new Display();
    Shell shell = new Shell(display);
    GridLayout layout = new GridLayout();
    layout.numColumns = 2; //設置行數
    layout.makeColumnsEqualWidth = true;//使每列的宽度相等
    shell.setLayout(layout);//设置shell的布局方式

    GridData data = new GridData(GridData.FILL_BOTH);
    Button one = new Button(shell, SWT.PUSH);//设置按钮的外观为PUSH
    one.setText("one";
    one.setLayoutData(data);

    data = new GridData(GridData.FILL_BOTH);
    Button two = new Button(shell, SWT.PUSH);
    two.setText("two";
    two.setLayoutData(data);

    data = new GridData(GridData.FILL_BOTH);
    Button three = new Button(shell, SWT.PUSH);
    three.setText("three";
    three.setLayoutData(data);

    data = new GridData(GridData.FILL_BOTH);
    Button four = new Button(shell, SWT.PUSH);
    four.setText("four";
    four.setLayoutData(data);
    shell.setText("順天科技--勤學博思製作";       
    shell.pack();//使窗口適應控件
    shell.open();
    while (!shell.isDisposed()) {
      if (!display.readAndDispatch()) {
        display.sleep();
      }
    }
    display.dispose();
  }
}

GridLayout2x2.jpg (7.72 KB, 下载次数: 30)

GridLayout2x2.jpg

论坛徽章:
0
5 [报告]
发表于 2005-03-30 19:56 |只看该作者

SWT&Jface权威指南---SWT中布局管理器

import org.eclipse.swt.widgets.*;
import org.eclipse.swt.layout.FormAttachment;
import org.eclipse.swt.layout.FormData;
import org.eclipse.swt.layout.FormLayout;
import org.eclipse.swt.SWT;

public class FormLayoutFormAttachment {
  public static void main(String[] args) {
    Display display = new Display();
    Shell shell = new Shell(display);
    FormLayout layout = new FormLayout();
    layout.marginHeight = 5;
    layout.marginWidth = 10;
    shell.setLayout(layout);
    Button button = new Button(shell, SWT.PUSH);
    button.setText("Button";
    FormData data = new FormData();
    data.height = 50;
    data.right = new FormAttachment(100, -50);
    data.left = new FormAttachment(0, 10);
    data.top = new FormAttachment(1, 4, 0);
    button.setLayoutData(data);

    Button button2 = new Button(shell, SWT.PUSH);
    button2.setText("Button 2";
    data = new FormData();
    button2.setLayoutData(data);
    data.bottom = new FormAttachment(100, 0);
    data.top = new FormAttachment(button, 5);
    data.left = new FormAttachment(button, 0, SWT.LEFT);
    data.right = new FormAttachment(button, 0, SWT.RIGHT);
    shell.setText("順天科技--勤學博思製作";
    shell.pack();
    shell.open();
    while (!shell.isDisposed()) {
      if (!display.readAndDispatch()) {
        display.sleep();
      }
    }
    display.dispose();
  }
}

FormLayoutFormAttachment.jpg (7.32 KB, 下载次数: 24)

FormLayoutFormAttachment.jpg

论坛徽章:
0
6 [报告]
发表于 2005-03-30 19:59 |只看该作者

SWT&Jface权威指南---SWT中布局管理器

import org.eclipse.swt.events.*;
import org.eclipse.swt.widgets.*;
import org.eclipse.swt.custom.StackLayout;
import org.eclipse.swt.SWT;

public class StackLayoutTest {
  public static void main(String[] args) {
    Display display = new Display();
    Shell shell = new Shell(display);
    StackLayout layout = new StackLayout();
    shell.setLayout(layout);
    StackLayoutSelectionAdapter adapter = new StackLayoutSelectionAdapter(shell,
        layout);
    Button one = new Button(shell, SWT.PUSH);
    one.setText("one";
    one.addSelectionListener(adapter);
    Button two = new Button(shell, SWT.PUSH);
    two.setText("two";
    two.addSelectionListener(adapter);
    Button three = new Button(shell, SWT.PUSH);
    three.setText("three";
    three.addSelectionListener(adapter);
    layout.topControl = one;
    shell.setText("順天科技--勤學博思製作";
    shell.open();
    while (!shell.isDisposed()) {
      if (!display.readAndDispatch()) {
        display.sleep();
      }
    }
    display.dispose();
  }
}

class StackLayoutSelectionAdapter extends SelectionAdapter {
  Shell shell;
  StackLayout layout;

  public StackLayoutSelectionAdapter(Shell shell, StackLayout layout) {
    this.shell = shell;
    this.layout = layout;
  }
  public void widgetSelected(SelectionEvent event) {
    Control control = layout.topControl;
    Control[] children = shell.getChildren();
    int i = 0;
    for (int n = children.length; i < n; i++) {
      Control child = children;
      if (child == control) {
        break;
      }
    }
    ++i;
    if (i >;= children.length)
      i = 0;
    layout.topControl = children;
    shell.layout();
  }
}

StackLayoutTest.jpg (8.98 KB, 下载次数: 25)

StackLayoutTest.jpg

论坛徽章:
0
7 [报告]
发表于 2005-03-30 20:02 |只看该作者

SWT&Jface权威指南---SWT中布局管理器

import org.eclipse.swt.widgets.*;
import org.eclipse.swt.SWT;

public class NoLayoutSimple {
  public static void main(String[] args) {
    Display display = new Display();
    Shell shell = new Shell(display);
    Button button = new Button(shell, SWT.PUSH);
    button.setText("No layout";
    button.setBounds(5, 5, 100, 100);
    shell.setText("順天科技--勤學博思製作";
    shell.pack();
    shell.open();
    while (!shell.isDisposed()) {
      if (!display.readAndDispatch()) {
        display.sleep();
      }
    }
    display.dispose();
  }
}

NoLayoutSimple.jpg (7.36 KB, 下载次数: 24)

NoLayoutSimple.jpg
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP