因此,在本節的代碼中,我基本上要告訴GUI禁用按鈕,並在沒有線程運行時彈出一個彈出窗口(例如,調用的方法has完成)。在Java GUI中使用線程數
public void actionPerformed(ActionEvent event)
{
String command = event.getActionCommand();
//If btnConvertDocuments is clicked, the FileConverter method is called and the button is then disabled [so as to prevent duplicates].
if (command.equals("w"))
{
new Thread(new Runnable()
{
public void run()
{
FileConverter fc = new FileConverter();
}
}).start();
if (Thread.activeCount() == 0)
{
btnConvertDocuments.setEnabled(false);
//Validation message ensuring completion of the step.
JOptionPane.showMessageDialog(this, "Step 1 Complete!", "Validation", JOptionPane.INFORMATION_MESSAGE);
}
}
爲什麼if (Thread.activeCount() == 0)
似乎永遠不會被調用?這不是我想要完成我的目標嗎?提前感謝您的任何意見!
作爲替代直接使用線程,你可能想看看['ExecutorService'(HTTP://下載。 oracle.com/javase/6/docs/api/java/util/concurrent/ExecutorService.html)和['Future'](http://download.oracle.com/javase/6/docs/api/java/ UTIL /併發/ Future.html)。 –