java JFrame是否可以像JDialog一样工作?
我有一个简单的JDialog和JOptionPane,它工作得很好。但我想使用JFrame并创建更复杂的窗口,但我希望它充当JDialog,这意味着我需要在JFrame或JDialog打开时暂停代码,因为下一步取决于窗口中选择的内容
这是我的类,是否需要运行它以停止我的代码
public class MyFrame extends JDialog{
private static final long serialVersionUID = 1L;
public MyFrame() throws IOException {
setSize(600, 450);
dialogInit();
setTitle("Travel");
setVisible(true);
URL url = this.getClass().getResource("/images/travel1.png");
BufferedImage bf = ImageIO.read(url);
this.setContentPane(new backImage(bf));
JButton b = new JButton("Send");
JCheckBox tf=new JCheckBox("Country");
b.setBounds(318, 143, 98, 27);
tf.setBounds(235, 104, 150, 27);
add(b);
add(tf);
}
}
# 1 楼答案
在代码中,您仍然在制作一个JDialog,因为
MyFrame extends JDialog
,这可能是您想要的,而不是JFrame。除此之外,你需要做的是让它成为模态通过适当的constructor或使用setModal(true)创建对话框将阻止对程序其他窗口的输入,并在对话框关闭之前暂停调用方的执行
# 2 楼答案
您可以让机架手动运行事件泵,直到其关闭。见this question
# 3 楼答案
你想要的是你的对话是modal。使用^{} :
然后,当您从构建对话框的代码中调用
setVisible
时,代码将等待对话框关闭