java swt designerpdf_eclipse学习笔记!(4) ----- SWT Designer 下 SWT常用组件

本文详细介绍了SWT(Standard Widget Toolkit)中Button、Text、Combo和List等组件的使用方法,包括构造、样式设置、事件监听及常用方法。通过实例展示了如何创建按钮、文本框、下拉框并实现特定功能,如输入限制和对象绑定。同时,解释了各组件样式和方法的用途,如 SWT.READ_ONLY、setTextLimit、setData 和 getSelection 等。
摘要由CSDN通过智能技术生成

一个button类的实例

基本的步骤和之前的是一样的,一个SWT的项目,然后在页面设置面板里添加button。双击button(直接在source里面添加了监听器的代码)。下面是代码:

ExpandedBlockStart.gif

ContractedBlock.gifpublicclassButtonTest...{

ExpandedSubBlockStart.gif

ContractedSubBlock.gifpublicstaticvoidmain(String[] args)...{

6a9c071a08f1dae2d3e1c512000eef41.pngfinalDisplay display=Display.getDefault();

6a9c071a08f1dae2d3e1c512000eef41.pngfinalShell shell=newShell();

6a9c071a08f1dae2d3e1c512000eef41.png        shell.setSize(500,375);

6a9c071a08f1dae2d3e1c512000eef41.png        shell.setText("SWT Application");

6a9c071a08f1dae2d3e1c512000eef41.png        shell.open();

6a9c071a08f1dae2d3e1c512000eef41.png

6a9c071a08f1dae2d3e1c512000eef41.pngfinalButton okButton=newButton(shell, SWT.NONE);

6a9c071a08f1dae2d3e1c512000eef41.png//因为是比较简单的例子,只在这一个地方用这个的原因,所以用匿名方式ExpandedSubBlockStart.gif

ContractedSubBlock.gifokButton.addMouseListener(newMouseAdapter()...{

ExpandedSubBlockStart.gif

ContractedSubBlock.gifpublicvoidmouseUp(MouseEvent e)...{

6a9c071a08f1dae2d3e1c512000eef41.png                MessageDialog.openInformation(null,"","you click the"+okButton.getText()+"button");

ExpandedSubBlockEnd.gif            }ExpandedSubBlockEnd.gif        });

6a9c071a08f1dae2d3e1c512000eef41.png        okButton.setText("OK.");

6a9c071a08f1dae2d3e1c512000eef41.png        okButton.setBounds(97,196,244,54);

6a9c071a08f1dae2d3e1c512000eef41.png        shell.layout();

ExpandedSubBlockStart.gif

ContractedSubBlock.gifwhile(!shell.isDisposed())...{

6a9c071a08f1dae2d3e1c512000eef41.pngif(!display.readAndDispatch())

6a9c071a08f1dae2d3e1c512000eef41.png                display.sleep();

ExpandedSubBlockEnd.gif        }ExpandedSubBlockEnd.gif    }ExpandedBlockEnd.gif}

程序说明: Button类的构造方法是new Button(Composite parent,int style),它有2个参数:

第一个参数是指Button创建在哪个容器上,Composite是最常用的容器,而shell是Composite的子类,所以此参数也能接受shell和任何继承Composite的类。

第二个参数用来指定button应用那种(或几种)式样,SWT.NONE是保持Button组件的默认式样。式样其实是一个常量,如SWT.NONE的值是0。等等....

让button应用多是式样... 要生成一个文字靠左的,深陷型的复选框,只要用 “ | ” 将各个样式连起来即可,如下所示:new Button(shell,SWT.LEFT|SWT.BORDER|SWT.CHECK)。

组件的常用方法

SWT/JFace中的每一个组件之间都有很多同名的方法,很幸运这些方法在各个组件里的作用和用法都是相同或相似的,常用方法简述如下:

okButton.setImage(SWTResourceManager.getImage(ButtonTest.class, "275.jpg"));

okButton.setBackground(SWTResourceManager.getColor(158, 224, 167));

okButton.setText("OK.");

okButton.setBounds(81, 183, 244, 54);

等很多的方法,到现在的SWT编辑器下面这些常用的方法都可以在页面设置项里找到,所以很方便。

一个文本框(text类)的实例

本实例中创建一个文本框,它有如下功能:

只能输入数字。

