SWT Button 事件例子
1、新建三个文件:
testForm001.java :窗体类
testEvent001.java :事件处理类
testBS001.java :业务处理类
2、该三个文件的内容分别为:
testForm001.java
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
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();
//
}
}
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
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
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));
}
}
}
}
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
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
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() + "]";
}
}
}
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() + "]";
}
}
}