SwingWorker取消操作

时间:2013-10-16 18:55:38

标签: multithreading swingworker

我正在寻找如何取消我的帖子,找到了一些答案,但这对我没有帮助。

我的应用程序需要从0-X开始,然后按取消按钮取消线程并使用最后一个计数编号更新Atual Label值。  再说一遍:我发现了一些例子和说明并没有帮助我,所以请不要发布另一个帖子示例。

import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.SwingUtilities;



public class SwingWorker extends JFrame {

 Thread worker=new Thread();

 boolean parar=false;

 int em;

 public int quantos;

 public int sleep;

 private JLabel contador = new JLabel("0");

 private JLabel statusLabel = new JLabel("N\u00E3o completado");

 private JButton startButton = new JButton("Iniciar");

 private JLabel atual = new JLabel("0");

 JButton btnNewButton = new JButton("Cancelar");

 JLabel lblltimoNmero = new JLabel("\u00DAltimo n\u00FAmero");



 public SwingWorker(String title) {

  super(title);
  setTitle("Contador do n\u00FAmero!");
  getContentPane().setLayout(null);
  contador.setBounds(106, 97, 48, 37);

  contador.setFont(new Font("serif", Font.BOLD, 28));

  getContentPane().add(contador);
  statusLabel.setBounds(76, 155, 139, 14);

  getContentPane().add(statusLabel);
  startButton.setBounds(35, 210, 79, 23);

  getContentPane().add(startButton);

  //Button cancelar
  btnNewButton.setBounds(118, 210, 100, 23);
  getContentPane().add(btnNewButton);
  btnNewButton.setEnabled(false);
  btnNewButton.addActionListener(new ActionListener() {

    @Override
    public void actionPerformed(ActionEvent e) {
        parar=true;
        atual.setText(""+(em-1));

        //----------------------------------------
        //  Worker cancel action
        //worker.cancel();


        //-------------------------
    }
});

  lblltimoNmero.setBounds(10, 288, 110, 14);
  getContentPane().add(lblltimoNmero);

  atual.setFont(new Font("Serif", Font.BOLD, 28));
  atual.setBounds(106, 269, 59, 37);
  getContentPane().add(atual);

  JButton btnQuantosNmeros = new JButton("Ir at\u00E9");
  btnQuantosNmeros.setBounds(35, 244, 79, 23);
  getContentPane().add(btnQuantosNmeros);

  JButton btnSleepPor = new JButton("Sleep por");
  btnSleepPor.setBounds(118, 244, 89, 23);
  getContentPane().add(btnSleepPor);
  btnSleepPor.addActionListener(new ActionListener() {

    @Override
    public void actionPerformed(ActionEvent arg0) {
        sleep =Integer.parseInt(JOptionPane.showInputDialog("Thread dormir por(ms): "));
        startButton.setEnabled(true);

    }
});

  btnQuantosNmeros.addActionListener(new ActionListener() {


    public void actionPerformed(ActionEvent arg0) {
        quantos =Integer.parseInt(JOptionPane.showInputDialog("Ir até o número :"));

    }
});

  startButton.addActionListener(new ActionListener() {

   public void actionPerformed(ActionEvent arg0) {
       btnNewButton.setEnabled(true);
    start();
    startButton.setEnabled(false);

   }

  });

  setSize(244, 400);

  setDefaultCloseOperation(EXIT_ON_CLOSE);

  setVisible(true);

  JOptionPane.showMessageDialog(null, "Clique em -Ir até- e em -Sleep por-");
 }



 private void start() {

        worker = new Thread() {
           public void run() {  
               if(quantos!=0){
            for(int i=0; i<=quantos; i++) {

             final int count = i;
             em++;
             SwingUtilities.invokeLater(new Runnable() {
              public void run() {
               contador.setText(Integer.toString(count));

              }
             });

             try {
              Thread.sleep(sleep);
             } catch (InterruptedException e) {

             }
            }}

            SwingUtilities.invokeLater(new Runnable() {
             public void run() {
              statusLabel.setText("Completo");
              startButton.setEnabled(true);
               atual.setText(Integer.toString( em-1 ));
               em=0;
               parar=false;
             }
            });

           }
          };

          worker.start();
 }

 public static void main(String[] args) {

  SwingUtilities.invokeLater(new Runnable() {

   @Override

   public void run() {

    new SwingWorker("SwingWorker");

   }

  });

 }
}

1 个答案:

答案 0 :(得分:0)

使用SwingWorker的正确方法是创建它的子类并覆盖其doInBackground方法。请参阅教程中的示例:https://docs.oracle.com/javase/tutorial/uiswing/concurrency/simple.html,以及取消SwingWorkerhttps://docs.oracle.com/javase/tutorial/uiswing/concurrency/cancel.html的教程。

您已经创建了JFrame的子类,并将其命名为SwingWorker,因此我不太了解您尝试做什么。您应该可以找到一些关于继承如何在Java中工作的教程,例如http://docs.oracle.com/javase/tutorial/java/IandI/index.html