我在java swing应用程序中添加了一个进度条。当我点击进度条关闭按钮时,它应该要求确认并在用户选择是时停止进度条后台任务而不是终止进度条和后台任务,否则按原样启动进度条线程并执行后台任务。 / p>
任何人都可以指导我解决这个问题吗?
任何评论都表示赞赏。 谢谢, 拉胡尔
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()中。当用户选择是,它应该关闭进度条,或者如果用户选择否按钮,则启动swing工作者的wait()线程,然后再次开始执行后台进程。
目前我已经尝试过task.wait();这一切都停止主线程等待..... !!! 有什么建议?
答案 0 :(得分:1)
我假设您使用带有进度条的计时器,因为您没有发布代码,请发布SSCCE。
无论如何,在 actionPerfomed 方法中,您必须有条件语句检查所选选项是或否:
在任何事情停止计时之前,用户选择它,然后:
if(Yes):停止计时器并将进度值设置为零和 重置您在计时器的 actionListener中使用的所有变量。
如果(否):启动计时器。
注意:如果您尝试了任何操作,我将不会发布代码。