“永远在最前面"带有 Java 的 Windows [英] "Always on Top" Windows with Java

查看:23
本文介绍了“永远在最前面"带有 Java 的 Windows的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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?

推荐答案

试试Window类的这个方法:

Window.setAlwaysOnTop(布尔值)

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

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

这是在 Java 1.5 中添加的

This was added in Java 1.5

示例代码:

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 );
    }
}

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

这篇关于“永远在最前面"带有 Java 的 Windows的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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