2013-10-16 70 views
0

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

我的应用程序需要从0-X开始,然后按取消按钮取消线程,并使用上次计数编号更新Atual标签值。 再次:我找到了一些例子和解释,并没有帮助我,所以请不要发布另一个帖子的例子。

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"); 

    } 

    }); 

} 
} 

回答

0

请不要把这当成一个命中,但我不知道这个线程正在发生什么。潘打算!

它看起来像你试图使用SwingWorker而不扩展它,这不是如何面向对象的继承工作。启动与Java教程:

http://docs.oracle.com/javase/tutorial/java/index.html

而具体的接口和的传承:

http://docs.oracle.com/javase/tutorial/java/IandI/index.html

如果你是新来的Java编程或一般的,不SwingWorker的开始! SwingWorker是复杂的,奇怪的和难以合作的。

此外,如果人们试图帮助你,请不要公开拒绝它,好像你在做什么。只是,我不知道,真的很糟糕。没有人需要帮助你。