框架始终只在我的程序之上 [英] Frame always on top of my program only

查看:113
本文介绍了框架始终只在我的程序之上的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在未修饰的alwaysOnTop框架中创建一种工具栏。因此,我希望我的框架位于我的主框架之上,但不希望我的框架位于其他程序的框架之上。我试过这段代码:

I'm trying to create a kind of toolbar in an undecorated alwaysOnTop frame. Thus, I want my frame to be on top of my main frame, but not on top of frames from other programs. I tried this code :

public class Test {
    private static JFrame mainFrame;

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                mainFrame = new JFrame("test");
                mainFrame.setSize(800,600);
                mainFrame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
                mainFrame.setVisible(true);

                A a = new A();
            }
        });
    }

    public static class A extends JDialog {

        public A() {
            super(mainFrame);
            setAlwaysOnTop(true);
            setFocusable(false);
            setSize(80,60);
            setVisible(true);
        }
    }
}

但是尽管使用了JDialog并且对所有者进行了精确化,框架保持在其他应用程序之上(至少使用Ubuntu。也许结果与其他操作系统不同?)

But despite the use of JDialog and precising the owner, the frame stay on top of other applications (at least with Ubuntu. Maybe the result is different with other OS ?)

EDIT
好​​的,我在对话框中尝试了这个代码:

EDIT : Ok, I tried this code for my dialog :

public static class A extends JDialog {

    public A(String name) {
        super(mainFrame, name);
        setAlwaysOnTop(true);
        setFocusable(false);
        setSize(80, 60);
        setVisible(true);

        mainFrame.addWindowListener(new WindowAdapter() {
            @Override
            public void windowActivated(WindowEvent e) {
                A.this.setAlwaysOnTop(true);
            }

            @Override
            public void windowDeactivated(WindowEvent e) {
                // A.this.setAlwaysOnTop(false);
                A.this.toBack();
            }
        });
    }
}

现在的问题是当主窗口松散焦点时,对话窃取焦点,我不明白为什么。例如,我运行我的应用程序,我尝试切换到Firefox,Firefox出现并覆盖mainFrame,但A对话框获得焦点并保持在屏幕上。现在,如果我再次选择Firefox,对话框将最终正确消失。你能解释一下为什么对话会成为焦点吗?

The issue now is that when the main window loose focus, the dialog steals the focus back and I don't understand why. For instance, I run my app, I try to switch to Firefox, Firefox appears and covers the mainFrame, but the A dialog gets the focus and stays in the screen. Now, if I select Firefox again, the dialog will at last correctly disappear. Could you explain me why the dialog gets the focus?

谢谢

推荐答案

<好吧,我找到了一个解决方案(不知道它是否是解决方案,但是它正在工作,所以......)

Ok, I found a solution (don't know if it is THE solution, but it's working, so...)

我发现了setFocusableWindowState(),非常适合工具栏。顺便说一句,我不知道我之前的setFocusable(false)是否有任何影响。

I discovered setFocusableWindowState(), that is perfect for toolbars. By the way, I don't know if my previous setFocusable(false) had any effect.

下一个问题是焦点在这段代码中得到了非常奇怪的行为:如果我从MyApp切换到Firefox,会发生以下情况:

The next issue was that the focus gets very weird behaviour with this code : If I switch from MyApp to Firefox, here is what happens :

focus : MyApp -> Firefox
execution of MyDialog.toFront()
focus : Firefox -> MyDialog
MyDialog not focusable !
focus : MyDialog -> MyApp !!!

结果:没有变化!

所以我终于得到了一些技巧:在MyDialog.toFront()之后,你将焦点交还给前一个所有者。我发现没有错误的唯一方法是:mainFrame.toBack()

So I finally got the tricks : just after MyDialog.toFront(), you give back the focus to the previous owner. And the only way I found to do this with no error was : mainFrame.toBack()

最终代码:

public class Test {
    private static JFrame mainFrame;

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                mainFrame = new JFrame("test");
                mainFrame.setSize(800,600);
                mainFrame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
                mainFrame.setVisible(true);

                A a = new A();
            }
        });
    }

    public static class A extends JDialog {

        public A() {
            super(mainFrame);
            setAlwaysOnTop(true);
            setFocusableWindowState(false);
            setSize(80,60);
            setVisible(true);

            mainFrame.addWindowListener(new WindowAdapter() {
                @Override
                public void windowActivated(WindowEvent e) {A.this.setAlwaysOnTop(true);}
                @Override
                public void windowDeactivated(WindowEvent e) {
                    A.this.toBack();
                    mainFrame.toBack();
                }
            });
        }
    }
}

这篇关于框架始终只在我的程序之上的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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