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

  •  一个button类的实例 

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

public   class  ButtonTest  {
    
public static void main(String[] args) {
        
final Display display = Display.getDefault();
        
final Shell shell = new Shell();
        shell.setSize(
500375);
        shell.setText(
"SWT Application");
        shell.open();

        
final Button okButton = new Button(shell, SWT.NONE);
        
//因为是比较简单的例子,只在这一个地方用这个的原因,所以用匿名方式
        okButton.addMouseListener(new MouseAdapter() {
            
public void mouseUp(MouseEvent e) {
                MessageDialog.openInformation(
null,"","you click the "+ okButton.getText() +"button");
            }

        }
);
        okButton.setText(
"OK.");
        okButton.setBounds(
9719624454);
        shell.layout();
        
while (!shell.isDisposed()) {
            
if (!display.readAndDispatch())
                display.sleep();
        }

    }

}

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

  1. 第一个参数是指Button创建在哪个容器上,Composite是最常用的容器,而shell是Composite的子类,所以此参数也能接受shell和任何继承Composite的类。
  2. 第二个参数用来指定button应用那种(或几种)式样,SWT.NONE是保持Button组件的默认式样。式样其实是一个常量,如SWT.NONE的值是0。等等....
  3. 让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类)的实例

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

  1. 只能输入数字。
  2. 至少输入一个值。
  3. 长度不能多于10个字符。    (实例代码如下:)
    public   class  ButtonTest  {
        
    private static Text text;
        
    public static void main(String[] args) {
            
    final Display display = Display.getDefault();
            
    final Shell shell = new Shell();
            shell.setSize(
    500375);
            shell.setText(
    "SWT Application");
            shell.open();

            
    final Button okButton = new Button(shell, SWT.NONE);
            okButton.setImage(SWTResourceManager.getImage(ButtonTest.
    class"275.jpg"));
            okButton.addSelectionListener(
    new SelectionAdapter() {
                
    public void widgetSelected(SelectionEvent e) {
                    String str 
    = text.getText();
                    
    if (str == null || str.equals("")) {
                        MessageDialog.openInformation(shell, 
    """please enter a number");
                    }
     else {
                        MessageDialog.openInformation(shell, 
    """your enter the number is true");
                    }

                }

            }
    );
            okButton.setText(
    "OK.");
            okButton.setBounds(
    8118324454);

            text 
    = new Text(shell, SWT.BORDER);
            text.setTextLimit(
    10);  //最都只能输入10个字符
            text.setBounds(708522846);
            text.addVerifyListener(
    new VerifyListener(){//检查监听器,每输入一个字符都回触发
                public void verifyText(VerifyEvent e) {
                    
    //检查输入字符(e.text)是否在0123456789这个字符串中,不在indexOf会返回-1
                    boolean b = "0123456789".indexOf(e.text) >= 0 ;
                    e.doit 
    = b;  //doit属性如果为true,则字符允许输入,反之不允许
                }

            }
    );

            shell.layout();
            
    while (!shell.isDisposed()) {
                
    if (!display.readAndDispatch())
                    display.sleep();
            }

        }

    }

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

常用方法:

  1. setEchoChar(char echo)
    说明:将输入到文本框的字符表示成echo字符.
    例子:text.setEchoChar('*');相当于SWT.PASSWORD式样。
  2. setTabs(int tabs)
    说明:按Tab键时前进多少个空格的长度,默认值为8.只有当text的样式为SWT.MULTI,SWT.V_SCROLL,SWT.H_SCROLL时,此设置才会有效。
    例子:text.setTabs(4) 按键时前进4个空格长度
  3.  setTopIndex(int index)
    说明:转到文本框的第index行,0为第一行,此命令可以在文本框中进行快速行定位。

    例子:text.setTopIndex(0); 将当前行定位到首行上。
     
  4. setTextLimit(int limit)
    说明:设置最大入力数。
    例子:text.setTextLimit(10); 文本框最多只能输入10个字符。
     

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

 

public   class  ComboTest  {
    
private static Combo combo;
    
public static void main(String[] args) {
        
final Display display = Display.getDefault();
        
final Shell shell = new Shell();
        shell.setSize(
500375);
        shell.setText(
"SWT Application");
        shell.open();
        combo 
= new Combo(shell, SWT.READ_ONLY);//定义一个只读的下拉框
        combo.setBounds(846730859);

        
final Button button = new Button(shell, SWT.NONE);
        button.setText(
"input key");
        button.setBounds(
5422013552);        
        button.addSelectionListener(
new SelectionAdapter() {
            
public void widgetSelected(SelectionEvent e) {
                combo.removeAll(); 
//先清空combo
                for(int i=1;i<10;i++)
                    combo.add(i
+"");//在combo中显示 1 到 9 
                combo.select(0);    //设置 当前项是第一项
            }

        }
);

        
final Button button_1 = new Button(shell, SWT.NONE);
        button_1.setText(
"get key");
        button_1.setBounds(
27622012452);        
        button_1.addSelectionListener(
new SelectionAdapter() {
            
public void widgetSelected(SelectionEvent e) {
                MessageDialog.openInformation(shell, 
null, combo.getText());
            }

        }
);
        shell.layout();
        
while (!shell.isDisposed()) {
            
if (!display.readAndDispatch())
                display.sleep();
        }

    }

}

 程序说明:
在这个例子中最关键的是设置值部分的代码,它是用add添加的,还有一种combo类设置值的方法,是将所有的值存到一个字符串数组,然后将这个数组作为参数,批量加入到combo中, 语句如下:
combo.setItems(new String[]{"1","2","3"});

让combo的各项和对象一一对应
        combo中的项只能是字符串,但是在实际应用中有很多这样的需要:将combo中的各项和一些对象一一对应起来。这些对象可以是任何类,这时就可以根据选择各项来得到相应的对象。
        要实现这样的功能,使用setData方法就可以了。基于上面的例子,只需要修改设值和取值的事件代码。代码如下:

      ... ...  
      
final  Button button  =   new  Button(shell, SWT.NONE);
        button.addSelectionListener(
new  SelectionAdapter()  {
            
public void widgetSelected(SelectionEvent e) {
                combo.removeAll();
                
for(int i=1;i<10;i++){
                    combo.add(i
+"no used");//这时的这行代码的作用就仅仅是在页面上显示的标签                    
                    combo.setData(""+(i-1), new Integer(i)); //关键代码,具体说明看下面的【代码说明】                    
                }

                combo.select(
0);
            }

        }
);
        ... ...
        
final  Button button_1  =   new  Button(shell, SWT.NONE);
        button_1.addSelectionListener(
new  SelectionAdapter()  {
            
public void widgetSelected(SelectionEvent e) {
                String key 
= "" + combo.getSelectionIndex(); //取得key
                Integer selectObject = (Integer)combo.getData(key);//通过key 取得object               
                
//下面是显示结果
                MessageDialog.openInformation(shell, null"select the Integer is "+ selectObject.toString());
            }

        }
);
       ... ...

 程序说明:
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数组。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值