欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Java SwingWorkder使用实例

程序员文章站 2024-02-26 11:48:52
首先考虑的就是swing内部的 swingworkder对象,但是网上现成的例子不好找,主要参照了网上的资料,自己整理了一个。 注意:swingworkder对象只能执行...

首先考虑的就是swing内部的 swingworkder对象,但是网上现成的例子不好找,主要参照了网上的资料,自己整理了一个。

注意:swingworkder对象只能执行一次,不能重复执行!例如点击开始,结束后,就不能再次开始了。

复制代码 代码如下:

import java.awt.borderlayout;
import java.awt.event.*;
import java.beans.propertychangeevent;
import java.beans.propertychangelistener;
import java.util.list;
import java.util.concurrent.executionexception;
import javax.swing.*;
import javax.swing.swingworker;

public class swingworkersample {

    public static void main(string[] args) {

        // 外观定义
        try {
            uimanager
                    .setlookandfeel("com.sun.java.swing.plaf.nimbus.nimbuslookandfeel");
        } catch (exception ex) {
        }

        // 窗体
        jframe f = new jframe("swingworker sample");
        f.setdefaultcloseoperation(jframe.exit_on_close);

        // 表示区
        final jtextarea taoutput = new jtextarea(15, 30);
        jscrollpane sp = new jscrollpane(taoutput);
        f.add(sp);

        // 底部进度条
        final jprogressbar progressbar = new jprogressbar();
        f.add(borderlayout.south, progressbar);

        // 开始按钮
        jbutton b = new jbutton("开始");
        f.add(borderlayout.north, b);

        // 结束按钮
        jbutton b2 = new jbutton("结束");
        f.add(borderlayout.after_line_ends, b2);

        // swingworker类
        final swingworker<integer, int[]> sw = new swingworker<integer, int[]>() {

            /** 执行操作 */
            @override
            protected integer doinbackground() throws exception {

                int sum = 0;

                if (!this.iscancelled()) {

                    for (int i = 1; i <= 10; ++i) {
                        sum += i;
                        publish(new int[] { i, sum });// 调用表示中间结果
                        setprogress(i * 10);// 进度条
                        thread.sleep(1000);
                    }

                }
                return sum;
            }

            /** 表示中间结果 */
            @override
            protected void process(list<int[]> chunks) {
                stringbuilder sb = new stringbuilder();
                for (int[] values : chunks) {
                    sb.append(string.format("累加%d等于%d%n", values[0], values[1]));
                }
                taoutput.append(sb.tostring());
            }

            /** 处理完成 */
            @override
            protected void done() {

                if (this != null && !this.isdone()) {
                    try {
                        int result = get();
                        taoutput.append("完了。 合计" + result + "\n");
                    } catch (interruptedexception ex) {
                    } catch (executionexception ex) {
                    }
                }
            }

        };

        // 进度条表示
        sw.addpropertychangelistener(new propertychangelistener() {
            @override
            public void propertychange(propertychangeevent evt) {
                if ("progress".equals(evt.getpropertyname())) {
                    progressbar.setvalue((integer) evt.getnewvalue());
                }
            }
        });

        // 开始按钮事件
        b.addactionlistener(new actionlistener() {
            @override
            public void actionperformed(actionevent e) {

                sw.execute();// 异步调用swingworkder
            }
        });

        // 结束按钮
        b2.addactionlistener(new actionlistener() {
            @override
            public void actionperformed(actionevent e) {
                if (sw != null && !sw.isdone()) {
                    // cancel
                    sw.cancel(true);
                }

                joptionpane.showmessagedialog(null, "结束!");
            }
        });

        // 窗体表示
        f.pack();
        f.setvisible(true);
    }
}