7

(已解决:每当窗口聚焦时都会WindowStateListener延迟调用)toBack

大家好!

我一直在试图弄清楚如何制作一个java.awt.Window(任何子类都可以),以免它被带到前面。我正在开发一个出现在所有应用程序窗口下方并在屏幕上显示小部件的 Java“类似 Samurize”的程序。就像“使用 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);
    }
}
4

2 回答 2

9

你想用setFocusableWindowState(false)

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

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

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

于 2010-01-09T03:38:44.440 回答
1

setFocusableWindowState

公共无效 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

于 2010-01-09T03:40:08.033 回答