java swt 多线程_SWT中多线程(Thread)控制进度(ProgressBar)条示例 | 学步园

/*

* @(#)TestSWTThreadTaskConsole.java 1.0 09/03/26

*/

package cn.edu.tsinghua.cs.keg.andy.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.graphics.Point;

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;

/**

* 前台GUI界面,用于显示进度条以及文本内容

* @author Andy Wang

* @since JDK5.0

* @version 1.0 09/03/26

*

*/

public class TestSWTThreadTaskUI {

private ProgressBar progressBar;

private Text consoleText;

private Text numText;

private Button startBtn;

private Button stopBtn;

private TestSWTThreadTaskConsole console = new TestSWTThreadTaskConsole(this);

public TestSWTThreadTaskUI(){}

/**

* 创建GUI界面

*/

public void createTaskUI(){

Display display = Display.getDefault();

Shell shell = new Shell(display,SWT.MIN|SWT.MAX|SWT.CLOSE);

shell.setText("测试多线程使用的前台界面");

Point point = new Point(300,300);

shell.setSize(point);

shell.setLayout(new GridLayout());

//任务栏所在组

{

Group group = new Group(shell,SWT.NONE);

group.setLayout(new GridLayout(4,false));

group.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));

new Label(group,SWT.NONE).setText("任务数:");

numText = new Text(group,SWT.BORDER);

numText.setText("100");

numText.setLayoutData(new GridData(100,-1));

numText.addVerifyListener(new VerifyListener(){

public void verifyText(VerifyEvent e) {

e.doit = "0123456789".indexOf(e.text) >= 0;

//     if(!e.doit){

//      MessageDialog.openInformation(null, "提示信息", "请输入数字!!");

//     }

}

});

startBtn = new Button(group,SWT.PUSH);

startBtn.setText("执行");

startBtn.addSelectionListener(new SelectionAdapter(){

public void widgetSelected(SelectionEvent e){

setButtonIsEnabled(false);

String num = numText.getText();

final int taskCount = new Integer(num).intValue();

progressBar.setMaximum(taskCount-1);

consoleText.insert("后台程序开始启动..../n");

new Thread(){

public void run(){

//       调用后台程序,并将参数传给后台

console.startConsole(taskCount);

}

}.start();

consoleText.insert("后台程序运行结束!/n");

}

});

stopBtn = new Button(group,SWT.PUSH);

stopBtn.setText("停止");

stopBtn.setEnabled(false);

stopBtn.addSelectionListener(new SelectionAdapter(){

public void widgetSelected(SelectionEvent e){

setButtonIsEnabled(true);

console.stopConsole();

}

});

}

//  进度条

{

progressBar = new ProgressBar(shell,SWT.BORDER);

//   pro.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));

progressBar.setLayoutData(new GridData(286,-1));

}

//输出信息的文本框

{

consoleText = new Text(shell,SWT.BORDER|SWT.WRAP|SWT.READ_ONLY|SWT.MULTI|SWT.V_SCROLL);

final GridData gri = new GridData();

//   final GridData gri = new GridData(GridData.FILL_HORIZONTAL);

gri.heightHint = 180;

gri.widthHint = 263;

consoleText.setLayoutData(gri);

}

shell.open();

while(!shell.isDisposed()){

if(!display.readAndDispatch()){

display.sleep();

}

}

display.dispose();

}

/**

* get console text

* @return

*/

public Text getConsoleText(){

return consoleText;

}

/**

* get a progress bar

* @return

*/

public ProgressBar getProgressBar(){

return progressBar;

}

public void setButtonIsEnabled(boolean flag){

startBtn.setEnabled(flag);

stopBtn.setEnabled(!flag);

}

/**

* start main class

* @param args

*/

public static void main(String[] args) {

try{

TestSWTThreadTaskUI tui = new TestSWTThreadTaskUI();

tui.createTaskUI();

}catch(Exception e){}

}

}

/*

* @(#)TestSWTThreadTaskConsole.java 1.0 09/03/27

*/

package cn.edu.tsinghua.cs.keg.andy.swt;

import org.eclipse.swt.widgets.Display;

/**

* 后台控制类,用于对TestSWTThreadTaskUI类的控制

* @author Andy Wang

* @since JDK5.0

* @version 1.0 09/03/27

*

*/

public class TestSWTThreadTaskConsole {

private TestSWTThreadTaskUI ui ;

private boolean stopFlag = false;

public TestSWTThreadTaskConsole(){}

public TestSWTThreadTaskConsole(TestSWTThreadTaskUI gui){

this.ui = gui;

}

/**

* start console

* @param numText task numbers

*/

public void startConsole(int numText){

stopFlag = false;

this.insertConsoleText("后台线程开始启动.../n");

for(int i = 0;i < numText;i++){

if(stopFlag) break;

try{

Thread.sleep(100);

}catch(Exception e){

e.printStackTrace();

}

this.insertConsoleText("第[ "+ i +" ]个任务处理完毕!/n");

this.moveProgressBar(i);

}

this.insertConsoleText("后台线程运行结束!!!!");

this.setButtonIsEnabled(true);

}

/**

* text insert

* @param content a string

*/

public void insertConsoleText(final String content){

Display.getDefault().syncExec(new Runnable(){

public void run(){

ui.getConsoleText().insert(content);

}

});

}

/**

* move progress bar

* @param progress

*/

public void moveProgressBar(final int progress){

Display.getDefault().syncExec(new Runnable(){

public void run(){

ui.getProgressBar().setSelection(progress);

}

});

}

/**

* set button state

* @param flag

*/

private final void setButtonIsEnabled(final boolean flag){

Display.getDefault().syncExec(new Runnable(){

public void run(){

ui.setButtonIsEnabled(flag);

}

});

}

/**

* stop console threads

*/

public void stopConsole(){

stopFlag = true;

}

}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值