当可见另一个JFrame时,模态对话框并不总是在未装饰的JFrame顶部 [英] Modal dialog not always on top of an undecorated JFrame when another JFrame is visible

查看:141
本文介绍了当可见另一个JFrame时,模态对话框并不总是在未装饰的JFrame顶部的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对模式对话框未经修饰的JFrame有一个奇怪的问题.

I have a weird problem with modal dialogs and undecorated JFrame.

如果我创建一个主装饰的JFrame,那么由于JOptionPane,我将显示一个模式对话框,一切正常.模态对话框始终位于顶部,我无法单击主要名声.

If I create a main undecorated JFrame then I display a modal dialog thanks to the JOptionPane, everything goes well. The modal dialog stays always on top and I can't click on the main fame.

但是,如果创建另一个JFrame(或另一个JDialog),模态对话框仍会阻止我与主机交互,但是现在,模态对话框并不总是位于顶部,而是在我位于主机下方时点击它.

But, if create another JFrame (or another JDialog), the modal dialog still prevent me to interact with the main frame, but now the modal dialog is not always on top and go underneath the main frame when I click on it.

不会发生此问题:

  • 如果主机架已装饰
  • 或者如果第二帧不可见

编辑

我在Linux Sus e上使用jdk1.7.0.0_09.但是与jre 1.6.0_32

I use jdk1.7.0.0_09 on Linux Suse.But I have the same result with jre 1.6.0_32

我用来测试的代码:

 public static void main(String[] args) {
    // creates main frame and set visible to true
    final JFrame mainFrame = new JFrame();
    mainFrame.setUndecorated(true); // if I comment this line, everything goes well
    mainFrame.add((new JPanel()).add(new JButton("OK")));
    mainFrame.setSize(new Dimension(500, 500));
    mainFrame.setVisible(true);
    // creates a dialog and set visible to true
    JFrame anotherFrame = new JFrame();
    anotherFrame.setVisible(true); // or if I comment this line, everything goes well too
    // display a modal dialog
    JOptionPane.showMessageDialog(mainFrame, "A message");
}

推荐答案

但是,如果创建另一个JFrame(或另一个JDialog),则模式对话框 仍然阻止我与主机交互,但是现在模态 对话框并不总是位于顶部,而是位于主框架下方 点击它.

But, if create another JFrame (or another JDialog), the modal dialog still prevent me to interact with the main frame, but now the modal dialog is not always on top and go underneath the main frame when I click on it.

  • 根本不是真的,直到JOptioPane都可见,这两种方式都是无法访问的

    • not true at all, both are not accesible untill JOptioPane is visible

      JOptionPane或JDialod.setModal(true)阻止从currnet JVM调用的alll窗口的鼠标或按键事件

      JOptionPane or JDialod.setModal(true) block mouse or key events to the alll windows invoked from currnet JVM

      您的问题中肯定还有其他不清楚的地方,其余的代码,次要的可能是Java版本和Native OS

      there must be something else that isn't clear from your question, rest of code, minor could be Java version and Native OS

      Java6(winxp)的代码,在Win7/Java7(x.x_011)上对我有效

      code for Java6 (winxp), works for me on Win7 / Java7(x.x_011)

      import java.awt.Dimension;
      import javax.swing.JButton;
      import javax.swing.JFrame;
      import javax.swing.JOptionPane;
      import javax.swing.JPanel;
      
      public class Main {
      
         private JFrame mainFrame = new JFrame();
         private JFrame anotherFrame = new JFrame();
      
          public Main() {
              mainFrame.setUndecorated(true);
              mainFrame.add((new JPanel()).add(new JButton("OK")));
              mainFrame.setSize(new Dimension(100, 60));
              mainFrame.setVisible(true);
              mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
              anotherFrame.setVisible(true);
              anotherFrame.setLocation(110, 0);
              anotherFrame.setSize(new Dimension(100, 60));
              anotherFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
              JOptionPane.showMessageDialog(mainFrame, "A message");
          }
      
      
          public static void main(String[] args) {
             java.awt.EventQueue.invokeLater(new Runnable() {
      
                  public void run() {
                      Main main = new Main();
                  }
              });
          }
      }  
      

      这篇关于当可见另一个JFrame时,模态对话框并不总是在未装饰的JFrame顶部的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