带有 Java 的“始终在顶部”Windows

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/297938/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 12:44:55  来源:igfitidea点击:

"Always on Top" Windows with Java

javauser-interfaceswingawt

提问by Laplie Anderson

In Java, is there a way to have a window that is "Always on top" regardless if the user switches focus to another application? I've searched the web, and all of the solutions lean to some sort of JNI interface with native bindings. Truly this can't be the only way to do it?.. or is it?

在 Java 中,有没有办法让窗口“始终在最前面”,而不管用户是否将焦点切换到另一个应用程序?我在网上搜索过,所有的解决方案都依赖于某种带有本机绑定的 JNI 接口。这真的不是唯一的方法吗?.. 还是它?

采纳答案by OscarRyz

Try this method of the Windowclass:

试试这个Window类的方法:

Window.setAlwaysOnTop(boolean)

Window.setAlwaysOnTop(boolean)

It works the same way as the default in the Windows TaskManager: switch to another app but it shows always on top.

它的工作方式与 Windows 任务管理器中的默认设置相同:切换到另一个应用程序,但它始终显示在顶部。

This was added in Java 1.5

这是在 Java 1.5 中添加的

Sample code:

示例代码:

import javax.swing.JFrame;
import javax.swing.JLabel;

public class Annoying {
    public static void main(String[] args) {
        JFrame frame = new JFrame("Hello!!");

        // Set's the window to be "always on top"
        frame.setAlwaysOnTop( true );

        frame.setLocationByPlatform( true );
        frame.add( new JLabel("  Isn't this annoying?") );
        frame.pack();
        frame.setVisible( true );
    }
}

alt text

替代文字

Window remains on top even when is not active

即使不活动,窗口也保持在顶部

回答by pinkpanther

From my observation I found that AlwaysOnTop privilege is given to the latest process which requested to be always on top.

根据我的观察,我发现 AlwaysOnTop 特权授予了要求始终处于最前面的最新进程。

So, if you have an application which setAlwaysOnTop(true)and later another application uses this option, the privilege is given to the second application. In order to work around this I have set the setAlwaysOnTop(false)and again setAlwaysOnTop(true)whenever any window comes on top of the current window.

因此,如果您有一个应用程序,setAlwaysOnTop(true)随后另一个应用程序使用此选项,则特权将授予第二个应用程序。为了解决这个问题setAlwaysOnTop(false)setAlwaysOnTop(true)每当任何窗口出现在当前窗口的顶部时,我都会再次设置和。

I've checked it with wordwebin windows. WordWeb is one of the applications which uses AlwaysOnTopoption from the OS

我已经用wordwebin检查过了windows。WordWeb 是使用AlwaysOnTop选项的应用程序之一OS

I'm not sure about if it works properly with your game scenario.

我不确定它是否适用于您的游戏场景。

Warning: I'm not aware of the side effects.

警告:我不知道副作用。

Here is the code example:

下面是代码示例:

import java.awt.event.*;

import javax.swing.*;

public class MainWindow extends JFrame implements WindowFocusListener
{
    public MainWindow()
    {
        addWindowFocusListener(this);
        setAlwaysOnTop(true);
        this.setFocusable(true);
       // this.setFocusableWindowState(true);
        panel = new JPanel();
        //setSize(WIDTH,HEIGHT);
        setUndecorated(true);
        setLocation(X,Y);
        setExtendedState(MAXIMIZED_BOTH);
        setVisible(true);
    }

    public void windowGainedFocus(WindowEvent e){}
    public void windowLostFocus(WindowEvent e)
    {
        if(e.getNewState()!=e.WINDOW_CLOSED){
            //toFront();
            //requestFocus();
            setAlwaysOnTop(false);
            setAlwaysOnTop(true);
            //requestFocusInWindow();
            System.out.println("focus lost");
        }

    }

    private JPanel panel;
    private static final int WIDTH = 200;
    private static final int HEIGHT = 200;
    private static final int X = 100;
    private static final int Y = 100;

    public static void main(String args[]){
              new MainWindow();}
    }

回答by X.Zhang

dont use setFullScreenWindow,just get the screen size and then setSize, and everything will be fine.

不要使用setFullScreenWindow,只需获取屏幕大小然后setSize,一切都会好起来的。