【问题标题】:Making an Unfocusable Window in Java用 Java 制作一个无法聚焦的窗口
【发布时间】:2010-01-09 03:30:47
【问题描述】:

(已解决:当窗口聚焦时,WindowStateListener 和对 toBack 的延迟调用)

大家好!

我一直在试图弄清楚如何制作一个java.awt.Window(任何子类都可以),这样它就不会被带到前面。我正在开发一个出现在所有应用程序窗口下方并在屏幕上显示小部件的 Java“类似 Samurize”的程序。就像“Always on top windows with Java”一样,我希望有一些简单的东西,如果可能的话,希望只是一个方法调用,但我已经检查了 API 文档,但我没有运气。

编辑: 抱歉,我的意思是“始终处于底部”,而不是简单的“无法聚焦”。

这是一个基本的测试用例。单击窗口时,它不应高于当前屏幕上的任何其他窗口:

import java.awt.*;
import javax.swing.*;

public class Main extends JFrame {
    public Main() {
        Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();

        setFocusable(false);
        setFocusableWindowState(false);
        setBounds(new Rectangle(dim));

        toBack();
    }

    public static void main(String[] args) {
        new Main().setVisible(true);
    }
}

【问题讨论】:

    标签: java user-interface swing awt jframe


    【解决方案1】:

    你想使用setFocusableWindowState(false)

    (fwiw,这是在您提到的帖子的顶部答案链接的 API 文档中)

    编辑:向执行toBack()的窗口状态更改添加一个监听器怎么样?

    编辑:您也可以考虑覆盖 toFront 方法以防止任何东西将窗口拉到前面。

    【讨论】:

    • 应该可以,但至少在我的 Mac OS X System 10.6 上,窗口仍然可以聚焦。
    • 啊哈!我想到了。在进行 WindowStateListener 处理时,您只需要使用 NSTimer 推迟调用 toBack()
    【解决方案2】:

    setFocusableWindowState

    public void setFocusableWindowState(boolean focusableWindowState)

    Sets whether this Window can become the focused Window if it meets the other requirements outlined in isFocusableWindow. If this Window's focusable Window state is set to false, then isFocusableWindow will return false. If this Window's focusable Window state is set to true, then isFocusableWindow may return true or false depending upon the other requirements which must be met in order for a Window to be focusable.
    
    Setting a Window's focusability state to false is the standard mechanism for an application to identify to the AWT a Window which will be used as a floating palette or toolbar, and thus should be a non-focusable Window. Setting the focusability state on a visible Window can have a delayed effect on some platforms — the actual change may happen only when the Window becomes hidden and then visible again. To ensure consistent behavior across platforms, set the Window's focusable state when the WIndow is invisible and then show it.
    
    Parameters:
        focusableWindowState - whether this Window can be the focused Window
    Since:
        1.4
    See Also:
        isFocusableWindow(), getFocusableWindowState(), isShowing(), Component.setFocusable(boolean)
    

    http://java.sun.com/j2se/1.5.0/docs/api/java/awt/Window.html#setFocusableWindowState%28boolean%29

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-06-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多