swt组件实例--演示Display、Shell以及Text、Button等组件的用法

swt组件实例--演示Display、Shell以及Text、Button等组件的用法,案例效果:

image.png

java代码:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

package swt;

 

import org.eclipse.swt.SWT;

import org.eclipse.swt.events.SelectionAdapter;

import org.eclipse.swt.events.SelectionEvent;

import org.eclipse.swt.layout.FillLayout;

import org.eclipse.swt.widgets.Button;

import org.eclipse.swt.widgets.Display;

import org.eclipse.swt.widgets.Shell;

import org.eclipse.swt.widgets.Text;

 

public class HelloSwt_01 {

    public static void main(String[] args) {

        //负责和操作系统交互,如读取底层事件等

        Display display=new Display();

        //窗口

        final Shell topShell=new Shell(display,SWT.SHELL_TRIM|SWT.BORDER);

        topShell.setText("这是一个窗口");

        topShell.setSize(800500);

        topShell.setLayout(new FillLayout());

        //不设置布局不显示

//      topShell.setLayout(new GridLayout());

        Text text=new Text(topShell,SWT.BORDER);

        text.setText("文本框");

        text.selectAll();

//      text.copy();

//      text.paste();

//      text.clearSelection();

        new Text(topShell, SWT.SINGLE|SWT.NONE).pack();

//      new Text(topShell, SWT.MULTI|SWT.WRAP).setLayoutData(new GridData(GridData.FILL_BOTH));

        Button button=new org.eclipse.swt.widgets.Button(topShell, SWT.BORDER);

        button.setText("创建子窗口");

        button.addSelectionListener(new SelectionAdapter() {

 

            @Override

            public void widgetSelected(SelectionEvent e) {

                creatMuti(topShell,"子窗口");

            }

             

        });

        button.pack();

        text.pack();

//      topShell.pack();

        topShell.open();

         

//      Shell dialogShell=new Shell(topShell,SWT.SHELL_TRIM|SWT.PRIMARY_MODAL);

//      dialogShell.setSize(200, 100);

//      dialogShell.pack();

//      dialogShell.open();

        while(!topShell.isDisposed()){

            if (!display.readAndDispatch()) {

                display.sleep();

            }

        }

        display.dispose();

    }

    public static Shell creatMuti(Shell parent,String name){

        Shell childShell=new Shell(parent, SWT.DIALOG_TRIM);

        childShell.setText(name);

        childShell.setSize(200200);

        childShell.open();

        return childShell;

    }

}

这是使用swt创建图形界面的一个小例子,需要注意的是,别忘了引入swt的jar包。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值