一般来说,是的.
我要做的是在你的doInBackground方法中使用SwingUtilities.invokeLater来显示对话框并在你的done方法中隐藏对话框.
这应该意味着即使对话框没有进入屏幕,您也可以获得对流量的更多控制……
小问题是你现在必须将对话框传递给工人,以便它可以控制它…
public class TestSwingWorkerDialog {
public static void main(String[] args) {
new TestSwingWorkerDialog();
}
private JDialog dialog;
public TestSwingWorkerDialog() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException ex) {
} catch (InstantiationException ex) {
} catch (IllegalAccessException ex) {
} catch (UnsupportedLookAndFeelException ex) {
}
MyWorker worker = new MyWorker();
worker.execute();
}
});
}
public class MyWorker extends SwingWorker {
@Override
protected Object doInBackground() throws Exception {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
getDialog().setVisible(true);
}
});
Thread.sleep(2000);
return null;
}
@Override
protected void done() {
System.out.println("now in done...");
JDialog dialog = getDialog();
// Don't care,dismiss the dialog
dialog.setVisible(false);
}
}
protected JDialog getDialog() {
if (dialog == null) {
dialog = new JDialog();
dialog.setModal(true);
dialog.setDefaultCloSEOperation(JDialog.DO_NOTHING_ON_CLOSE);
dialog.setLayout(new BorderLayout());
dialog.add(new JLabel("Please wait..."));
dialog.setSize(200,200);
dialog.setLocationRelativeTo(null);
}
return dialog;
}
}