swt入门 --常用组件的使用

 swt的常用组件button ,text ,combo,list ,还有一些容器类composite ,group,这里选择几个,列写简单的用法
不写解释了,因为代码很简单,而且代码上的注释足以说明.
1,combo  和text
package  com.test;

import  org.eclipse.swt.SWT;
import  org.eclipse.swt.events.SelectionAdapter;
import  org.eclipse.swt.events.SelectionEvent;
import  org.eclipse.swt.widgets.Combo;
import  org.eclipse.swt.widgets.Display;
import  org.eclipse.swt.widgets.List;
import  org.eclipse.swt.widgets.Shell;
import  org.eclipse.swt.widgets.Text;

public   class  CTShow  {

    
private static List list;
    
private static Combo combo;
    
private static Text text;
    
public static void main(String[] args) {
        
final Display display = Display.getDefault();
        
final Shell shell = new Shell();
        shell.setSize(
478120);
        shell.setText(
"combo and text");
        shell.open();
        
//构造text
        text = new Text(shell, SWT.BORDER);
        text.setBounds(
1603428120);
        
//构造combo
        combo = new Combo(shell, SWT.NONE);
        combo.setItems(
new String[] {"one""two""three""four"});
        combo.setData(
"one""1");
        combo.setData(
"two""2");
        combo.setData(
"three""3");
        combo.setData(
"four""4");
        combo.setBounds(
283410220);
        
//添加监听器
        combo.addSelectionListener(new SelectionAdapter(){
            
public void widgetSelected(SelectionEvent e){
                String selText 
= combo.getText();
                String selValue 
= (String)combo.getData(selText);
                text.setText(
"text: " + selText + " value: " + selValue);
            }
}
);
        
        shell.layout();
        
//消息循环
        while (!shell.isDisposed()) {
            
if (!display.readAndDispatch())
                display.sleep();
        }

    }


}

2,list、 button 、messagebox
package  com.test;

import  org.eclipse.jface.dialogs.MessageDialog;
import  org.eclipse.swt.SWT;
import  org.eclipse.swt.events.SelectionAdapter;
import  org.eclipse.swt.events.SelectionEvent;
import  org.eclipse.swt.widgets.Button;
import  org.eclipse.swt.widgets.Display;
import  org.eclipse.swt.widgets.List;
import  org.eclipse.swt.widgets.Shell;

public   class  LBMShow  {

    
private static List list;
    
/**
     * Launch the application
     * 
@param args
     
*/

    
public static void main(String[] args) {
        
final Display display = Display.getDefault();
        
final Shell shell = new Shell();
        shell.setSize(
192217);
        shell.setText(
"list and button and messagebox");
        
//

        shell.open();
        
//构造list
        list = new List(shell, SWT.BORDER|SWT.MULTI|SWT.V_SCROLL);
        list.setItems(
new String[] {"语文""数学""英语""物理""化学"});
        list.setData(
"化学""HX");
        list.setData(
"物理""WL");
        list.setData(
"语文""YW");
        list.setData(
"数学""SX");
        list.setData(
"英语""YY");
        list.setBounds(
292510075);
        
//构造button
        final Button button = new Button(shell, SWT.NONE);
        button.setText(
"提示");
        button.setBounds(
481215822);
        
//添加监听器
        button.addSelectionListener(new SelectionAdapter(){
            
public void widgetSelected(SelectionEvent e){
                
for (String str : list.getSelection()) {
                    
//messagebox提示
                    MessageDialog.openInformation(nullnull, str);
                }

            }

        }
);
        
        shell.layout();
        
//消息循环
        while (!shell.isDisposed()) {
            
if (!display.readAndDispatch())
                display.sleep();
        }

    }


}

3,group组
package  com.test;

import  org.eclipse.swt.SWT;
import  org.eclipse.swt.widgets.Button;
import  org.eclipse.swt.widgets.Display;
import  org.eclipse.swt.widgets.Group;
import  org.eclipse.swt.widgets.Shell;
import  org.eclipse.swt.widgets.Text;

public   class  GroupShow  {
    
private static Text text;
    
public static void main(String[] args) {
        
final Display display = Display.getDefault();
        Shell shell 
= new Shell();
        shell.setText(
"group");
        shell.open();
        
//构造group
        final Group group = new Group(shell, SWT.NONE);
        group.setText(
"   组  ");
        group.setBounds(
4128143100);
        
//把按钮放在group中,注意new Button(group这里
        final Button button = new Button(group, SWT.NONE);
        button.setText(
"按钮");
        button.setBounds(
42594822);
        
//构造text
        text = new Text(group, SWT.BORDER);
        text.setBounds(
32288025);
        
//
        shell.layout();
        
while(!shell.isDisposed()){
            
if (!display.readAndDispatch()){
                display.sleep();
            }

        }

    }

}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值