我在我的 java swing 应用程序中添加了一个进度条。当我单击进度条关闭按钮时,它应该要求确认并在用户选择“是”时停止进度条后台任务,而不是终止进度条和后台任务,否则按原样启动进度条线程并执行后台任务。
谁能指导我解决这个问题?
任何意见表示赞赏。谢谢,拉胡尔
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.beans.*;
import java.util.Random;
public class ProgressBarDemo extends JPanel implements ActionListener,
PropertyChangeListener, Runnable {
/**
*
*/
private static final long serialVersionUID = 1L;
private JProgressBar progressBar;
private JButton startButton;
private JTextArea taskOutput;
private Task task=new Task();;
// private final Object lock = new Object();
class Task extends SwingWorker<Void, Void> implements Runnable {
/*
* Main task. Executed in background thread.
*/
@Override
public Void doInBackground() {
Random random = new Random();
int progress = 0;
// Initialize progress property.
setProgress(0);
while (progress < 100) {
// Sleep for up to one second.
try {
Thread.sleep(random.nextInt(1000));
} catch (InterruptedException ignore) {
}
// Make random progress.
progress += random.nextInt(10);
setProgress(Math.min(progress, 100));
}
return null;
}
/*
* Executed in event dispatching thread
*/
@Override
public void done() {
Toolkit.getDefaultToolkit().beep();
startButton.setEnabled(true);
setCursor(null); // turn off the wait cursor
taskOutput.append("Done!\n");
}
}
@Override
public void run() {
}
public ProgressBarDemo() {
super(new BorderLayout());
// Create the demo's UI.
startButton = new JButton("Start");
startButton.setActionCommand("start");
startButton.addActionListener(this);
progressBar = new JProgressBar(0, 100);
progressBar.setValue(0);
progressBar.setStringPainted(true);
taskOutput = new JTextArea(5, 20);
taskOutput.setMargin(new Insets(5, 5, 5, 5));
taskOutput.setEditable(false);
JPanel panel = new JPanel();
panel.add(startButton);
panel.add(progressBar);
add(panel, BorderLayout.PAGE_START);
add(new JScrollPane(taskOutput), BorderLayout.CENTER);
setBorder(BorderFactory.createEmptyBorder(20, 20, 20, 20));
}
/**
* Invoked when the user presses the start button.
*/
public void actionPerformed(ActionEvent evt) {
startButton.setEnabled(false);
setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
// Instances of javax.swing.SwingWorker are not reusuable, so
// we create new instances as needed.
//task = new Task();
task.addPropertyChangeListener(this);
task.execute();
}
/**
* Invoked when task's progress property changes.
*/
public void propertyChange(PropertyChangeEvent evt) {
if ("progress" == evt.getPropertyName()) {
int progress = (Integer) evt.getNewValue();
progressBar.setValue(progress);
taskOutput.append(String.format("Completed %d%% of task.\n",
task.getProgress()));
}
}
/**
* Create the GUI and show it. As with all GUI code, this must run on the
* event-dispatching thread.
*/
private void createAndShowGUI() {
// Create and set up the window.
JFrame frame = new JFrame("ProgressBarDemo");
frame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
// Create and set up the content pane.
JComponent newContentPane = new ProgressBarDemo();
newContentPane.setOpaque(true); // content panes must be opaque
frame.setContentPane(newContentPane);
// Display the window.
frame.pack();
frame.setVisible(true);
frame.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent we) {
String ObjButtons[] = { "Yes", "No" };
try {
synchronized (task) {
System.out.println(task.getState());
task.wait();
System.out.println(task.getState());
}
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
int PromptResult = JOptionPane.showOptionDialog(null,
"Are you sure you want to exit?",
"Confirmation...!!!",
JOptionPane.DEFAULT_OPTION,
JOptionPane.WARNING_MESSAGE, null, ObjButtons,
ObjButtons[1]);
if (PromptResult == 0) {
System.exit(0);
} else {
synchronized (task) {
task.notify();
}
}
}
});
}
public static void main(String[] args) {
// Schedule a job for the event-dispatching thread:
// creating and showing this application's GUI.
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
new ProgressBarDemo().createAndShowGUI();
}
});
}
}
我希望当我单击进度条的关闭按钮时,它应该提示我进行确认。当确认对话框打开时,在后台工作的 Swing 工作线程应该在 wait() 中。当用户选择 yes 时,它应该关闭进度条,或者如果用户选择 no 按钮,则启动 swing worker 的 wait() 线程并再次开始执行后台进程。
目前当我尝试过 task.wait(); 这一切都停止了主线程等待.....!!! 有什么建议么?