SWT Button 事件例子

1、新建三个文件:

   testForm001.java :窗体类

   testEvent001.java :事件处理类

   testBS001.java :业务处理类

 

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

   testForm001.java

  

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(
353535)
                            .add(lblMsg, GroupLayout.PREFERRED_SIZE, 
111, GroupLayout.PREFERRED_SIZE))
                        .add(groupLayout.createSequentialGroup()
                            .add(
787878)
                            .add(btnMyPress, GroupLayout.PREFERRED_SIZE, 
151, GroupLayout.PREFERRED_SIZE))
                        .add(groupLayout.createSequentialGroup()
                            .add(
333)
                            .add(txtName, GroupLayout.PREFERRED_SIZE, 
217, GroupLayout.PREFERRED_SIZE)))
                    .addContainerGap(
263, Short.MAX_VALUE))
        );
        groupLayout.setVerticalGroup(
            groupLayout.createParallelGroup(GroupLayout.LEADING)
                .add(groupLayout.createSequentialGroup()
                    .add(
454545)
                    .add(lblMsg, GroupLayout.PREFERRED_SIZE, 
44, GroupLayout.PREFERRED_SIZE)
                    .add(
292929)
                    .add(btnMyPress, GroupLayout.PREFERRED_SIZE, 
61, GroupLayout.PREFERRED_SIZE)
                    .add(
272727)
                    .add(txtName, GroupLayout.PREFERRED_SIZE, 
80, GroupLayout.PREFERRED_SIZE)
                    .add(
555555))
        );
        shell.setLayout(groupLayout);
        shell.pack();
        
//
    }
 
}


 

 

    testEvent001.java

   

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

   

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() + "]";        
        }
    }

}

 

 

posted @ 2010-03-06 21:26  yongwnet  阅读(1415)  评论(0编辑  收藏  举报