【问题标题】:not to return to Dialog when another Dialog is open当另一个对话框打开时不返回对话框
【发布时间】:2014-05-15 09:36:10
【问题描述】:

我正在显示来自 JFrame 的对话框,但是当我在对话框外部单击时,对话框会被隐藏。除非你关闭它,否则对话框应该不会让你什么都不做?

这是我的代码:

从第一个对话框调用的对话框:

JProductStocking jps = JProductStocking.getProductStoking(JPanelTicket.this, oApp);
jps.setVisible(true);

这就是名为的 JDIalog:

public class JProductStocking extends javax.swing.JDialog implements BeanFactoryApp{
public JProductStocking(Component parent, boolean modal) {
        //super(parent, modal);
        initComponents();

    }

public static JProductStocking getProductStoking(Component parent, AppView app) {
        Window window = getWindow(parent);

        JProductStocking myMsg;
        if (window instanceof JFrame) { 
            myMsg = new JProductStocking((Frame) window, true);
        } else {
            myMsg = new JProductStocking((Dialog) window, true);
        }
        myMsg.init(app, parent);
        myMsg.applyComponentOrientation(parent.getComponentOrientation());
        return myMsg;
    }

     private static Window getWindow(Component parent) {
        if (parent == null) {
            return new JFrame();
        } else if (parent instanceof JFrame || parent instanceof Dialog) {
            return (Window) parent;
        } else {
            return getWindow(parent.getParent());
        }
    }

    public void init(AppView app, Component parent) {
        oApp = app;
       // m_dlSales = (DataLogicSales) app.getBean("com.openbravo.pos.forms.DataLogicSales");
        initComponents();
        ProductList = new ArrayList();
        this.setResizable(false);
        setLocationRelativeTo(parent);

    }
}

我没有很好地调用 jDialog 吗?还是我做错了什么?

【问题讨论】:

  • 您没有将任何父 Window 传递给 JDialog’s 构造函数,因此它不知道要阻止哪个父。当您在构造函数中添加//super(parent, modal); 注释时,它应该会引起您的注意……
  • @Holger 我不知道我怎么能忘记这一点,谢谢!像答案一样发布它,我会接受它。
  • @Holger 如果我没记错的话,提供父母只会告诉对话框保持在上面。它不会阻止用户与父级的任何可见部分进行交互。为此,对话框必须是模态的。
  • @schmop:是的,但问题还是一样:没有调用所需的构造函数。父母是否与阻塞相关取决于模态类型。
  • @user3480792 使用 JFrame 的局部变量,只创建一个 JDialog,将此对象重用于另一个操作,

标签: java swing parent jdialog modality


【解决方案1】:

您正在寻找的行为称为“模态”对话框。您必须将“true”传递给对话框构造函数:

public JProductStocking() {
        super((Frame)null, true); //better to pass an actual Frame, Window or Dialog object as a parent
        initComponents();

    }

【讨论】:

    【解决方案2】:

    您没有将父 Windowmodal 标志传递给 JDialog 的构造函数,因此使用了无模式的默认行为。请注意,除此之外,您的代码没有必要复杂。

    从 Java 6 开始,您可以将 Window 传递给 Dialog 的构造函数,并且允许为 null,因此它是故障安全的。结合现有方法SwingUtilities.windowForComponent,整个代码可能如下:

    public class JProductStocking extends javax.swing.JDialog
      implements BeanFactoryApp {
    
      public JProductStocking(Component parent, Dialog.ModalityType modality) {
        super(SwingUtilities.windowForComponent(parent), modality);
        initComponents();
      }
    // …
    

    请注意,使用 Java 6 Dialog.ModalityType,您可以配置 Dialog 以阻止除此对话框的子项 (APPLICATION_MODAL) 之外的所有其他应用程序窗口,或仅阻止对话框的父项及其子项 (DOCUMENT_MODAL )。这比简单的modal 标志提供了更多的控制。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-12-30
      • 2015-12-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-11-10
      • 1970-01-01
      • 2021-05-30
      相关资源
      最近更新 更多