2013-10-16 48 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是複雜的,奇怪的和難以合作的。

此外,如果人們試圖幫助你,請不要公開拒絕它,好像你在做什麼。只是,我不知道,真的很糟糕。沒有人需要幫助你。