package org.eclipse.wb.swt;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.VerifyEvent;
import org.eclipse.swt.events.VerifyListener;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Group;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.ProgressBar;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;
import wwww.InfoqUtil;
public class GUITest01 {
private Display display = Display.getDefault();
private Shell shell = new Shell();
private GUITest task = new GUITest(this); //task为后台处理类
//将界面组件设为类的实例变量
private Text taskText; //创建任务数文本框
private Button openButton; //创建“执行”按钮
private Button stopButton; //创建“停止”按钮
private ProgressBar progressBar; //创建进度条
private Text consoleText; //创建输出调试信息的文本框
private InfoqUtil infoqTask = new InfoqUtil(this);
public static void main(String[] args) { //main方法
try {
GUITest01 window = new GUITest01(); //创建窗口
window.open(); //打开窗口
} catch (Exception e) {
e.printStackTrace();
}
}
public void open(){
shell.setSize(330,330); //设置窗口大小
shell.setLayout(new GridLayout()); //设置窗口布局
shell.setText("SWT线程示例"); //设置窗口标题
Group group = new Group(shell, SWT.NONE); //设置任务数文本框和按钮的组
group.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
group.setLayout(new GridLayout(4,false));
new Label(group, SWT.NONE).setText("任务数:"); //设置任务数文本框
taskText = new Text(group, SWT.BORDER);
taskText.setText("20"); //默认任务数
taskText.setLayoutData(new GridData(100,-1));
taskText.addVerifyListener(new VerifyListener() {
public void verifyText(VerifyEvent e) { //只能输入数值
e.doit = "0123456789".indexOf(e.text) >=0;
}
});
openButton = new Button(group, SWT.PUSH); //创建执行按钮
openButton.setText("执行");
//对按钮添加事件监听
openButton.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent e) {
setButtonState(false); //设置两按钮的状态
String str = taskText.getText(); //得到任务数
final int taskCount = new Integer(str).intValue();
progressBar.setMaximum(taskCount-1); //设置进度条的格数
consoleText.insert("后台处理线程开始启动......\n");
new Thread(){ //为后台任务创建一个新的线程
public void run(){
//task.start(taskCount);
infoqTask.start(taskCount);
}
}.start();
consoleText.insert("后台处理线程启动结束。\n");
}
});
stopButton = new Button(group, SWT.PUSH); //创建停止按钮
stopButton.setText("停止");
stopButton.setEnabled(false); //设置按钮是否可用
//对按钮添加事件监听
stopButton.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent e){
task.stop(); //后台任务停止
infoqTask.stop();
}
});
progressBar = new ProgressBar(shell, SWT.NONE); //创建进度条
progressBar.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
//输出调试信息的文本框
consoleText = new Text(shell, SWT.MULTI|SWT.BORDER|SWT.V_SCROLL);
consoleText.setLayoutData(new GridData(GridData.FILL_BOTH));
//------------------------------------------------------
shell.layout();
shell.open();
while(!shell.isDisposed()){
if(!display.readAndDispatch()){
display.sleep();
}
}
display.dispose();
}
public void setButtonState(boolean bFlag){
openButton.setEnabled(bFlag);
stopButton.setEnabled(!bFlag);
}
//以下为后台类取界面组件的几个get方法
public Shell getShell(){
return shell;
}
public Text getConsoleText(){
return consoleText;
}
public ProgressBar getProgressBar(){
return progressBar;
}
}