至少输入一个值。

长度不能多于10个字符。    (实例代码如下:)

ExpandedBlockStart.gif

ContractedBlock.gifpublicclassButtonTest...{

6a9c071a08f1dae2d3e1c512000eef41.pngprivatestaticText text;

ExpandedSubBlockStart.gif

ContractedSubBlock.gifpublicstaticvoidmain(String[] args)...{

6a9c071a08f1dae2d3e1c512000eef41.pngfinalDisplay display=Display.getDefault();

6a9c071a08f1dae2d3e1c512000eef41.pngfinalShell shell=newShell();

6a9c071a08f1dae2d3e1c512000eef41.png        shell.setSize(500,375);

6a9c071a08f1dae2d3e1c512000eef41.png        shell.setText("SWT Application");

6a9c071a08f1dae2d3e1c512000eef41.png        shell.open();

6a9c071a08f1dae2d3e1c512000eef41.png

6a9c071a08f1dae2d3e1c512000eef41.pngfinalButton okButton=newButton(shell, SWT.NONE);

6a9c071a08f1dae2d3e1c512000eef41.png        okButton.setImage(SWTResourceManager.getImage(ButtonTest.class,"275.jpg"));

ExpandedSubBlockStart.gif

ContractedSubBlock.gif        okButton.addSelectionListener(newSelectionAdapter()...{

ExpandedSubBlockStart.gif

ContractedSubBlock.gifpublicvoidwidgetSelected(SelectionEvent e)...{

6a9c071a08f1dae2d3e1c512000eef41.png                String str=text.getText();

ExpandedSubBlockStart.gif

ContractedSubBlock.gifif(str==null||str.equals(""))...{

6a9c071a08f1dae2d3e1c512000eef41.png                    MessageDialog.openInformation(shell,"","please enter a number");

ExpandedSubBlockStart.gif

ContractedSubBlock.gif                }else...{

6a9c071a08f1dae2d3e1c512000eef41.png                    MessageDialog.openInformation(shell,"","your enter the number is true");

ExpandedSubBlockEnd.gif                }ExpandedSubBlockEnd.gif            }ExpandedSubBlockEnd.gif        });

6a9c071a08f1dae2d3e1c512000eef41.png        okButton.setText("OK.");

6a9c071a08f1dae2d3e1c512000eef41.png        okButton.setBounds(81,183,244,54);

6a9c071a08f1dae2d3e1c512000eef41.png

6a9c071a08f1dae2d3e1c512000eef41.png        text=newText(shell, SWT.BORDER);

6a9c071a08f1dae2d3e1c512000eef41.png        text.setTextLimit(10);//最都只能输入10个字符6a9c071a08f1dae2d3e1c512000eef41.pngtext.setBounds(70,85,228,46);

ExpandedSubBlockStart.gif

ContractedSubBlock.gif        text.addVerifyListener(newVerifyListener()...{//检查监听器,每输入一个字符都回触发ExpandedSubBlockStart.gif

ContractedSubBlock.gifpublicvoidverifyText(VerifyEvent e)...{

6a9c071a08f1dae2d3e1c512000eef41.png//检查输入字符(e.text)是否在0123456789这个字符串中,不在indexOf会返回-16a9c071a08f1dae2d3e1c512000eef41.pngbooleanb="0123456789".indexOf(e.text)>=0;

6a9c071a08f1dae2d3e1c512000eef41.png                e.doit=b;//doit属性如果为true,则字符允许输入,反之不允许ExpandedSubBlockEnd.gif}ExpandedSubBlockEnd.gif        });

6a9c071a08f1dae2d3e1c512000eef41.png

6a9c071a08f1dae2d3e1c512000eef41.png        shell.layout();

ExpandedSubBlockStart.gif

ContractedSubBlock.gifwhile(!shell.isDisposed())...{

6a9c071a08f1dae2d3e1c512000eef41.pngif(!display.readAndDispatch())

6a9c071a08f1dae2d3e1c512000eef41.png                display.sleep();

ExpandedSubBlockEnd.gif        }ExpandedSubBlockEnd.gif    }ExpandedBlockEnd.gif}

注意到这次MessageDialog的第一个参数用了shell做参数,而不是以前的null。两者的区别在于:用shell时,则弹出提示窗口时,windows任务栏不会新增一个任务项;用null时,则多出一个任务项。

常用方法:

setEchoChar(char echo)

说明:将输入到文本框的字符表示成echo字符.

例子:text.setEchoChar('*');相当于SWT.PASSWORD式样。

setTabs(int tabs)

说明:按Tab键时前进多少个空格的长度,默认值为8.只有当text的样式为SWT.MULTI,SWT.V_SCROLL,SWT.H_SCROLL时,此设置才会有效。

例子:text.setTabs(4) 按键时前进4个空格长度

setTopIndex(int index)

说明:转到文本框的第index行,0为第一行,此命令可以在文本框中进行快速行定位。例子:text.setTopIndex(0); 将当前行定位到首行上。

setTextLimit(int limit)

说明:设置最大入力数。

例子:text.setTextLimit(10); 文本框最多只能输入10个字符。

一个下拉框(combo类)的实例

ExpandedBlockStart.gif

ContractedBlock.gifpublicclassComboTest...{

6a9c071a08f1dae2d3e1c512000eef41.pngprivatestaticCombo combo;

ExpandedSubBlockStart.gif

ContractedSubBlock.gifpublicstaticvoidmain(String[] args)...{

6a9c071a08f1dae2d3e1c512000eef41.pngfinalDisplay display=Display.getDefault();

6a9c071a08f1dae2d3e1c512000eef41.pngfinalShell shell=newShell();

6a9c071a08f1dae2d3e1c512000eef41.png        shell.setSize(500,375);

6a9c071a08f1dae2d3e1c512000eef41.png        shell.setText("SWT Application");

6a9c071a08f1dae2d3e1c512000eef41.png        shell.open();

6a9c071a08f1dae2d3e1c512000eef41.png        combo=newCombo(shell, SWT.READ_ONLY);//定义一个只读的下拉框6a9c071a08f1dae2d3e1c512000eef41.pngcombo.setBounds(84,67,308,59);

6a9c071a08f1dae2d3e1c512000eef41.png

6a9c071a08f1dae2d3e1c512000eef41.pngfinalButton button=newButton(shell, SWT.NONE);

6a9c071a08f1dae2d3e1c512000eef41.png        button.setText("input key");

6a9c071a08f1dae2d3e1c512000eef41.png        button.setBounds(54,220,135,52);        

ExpandedSubBlockStart.gif

ContractedSubBlock.gif        button.addSelectionListener(newSelectionAdapter()...{

ExpandedSubBlockStart.gif

ContractedSubBlock.gifpublicvoidwidgetSelected(SelectionEvent e)...{

6a9c071a08f1dae2d3e1c512000eef41.png                combo.removeAll();//先清空combo6a9c071a08f1dae2d3e1c512000eef41.pngfor(inti=1;i<10;i++)

6a9c071a08f1dae2d3e1c512000eef41.png                    combo.add(i+"");//在combo中显示 1 到 96a9c071a08f1dae2d3e1c512000eef41.pngcombo.select(0);//设置 当前项是第一项ExpandedSubBlockEnd.gif}ExpandedSubBlockEnd.gif        });

6a9c071a08f1dae2d3e1c512000eef41.png

6a9c071a08f1dae2d3e1c512000eef41.pngfinalButton button_1=newButton(shell, SWT.NONE);

6a9c071a08f1dae2d3e1c512000eef41.png        button_1.setText("get key");

6a9c071a08f1dae2d3e1c512000eef41.png        button_1.setBounds(276,220,124,52);        

ExpandedSubBlockStart.gif

ContractedSubBlock.gif        button_1.addSelectionListener(newSelectionAdapter()...{

ExpandedSubBlockStart.gif

ContractedSubBlock.gifpublicvoidwidgetSelected(SelectionEvent e)...{

6a9c071a08f1dae2d3e1c512000eef41.png                MessageDialog.openInformation(shell,null, combo.getText());

ExpandedSubBlockEnd.gif            }ExpandedSubBlockEnd.gif        });

6a9c071a08f1dae2d3e1c512000eef41.png        shell.layout();

ExpandedSubBlockStart.gif

ContractedSubBlock.gifwhile(!shell.isDisposed())...{

6a9c071a08f1dae2d3e1c512000eef41.pngif(!display.readAndDispatch())

6a9c071a08f1dae2d3e1c512000eef41.png                display.sleep();

ExpandedSubBlockEnd.gif        }ExpandedSubBlockEnd.gif    }ExpandedBlockEnd.gif}None.gif

程序说明:

在这个例子中最关键的是设置值部分的代码,它是用add添加的,还有一种combo类设置值的方法,是将所有的值存到一个字符串数组,然后将这个数组作为参数,批量加入到combo中, 语句如下:

combo.setItems(new String[]{"1","2","3"});

让combo的各项和对象一一对应

combo中的项只能是字符串,但是在实际应用中有很多这样的需要:将combo中的各项和一些对象一一对应起来。这些对象可以是任何类,这时就可以根据选择各项来得到相应的对象。

要实现这样的功能,使用setData方法就可以了。基于上面的例子,只需要修改设值和取值的事件代码。代码如下:

None.gif... ...  

None.giffinalButton button=newButton(shell, SWT.NONE);

ExpandedBlockStart.gif

ContractedBlock.gif        button.addSelectionListener(newSelectionAdapter()...{

ExpandedSubBlockStart.gif

ContractedSubBlock.gifpublicvoidwidgetSelected(SelectionEvent e)...{

6a9c071a08f1dae2d3e1c512000eef41.png                combo.removeAll();

ExpandedSubBlockStart.gif

ContractedSubBlock.giffor(inti=1;i<10;i++)...{

6a9c071a08f1dae2d3e1c512000eef41.png                    combo.add(i+"no used");//这时的这行代码的作用就仅仅是在页面上显示的标签6a9c071a08f1dae2d3e1c512000eef41.pngcombo.setData(""+(i-1),newInteger(i));//关键代码,具体说明看下面的【代码说明】ExpandedSubBlockEnd.gif}6a9c071a08f1dae2d3e1c512000eef41.png                combo.select(0);

ExpandedSubBlockEnd.gif            }ExpandedBlockEnd.gif        });

None.gif        ... ...

None.giffinalButton button_1=newButton(shell, SWT.NONE);

ExpandedBlockStart.gif

ContractedBlock.gif        button_1.addSelectionListener(newSelectionAdapter()...{

ExpandedSubBlockStart.gif

ContractedSubBlock.gifpublicvoidwidgetSelected(SelectionEvent e)...{

6a9c071a08f1dae2d3e1c512000eef41.png                String key=""+combo.getSelectionIndex();//取得key6a9c071a08f1dae2d3e1c512000eef41.pngInteger selectObject=(Integer)combo.getData(key);//通过key 取得object               

6a9c071a08f1dae2d3e1c512000eef41.png//下面是显示结果6a9c071a08f1dae2d3e1c512000eef41.pngMessageDialog.openInformation(shell,null,"select the Integer is"+selectObject.toString());

ExpandedSubBlockEnd.gif            }ExpandedBlockEnd.gif        });

None.gif       ... ...

程序说明:

setData方法的格式是:setData(key,Object),它将对象(Object)对应一个键值(key),然后附着在combo上,键值可以是任意字符串,上例中之所以用""+(i-1),是因为它的值正好和combo的SelectionIndex值(当前项的序号)相同,这样在取值时就方便了。另外SelectionIndex的起始值是0,所以要 i -1 ( i 循环起始值是1) 。

combo中的样式 SWT.SIMPLE(无须单击下拉框,列表一直显示)

常用方法除了上面用到的,还有combo.deselectAll();就是让当前的选择项为空,就是不选择任何项。

select( int  index); 将index+1项设置为当前选择项。 combo.select(0);使首项为选择项。

setText(String string) 此方法和上面的一样也是设置当前选择项的。区别是上面是通过Index来选择的,这个是通过combo里面的String的内容来选择的,例如:  combo.setText("中国"); 就是把中国的那项选择为当前选择的,另外setText没有选择的背景阴影,select则有。

列表框(List类)

list的用法和combo基本一致,combo的两个例子只要将Text定义部分改为list的定义语句,再稍微修改,即可成为list类的例子。

在list中设值的方法和combo是一样的,但取值有所不同,这时因为list可以选择多项,而combo只能选择一项,combo'的getText可以得到当前选择项的字符串,而list由于可以选择多项,所以就不行了。

list取值用getSelection()方法,它返回的是一个所有选项组成的String数组。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值