0

我一直在寻找如何取消我的线程,找到了一些答案,但它没有帮助我。

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

   }

  });

 }
}
4

1 回答 1

0

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

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

于 2013-10-18T03:25:36.220 回答