如何配置没有按钮的 joptionpane 来处理()?
how to configure joptionpane with no button to dispose()?
我想在使用对话框初始化数据时显示一条消息。
但它不想关闭,我看不出这段代码有什么问题:
final JOptionPane optionPane = new JOptionPane("Information ", JOptionPane.INFORMATION_MESSAGE, JOptionPane.DEFAULT_OPTION, null, new Object[]{}, null);
JDialog dialog1 = optionPane.createDialog(null,"Wait for init");
dialog1.setAlwaysOnTop(true);
dialog1.setVisible(true);
dialog1.setModal(false);
dialog1.dispose();
谢谢
这个...
dialog1.setVisible(true);
dialog1.setModal(false);
错了。一旦对话框可见,它将等到关闭后再继续执行流程,这是模态对话框的巧妙副作用之一
我还怀疑您违反了 Swing 的单线程特性。
参见:
了解更多详情
可运行示例...
此示例显示一个对话框,等待 5 秒(在后台)然后关闭 window。一个巧妙的副作用是,window 不能被用户关闭
import java.awt.EventQueue;
import java.awt.GridBagLayout;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingWorker;
import javax.swing.border.EmptyBorder;
public class Main {
public static void main(String[] args) {
new Main();
}
public Main() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
JDialog dialog = new JDialog();
dialog.addWindowListener(new WindowAdapter() {
@Override
public void windowClosed(WindowEvent e) {
System.out.println("Closed");
}
});
dialog.add(new WaitPane());
dialog.setAlwaysOnTop(true);
dialog.setModal(true);
dialog.setDefaultCloseOperation(JDialog.DO_NOTHING_ON_CLOSE);
dialog.pack();
dialog.setLocationRelativeTo(null);
new SwingWorker<Void, Void>() {
@Override
protected Void doInBackground() throws Exception {
Thread.sleep(5000);
return null;
}
@Override
protected void done() {
System.out.println("Done");
dialog.dispose();
dialog.setVisible(false);
}
}.execute();
dialog.setVisible(true);
}
});
}
public class WaitPane extends JPanel {
public WaitPane() {
setBorder(new EmptyBorder(64, 64, 64, 64));
setLayout(new GridBagLayout());
add(new JLabel("Wait for init"));
}
}
}
我想在使用对话框初始化数据时显示一条消息。
但它不想关闭,我看不出这段代码有什么问题:
final JOptionPane optionPane = new JOptionPane("Information ", JOptionPane.INFORMATION_MESSAGE, JOptionPane.DEFAULT_OPTION, null, new Object[]{}, null);
JDialog dialog1 = optionPane.createDialog(null,"Wait for init");
dialog1.setAlwaysOnTop(true);
dialog1.setVisible(true);
dialog1.setModal(false);
dialog1.dispose();
谢谢
这个...
dialog1.setVisible(true);
dialog1.setModal(false);
错了。一旦对话框可见,它将等到关闭后再继续执行流程,这是模态对话框的巧妙副作用之一
我还怀疑您违反了 Swing 的单线程特性。
参见:
了解更多详情
可运行示例...
此示例显示一个对话框,等待 5 秒(在后台)然后关闭 window。一个巧妙的副作用是,window 不能被用户关闭
import java.awt.EventQueue;
import java.awt.GridBagLayout;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingWorker;
import javax.swing.border.EmptyBorder;
public class Main {
public static void main(String[] args) {
new Main();
}
public Main() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
JDialog dialog = new JDialog();
dialog.addWindowListener(new WindowAdapter() {
@Override
public void windowClosed(WindowEvent e) {
System.out.println("Closed");
}
});
dialog.add(new WaitPane());
dialog.setAlwaysOnTop(true);
dialog.setModal(true);
dialog.setDefaultCloseOperation(JDialog.DO_NOTHING_ON_CLOSE);
dialog.pack();
dialog.setLocationRelativeTo(null);
new SwingWorker<Void, Void>() {
@Override
protected Void doInBackground() throws Exception {
Thread.sleep(5000);
return null;
}
@Override
protected void done() {
System.out.println("Done");
dialog.dispose();
dialog.setVisible(false);
}
}.execute();
dialog.setVisible(true);
}
});
}
public class WaitPane extends JPanel {
public WaitPane() {
setBorder(new EmptyBorder(64, 64, 64, 64));
setLayout(new GridBagLayout());
add(new JLabel("Wait for init"));
}
}
}