- 无需任何按钮即可创建自己的
Dialog
...确保将其设置defaultCloseOperation
为DO_NOTHING
- 用一个
ProgressMonitor
进度监视器示例...
import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import javax.swing.JFrame;
import javax.swing.ProgressMonitor;
import javax.swing.SwingWorker;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
public class TestProgress {
public static void main(String[] args) {
new TestProgress();
}
public TestProgress() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
}
new BackgroundWorker().execute();
}
});
}
public class BackgroundWorker extends SwingWorker<Void, Void> {
private ProgressMonitor monitor;
public BackgroundWorker() {
addPropertyChangeListener(new PropertyChangeListener() {
@Override
public void propertyChange(PropertyChangeEvent evt) {
if ("progress".equalsIgnoreCase(evt.getPropertyName())) {
if (monitor == null) {
monitor = new ProgressMonitor(null, "Processing", null, 0, 99);
}
monitor.setProgress(getProgress());
}
}
});
}
@Override
protected void done() {
if (monitor != null) {
monitor.close();
}
}
@Override
protected Void doInBackground() throws Exception {
for (int index = 0; index < 100; index++) {
setProgress(index);
Thread.sleep(125);
}
return null;
}
}
}
对话框示例
import java.awt.EventQueue;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JProgressBar;
import javax.swing.SwingWorker;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
public class TestProgress {
public static void main(String[] args) {
new TestProgress();
}
public TestProgress() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
}
new BackgroundWorker().execute();
}
});
}
public class BackgroundWorker extends SwingWorker<Void, Void> {
private JProgressBar pb;
private JDialog dialog;
public BackgroundWorker() {
addPropertyChangeListener(new PropertyChangeListener() {
@Override
public void propertyChange(PropertyChangeEvent evt) {
if ("progress".equalsIgnoreCase(evt.getPropertyName())) {
if (dialog == null) {
dialog = new JDialog();
dialog.setTitle("Processing");
dialog.setLayout(new GridBagLayout());
dialog.setDefaultCloseOperation(JDialog.DO_NOTHING_ON_CLOSE);
GridBagConstraints gbc = new GridBagConstraints();
gbc.insets = new Insets(2, 2, 2, 2);
gbc.weightx = 1;
gbc.gridy = 0;
dialog.add(new JLabel("Processing..."), gbc);
pb = new JProgressBar();
gbc.gridy = 1;
dialog.add(pb, gbc);
dialog.pack();
dialog.setLocationRelativeTo(null);
dialog.setVisible(true);
}
pb.setValue(getProgress());
}
}
});
}
@Override
protected void done() {
if (dialog != null) {
dialog.dispose();
}
}
@Override
protected Void doInBackground() throws Exception {
for (int index = 0; index < 100; index++) {
setProgress(index);
Thread.sleep(125);
}
return null;
}
}
}