2
我目前正在學習Java。我正在創建一個從用戶獲取信息的項目,然後創建sql文件。在那個(冗長的)過程中,我使用任務和線程顯示了一個顯示進度的面板。但是,我發現如果我在開始時等待10秒,代碼將按預期執行,但如果沒有睡眠,程序會立即退出。任何信息非常感謝!線程和任務問題
package GUI;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JFrame;
public class GUIMainPanel implements ActionListener
{
private static JFrame frame;
public GUIMainPanel()
{
// Code that creates a panel, getting information from the user
}
public static void main(String[] args)
{
GUIMainPanel gui = new GUIMainPanel();
}
public void actionPerformed(ActionEvent e)
{
if (e.getActionCommand() == "Continue")
{
Runnable r1 = new Runnable()
{
public void run()
{
// Progress Monitor frame
ProgressMonitor pbd = new ProgressMonitor();
}
};
Runnable r2 = new Runnable()
{
public void run()
{
// Code that creates files and changes the progress
}
};
Thread thread1 = new Thread(r1);
Thread thread2 = new Thread(r2);
thread1.start();
thread2.start();
}
}
}
package GUI;
import java.awt.*;
import javax.swing.*;
import javax.swing.border.EmptyBorder;
import Queries.CreateListsQueries;
import java.beans.*;
public class ProgressMonitor implements PropertyChangeListener
{
private static JProgressBar progressBar; // Progress bar showing the progress of the query creation process
private static JLabel creatingQueriesLabel; // Label showing information about the current state of the query
// creation process
private Task task; // Task used to update the progress bar and label based on the current state of the query
// creation process
private static JFrame frame;
/**
* Constructor
*/
public ProgressMonitor()
{
// Code creating the Progress Monitor Panel and displaying it
// Instances of javax.swing.SwingWorker are not reusuable, so
// we create new instances as needed.
task = new Task();
task.addPropertyChangeListener(this);
task.execute();
}
class Task extends SwingWorker<Void, Void>
{
/*
* Main task. Executed in background thread.
*/
@Override
public Void doInBackground()
{
int progress = 0;
double tempProgress = 0;
String currentQueryText = "Test";
try
{
// Infamous 10 seconds wait here
Thread.sleep(10000);
}
catch (InterruptedException ignore)
{
ignore.printStackTrace();
}
// Initialize progress property.
setProgress(0);
while (progress < 100)
{
progress = GUIMainPanel.getProgress();
setProgress(progress);
}
return null;
}
/*
* Executed in event dispatching thread
*/
@Override
public void done()
{
Toolkit.getDefaultToolkit().beep();
frame.setCursor(null); // turn off the wait cursor
System.exit(0);
}
}
/**
* Invoked when task's progress property changes.
*/
public void propertyChange(PropertyChangeEvent evt)
{
if ("progress".equals(evt.getPropertyName()))
{
int progress = (Integer) evt.getNewValue();
progressBar.setValue(progress);
}
if ("currentQueryText".equals(evt.getPropertyName()))
{
String text = (String) evt.getNewValue();
creatingQueriesLabel.setText(text);
}
}
}
那麼你有'System.exit(0)'在你的代碼中間。這被稱爲? – markspace
謝謝你的回答!它在任務完成時被調用(或應該被調用),所以當進度大於100時。 –
老實說你的代碼很複雜,我認爲我們需要一個編譯和演示問題的例子。 [見這裏。](http://stackoverflow.com/help/mcve)我認爲任何人都不會僅僅通過閱讀代碼就能找到它。 – markspace