SWT Button 事件例子

1、新建三个文件:

   testForm001.java :窗体类

   testEvent001.java :事件处理类

   testBS001.java :业务处理类

 

2、该三个文件的内容分别为:

   testForm001.java

  

ExpandedBlockStart.gif testForm001
package  com.wy;

import  org.eclipse.swt.SWT;
import  org.eclipse.swt.events.SelectionAdapter;
import  org.eclipse.swt.events.SelectionEvent;
import  org.eclipse.swt.layout.grouplayout.GroupLayout;
import  org.eclipse.swt.layout.grouplayout.LayoutStyle;
import  org.eclipse.swt.widgets.Button;
import  org.eclipse.swt.widgets.Display;
import  org.eclipse.swt.widgets.Label;
import  org.eclipse.swt.widgets.Shell;
import  org.eclipse.swt.widgets.Text;

public   class  testForm001 {

    
private  Text txtName;
    
private  Button btnMyPress;
    
protected  Shell shell;

    
/**
     * Launch the application
     * 
@param  args
     
*/
    
public   static   void  main(String[] args) {
        
try  {
            testForm001 window 
=   new  testForm001();
            window.open();
        } 
catch  (Exception e) {
            e.printStackTrace();
        }
    }

    
/**
     * Open the window
     
*/
    
public   void  open() {
        
final  Display display  =  Display.getDefault();
        createContents();
        
        
new  testEvent001( this );
        
        shell.open();
        shell.layout();
        
while  ( ! shell.isDisposed()) {
            
if  ( ! display.readAndDispatch())
                display.sleep();
        }
    }

    
public  Text gettxtName()
    {
        
return  txtName;
    }
    
    
public  Button getbtnMyPress()
    {
        
return   btnMyPress;
    }
    
    
/**
     * Create contents of the window
     
*/
    
protected   void  createContents() {
        shell 
=   new  Shell();
        shell.setText(
" SWT Application " );

        btnMyPress 
=   new  Button(shell, SWT.NONE);
        btnMyPress.setData(
" newKey " null );
        btnMyPress.setText(
" buttonbtnMyPressvalue " );
        
    

        Label lblMsg;
        lblMsg 
=   new  Label(shell, SWT.NONE);
        lblMsg.setText(
" Labelvalue " );

        txtName 
=   new  Text(shell, SWT.BORDER);
        txtName.setRedraw(
true );
        
final  GroupLayout groupLayout  =   new  GroupLayout(shell);
        groupLayout.setHorizontalGroup(
            groupLayout.createParallelGroup(GroupLayout.LEADING)
                .add(groupLayout.createSequentialGroup()
                    .add(groupLayout.createParallelGroup(GroupLayout.LEADING)
                        .add(groupLayout.createSequentialGroup()
                            .add(
35 35 35 )
                            .add(lblMsg, GroupLayout.PREFERRED_SIZE, 
111 , GroupLayout.PREFERRED_SIZE))
                        .add(groupLayout.createSequentialGroup()
                            .add(
78 78 78 )
                            .add(btnMyPress, GroupLayout.PREFERRED_SIZE, 
151 , GroupLayout.PREFERRED_SIZE))
                        .add(groupLayout.createSequentialGroup()
                            .add(
3 3 3 )
                            .add(txtName, GroupLayout.PREFERRED_SIZE, 
217 , GroupLayout.PREFERRED_SIZE)))
                    .addContainerGap(
263 , Short.MAX_VALUE))
        );
        groupLayout.setVerticalGroup(
            groupLayout.createParallelGroup(GroupLayout.LEADING)
                .add(groupLayout.createSequentialGroup()
                    .add(
45 45 45 )
                    .add(lblMsg, GroupLayout.PREFERRED_SIZE, 
44 , GroupLayout.PREFERRED_SIZE)
                    .add(
29 29 29 )
                    .add(btnMyPress, GroupLayout.PREFERRED_SIZE, 
61 , GroupLayout.PREFERRED_SIZE)
                    .add(
27 27 27 )
                    .add(txtName, GroupLayout.PREFERRED_SIZE, 
80 , GroupLayout.PREFERRED_SIZE)
                    .add(
55 55 55 ))
        );
        shell.setLayout(groupLayout);
        shell.pack();
        
//
    }
 
}


 

 

    testEvent001.java

   

ExpandedBlockStart.gif testEvent001
package  com.wy;

import  org.eclipse.swt.widgets. * ;
import  org.eclipse.swt.events. * ;

public   class  testEvent001 {
    
    
private  testBS001 testBS = null ;
    
private  Text txtName = null ;
    
private  Button btnMyPress = null ;
    
    
public  testEvent001(testForm001 frm)
    {
        txtName
= frm.gettxtName();
        btnMyPress
= frm.getbtnMyPress();
        
        testBS
= new  testBS001();
        
        ButtonEvent btnEvent 
=   new  ButtonEvent( this );
        btnMyPress.addSelectionListener(btnEvent);
    }
    
    
public  Text getTxtName()
    {
        
return   this .txtName;
    }
    
    
class  ButtonEvent  extends  SelectionAdapter
    {
        testEvent001 ev001
= null ;
        
public  ButtonEvent(testEvent001 evt)
        {
            ev001
= evt;
        }
        
        
public   void  widgetSelected(SelectionEvent e)
        {
            
if  (e.getSource() == btnMyPress)
            {
                
// txtName.setText(testBS.GetName(ev001.txtName.getText()));
                txtName.setText(testBS.GetName(ev001));
            }
        }
    }

}

 

 

 

    testBS001.java

   

ExpandedBlockStart.gif testBS001
package  com.wy;

public   class  testBS001 {
    
    
public  String GetName(String oldValue)
    {
        
return   " wangyong 2010.3.6  oldValue=[ "   +  oldValue  +   " ] " ;
    }
    
    
public  String GetName(testEvent001 e)
    {
        
if  ((e.getTxtName().getText() == null ||  (e.getTxtName().getText().trim().equals( "" )))
        {
            
return   " wangyong 2010.3.6  oldValue=[空值] " ;
        }
        
else
        {
            
return   " wangyong 2010.3.6  oldValue=[ "   +  e.getTxtName().getText()  +   " ] " ;        
        }
    }

}

 

 

转载于:https://www.cnblogs.com/yongwnet/archive/2010/03/06/1679921.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值