【问题标题】:SwingWorker Cancel ActionSwingWorker 取消操作
【发布时间】:2018-07-17 19:13:12
【问题描述】:

我一直在寻找如何取消我的主题,找到了一些答案,但对我没有帮助。

我的应用程序需要从 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");

   }

  });

 }
}

【问题讨论】:

    标签: multithreading swingworker


    【解决方案1】:

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-02-09
      • 2010-11-03
      • 2011-12-26
      • 1970-01-01
      • 1970-01-01
      • 2011-09-11
      • 2011-09-01
      相关资源
      最近更新 更多