⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 taskgui.java

📁 Eclipse从入门到精通源代码/第二篇 SWT_JFace篇(6-16章)
💻 JAVA
字号:
/**
 * @作者:陈刚
 * @Email:glchengang@yeah.net
 * @Blog:http://blog.csdn.net/glchengang
 */
package swt.thread;

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;

public class TaskGUI {
    private Display display = Display.getDefault();

    private Shell shell = new Shell();

    private Task task = new Task(this); //Task为后台处理类

    //将界面组件设为类的实例变量
    private Text taskCountText; //任务数文本框

    private Button startButton;//“执行”按钮

    private Button stopButton;//“停止”按钮

    private ProgressBar progressBar;//进度条

    private Text consoleText;//输出调试信息的文本框

    public static void main(String[] args) {
        try {
            TaskGUI window = new TaskGUI();
            window.open();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void open() {
        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));
        //任务数文本框
        new Label(group, SWT.NONE).setText("任务数:");
        taskCountText = new Text(group, SWT.BORDER);
        taskCountText.setText("100");//默认任务数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
                String str = taskCountText.getText();
                final int taskCount = new Integer(str).intValue();
                //设置进度条的格数
                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();//后台任务停止
            }
        });
        //进度条
        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();
        }
    }

    /**
     * 使用“执行”“停止”两按钮的“置灰/使能”状态
     */
    public void setButtonState(boolean bFlag) {
        startButton.setEnabled(bFlag);
        stopButton.setEnabled(!bFlag);
    }

    /**
     * 为后台类取界面组件的几个get方法
     */
    public Shell getShell() {
        return shell;
    }

    public Text getConsoleText() {
        return consoleText;
    }

    public ProgressBar getProgressBar() {
        return progressBar;
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -