程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> JAVA編程 >> 關於JAVA >> Java SwingWorkder應用實例

Java SwingWorkder應用實例

編輯:關於JAVA

Java SwingWorkder應用實例。本站提示廣大學習愛好者:(Java SwingWorkder應用實例)文章只能為提供參考,不一定能成為您想要的結果。以下是Java 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);
    }
}

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved