JDialog.setAlwaysOnTop(true)在Windows下将所有对话框置于最前面 [英] JDialog.setAlwaysOnTop(true) brings all dialogs to the front under Windows

查看:190
本文介绍了JDialog.setAlwaysOnTop(true)在Windows下将所有对话框置于最前面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建两个对话框:
DialogA:setVisible(true)仅调用一次.
DialogB:setVisible(true)setAlwaysOnTop(true)每1.5秒调用一次

I create two dialogs:
DialogA: setVisible(true) called only once.
DialogB: setVisible(true) and setAlwaysOnTop(true) called every 1,5 sec

Windows:每次对dialogB.setAlwaysOnTop(true)的调用都将dialogA AND dialogB置于最前面.
OSX:每次对dialogB.setAlwaysOnTop(true)的调用都只将dialogB置于最前面. (预期的行为)

Windows: Each call to dialogB.setAlwaysOnTop(true) brings dialogA AND dialogB to the front.
OSX: Each call to dialogB.setAlwaysOnTop(true) brings only dialogB to the front. (Expected Behaviour)

测试用例(Windows):

1我从IDE中启动应用程序.
2我看到了DialogA.
3我在IDE中单击,然后DialogA消失.
4一秒钟后,将显示DialogA和DialogB.
5我在IDE中单击,然后DialogA和DialogB消失.转到4

Test Case (Windows):

1 I start the application from my IDE.
2 I see DialogA.
3 I click in the IDE and DialogA disappears.
4 After one second DialogA and DialogB will show up.
5 I click in the IDE and DialogA and DialogB disappears. GOTO 4

预期的行为(OSX):
4.一秒钟后,将显示DialogB.
5.我在IDE中单击,然后DialogB消失.转到4

Expected Behaviour(OSX):
4. After one second DialogB will show up.
5. I click in the IDE and DialogB disappears. GOTO 4

问题:
如何在Windows下获得预期的行为?

Question:
How do I get the expected behaviour under Windows?

import javax.swing.JDialog;
import javax.swing.JLabel;
public class JDialogSetAlwaysonTopTEST
{
public static void main(String[] p_Strings)
{
    final JDialog dialogA = new JDialog();
    dialogA.setLocation(0, 0);
    dialogA.add(new JLabel("DialogA: Click on the overlapped   window"));       
    java.awt.EventQueue.invokeLater(new Runnable() {           
        public void run() {
            dialogA.pack();
            dialogA.setVisible(true);
        }
    });

    try {Thread.sleep(3000);} catch (InterruptedException e){}

    final JDialog dialogB = new JDialog();
    dialogB.setLocation(70, 70);
    dialogB.add(new JLabel("DialogB:  Do you see DialogA?"));

    java.awt.EventQueue.invokeLater(new Runnable() {           
        public void run() {
            dialogB.pack();
            dialogB.setVisible(true);
        }
    });

    while(true)
    {
        java.awt.EventQueue.invokeLater(new Runnable()  {                  
            public void run() {
                dialogB.setAlwaysOnTop(true);  //prerequisite
                dialogB.setVisible(true);
                dialogB.setAlwaysOnTop(false); //prerequisite
            }
        });         
        try {Thread.sleep(1500);} catch (InterruptedException e){}
    }       
}
}

推荐答案

我找到了解决问题的肮脏"方法.

I found a "dirty" solution to my problem.

final JDialog dialogA = new JDialog(new JFrame());
...
final JDialog dialogB = new JDialog(new JFrame());

如果每个对话框都有一个独立的所有者dialogB.setAlwaysOnTop(true),则dialogB.setVisible(true)不会影响dialogA

If each dialog has an independent owner dialogB.setAlwaysOnTop(true), dialogB.setVisible(true) does not effect dialogA

这篇关于JDialog.setAlwaysOnTop(true)在Windows下将所有对话框置于最前面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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