Java SWT线程入门实例

线程初入门
特地敲了一遍代码,传上来备忘

前台界面:

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.DisposeEvent;
import org.eclipse.swt.events.DisposeListener;
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;

public class TaskGUI {

    private Task task =new Task(this);
    private Text taskCountText;
    private Button startButton;
    private Button stopButton;
    private ProgressBar progressBar;
    private Text consoleText;

    public static void main(String[] args){
        new TaskGUI().open();
    }

    public void open(){
        final Display display = Display.getDefault();
        final Shell shell = new Shell();
        shell.setSize(300,300);
        shell.setLayout(new GridLayout());
        Group group = new Group(shell,SWT.NONE);
        group.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
        group.setLayout(new GridLayout(4,false));   //跨四格,不等分

        setContent(group);

        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();
        shell.addDisposeListener(new DisposeListener(){
            public void widgetDisposed(DisposeEvent e){
                task.stop();
            }
        });
        while(!shell.isDisposed()){
            if(!display.readAndDispatch())
                display.sleep();
        }
        display.dispose();
    }

    private void setContent(Group group) {
        new Label(group,SWT.BORDER);
        taskCountText = new Text(group,SWT.BORDER);
        taskCountText.setText("100");
        taskCountText.setLayoutData(new GridData(100,-1));
        taskCountText.addVerifyListener(new VerifyListener(){
            public void verifyText(VerifyEvent e){
                e.doit = "0123456789".indexOf(e.text) >= 0;
            }
        });

        startButton = new Button(group,SWT.PUSH);
        startButton.setText("执行");
        startButton.addSelectionListener(new SelectionAdapter(){
            public void widgetSelected(SelectionEvent e){
                setButtonState(false);
                final int taskCount = Integer.valueOf(taskCountText.getText());
                progressBar.setMaximum(taskCount-1);
                consoleText.insert("后来处理线程开始启动......\n");
                new Thread(){
                    public void run(){
                        task.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();
            }
        });
    }

    public void setButtonState(boolean flag){
        if(!startButton.isDisposed())
            startButton.setEnabled(flag);
        if(!stopButton.isDisposed())
            stopButton.setEnabled(!flag);
    }

    public void moveProgress(int progress){
        if(!progressBar.isDisposed())
            progressBar.setSelection(progress);
    }

    public void consolePrintln(String str){
        if(!consoleText.isDisposed())
            consoleText.insert(str);
    }
}

后台任务:

package taskGUI;

import org.eclipse.swt.widgets.Display;

public class Task {
    private TaskGUI taskGUI;
    private boolean stopFlag;

    public Task(TaskGUI taskGUI) {
        this.taskGUI = taskGUI;
    }

    public void stop(){
        stopFlag = true;
    }

    public void start(int taskCount){
        stopFlag = false;
        log("后台线程开始执行任务......\n");
        for(int i=0 ; i<taskCount ; i++){
            if(stopFlag)
                break;
            try{
                Thread.sleep(100);
            }catch(InterruptedException e){
                log(e.getMessage());
            }
            log("任务"+(i+1)+"处理完毕\n");
            notifyOneTaskFinish(i);
        }
        log("后台线程结束执行任务!!!!!\n");
        notifyAllTasksFinish();
    }

    private void log(final String str){
        Display.getDefault().syncExec(new Runnable(){
            public void run(){
                taskGUI.consolePrintln(str);
            }
        });
    }

    private void notifyOneTaskFinish(final int progress){
    //后台访问前台界面类时要用下面这个方法包装,没有对SWT组建操作的方法就能被后台直接访问
        Display.getDefault().syncExec(new Runnable(){
            public void run(){
                taskGUI.moveProgress(progress);
            }
        });
    }

    public void notifyAllTasksFinish(){
        Display.getDefault().syncExec(new Runnable(){
            public void run(){
                taskGUI.setButtonState(true);
            }
        });
    }
}

这里写图片描述

重新按执行后,任务会重新开始执行,以后补充继续执行的功能

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值