combo

Combo 下拉列表框

public class Combo

extends Composite

Instances of this class are controls that allow the user to choose an item from a list of items, or optionally enter a new value by typing it into an editable text field. Often, Combos are used in the same place where a single selection List widget could be used but space is limited. A Combo takes less space than a List widget and shows similar information.

combo.removeAll();

         for(int i=0; i<10; i++){

         combo.add("第"+i+"个");

         }

combo.select(0);

MessageDialog.openInformation(shell, null, combo.getText());

1、取消Combo的全部下拉列表项  combo.removeAll()

2、添加Combo的下拉列表项 add(String),setItems(String[])

3、使Combo默认选中第一个 combo.select(0)

4、得到Combo选中的值 用combo.getText()方法。

public class Combo1 {

    public static void main(String[] args) {

        final Display display = Display.getDefault();

        final Shell shell = new Shell();

        shell.setSize(327, 253);

        shell.setText("SWT Application");

        //------------------新插入的界面核心代码------------------------

 

        final Combo combo = new Combo(shell, SWT.READ_ONLY); //定义一个只读的下拉框

        combo.setBounds(16, 11, 100, 25);

        //设值按钮

        final Button button1 = new Button(shell, SWT.NONE);

        button1.setBounds(17, 65, 100, 25);

        button1.setText("设值");

        button1.addSelectionListener(new SelectionAdapter() {

            public void widgetSelected(SelectionEvent e) {

                combo.removeAll(); //先清空combo,以防"设值"按钮多次按下时出BUG

                for (int i = 1; i <= 10; i++)

                    //循环,赋值

                    combo.add("第" + i + "个字符串"); //在combo中显示的字符串

                combo.select(0); //设置第一项为当前项

            }

        });

 

        //取值按钮

        final Button button2 = new Button(shell, SWT.NONE);

        button2.setBounds(136, 66, 100, 25);

        button2.setText("取值");

        button2.addSelectionListener(new SelectionAdapter() {

            public void widgetSelected(SelectionEvent e) {

                // combo.getText()得到combo中当前显示的字符串

                MessageDialog.openInformation(shell, null, combo.getText());

            }

        });

 

        //------------------END---------------------------------------------

        shell.layout();

        shell.open();

        while (!shell.isDisposed()) {

            if (!display.readAndDispatch())

                display.sleep();

        }

    }

}

 

 

 

  shell.setLayout(new FillLayout()); //FillLayout对象应用于shell

FillLayout() 填满整个屏幕。

 

 

 

 

 

转载于:https://www.cnblogs.com/the-wang/p/6841051.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值