(已解决:只要窗口聚焦,WindowStateListener
和toBack
的延迟调用)
大家好!
我一直在试图弄清楚如何制作一个java.awt.Window
(任何子类都会这样做),以便它不会被带到前面。我正在开发一个Java“Samurize-like”程序,该程序出现在所有应用程序窗口下方,并在屏幕上显示Widgets。就像“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);
}
}
答案 0 :(得分:8)
您想使用setFocusableWindowState(false)
(fwiw,这是在你提到的帖子的最高答案链接的API文件中)
编辑:如何为执行toBack()
的窗口状态更改添加侦听器?
编辑:您可能还会考虑重写toFront
方法,以防止任何因素将窗口拉到前面。
答案 1 :(得分:1)
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