【问题标题】:JFrame to behave as JDialog?JFrame 表现得像 JDialog?
【发布时间】:2013-06-14 20:07:00
【问题描述】:

我有一个带有 JOptionPane 的简单 JDialog,它运行良好。但是我想使用 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);
}
}

【问题讨论】:

    标签: java swing jframe jdialog modality


    【解决方案1】:

    你想要的是你的对话是modal。使用Dialog.setModal(boolean)

    public MyFrame() throws IOException {
        setModal(true);
    

    然后当您从构造它的代码中调用对话框上的setVisible 时,代码将等待对话框关闭。

    【讨论】:

      【解决方案2】:

      您可以让框架手动运行事件泵,直到它关闭。见this question

      【讨论】:

      • 他已经在扩展JDialog,只是还没有把它变成模态。
      • 啊,好点子。我以为他出于某种原因明确地试图避免使用 JDialog。
      【解决方案3】:

      在您的代码中,自 MyFrame extends JDialog 以来,您仍在制作 JDialog,这可能是您想要的,而不是 JFrame。您还需要做的是使其成为模态的。

      通过适当的constructor 或使用setModal(true) 创建对话框将阻止输入到程序的其他窗口并在调用者中暂停执行,直到对话框关闭。

      【讨论】:

        猜你喜欢
        • 2015-06-30
        • 1970-01-01
        • 2013-05-21
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-08-10
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多