如何将窗口始终设置在其他任何应用程序之上 [英] How to set window always on top of any other applications

查看:99
本文介绍了如何将窗口始终设置在其他任何应用程序之上的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个JFrame,它必须始终位于其他应用程序的顶部.为此,我正在使用

I have a JFrame which needs to be always on top of the other application. For this I am using setAlwaysOnTop() method of Window class.

这是我的代码:

class Test1 extends JFrame {

    public Test1() {
        initComponents();
        setTitle("Top Window");
        setAlwaysOnTop(true);
    }


    private void initComponents() {
        jLabel1 = new JLabel();

        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        jLabel1.setHorizontalAlignment(SwingConstants.CENTER);
        jLabel1.setText("I am a top most window");
        getContentPane().add(jLabel1, BorderLayout.CENTER);

        pack();
    }                     

    public static void main(String args[]) {

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

    private javax.swing.JLabel jLabel1;
}

这可以与其他一些应用程序(例如记事本,资源管理器等)一起正常工作,即,当我的应用程序位于记事本上方时,一切正常.

This works fine with some of the other application such as notepad, explorer etc, i.e. when my application is above notepad everything works fine.

但是,当我的Java应用程序超出任何已在顶部的应用程序(例如任务管理器)时,然后,方法setAlwaysOnTop()不起作用.

But when my java application goes above any application that is already on top such as task manager. Then the method setAlwaysOnTop() doesn't works.

我所需要的是使我的应用程序始终处于最优先位置的任何方法.

What I need is any way through which I can make my application always on top.

我还搜索了关于stackoverflow的许多其他相关文章,但似乎都没有一个答案.我还尝试了其他方法,例如覆盖windowDeactivated(WindowEvent e)方法

I have also searched many other related posts on stackoverflow but none seems to answer my question. I have also tried other ways such as overriding the windowDeactivated(WindowEvent e) method

addWindowListener(new WindowAdapter(){
    @Override
    public void windowDeactivated(WindowEvent e) {
        toFront();
        //requestFocus();
        //requestFocusInWindow();
    }
});

但是这也不起作用.

除了全屏应用程序以外,还有其他方法可以使我的 JFrame始终位于其他所有应用程序之上.

Is there any other way through which I can may my JFrame always on top of every other application except in case of full screen applications.

推荐答案

这取决于操作系统.通常,对于本机窗口,您不能保证窗口始终位于最顶部,您也不应该保证.任务管理器始终处于最佳状态,这是有充分理由的.

It depends on the operating system. In general, you can't guarantee that your window will always be on top when it comes to native windows, nor should you. The task manager is always-on-top for good reasons.

我记得某些版本的Vista和较旧的Windows系统允许这样做,而本机窗口和Java窗口最终争夺焦点.

I remember that some versions of Vista and older Windows systems allowed that and the native and Java windows ended up fighting for focus.

这篇关于如何将窗口始终设置在其他任何应用程序之上的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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