2010-01-09 18 views
6

(SOLVED: a WindowStateListener và một cuộc gọi hoãn lại để toBack bất cứ khi nào cửa sổ là tập trung)Làm một Window Unfocusable trong Java

Xin chào tất cả!

Tôi đã cố gắng tìm ra cách tạo một java.awt.Window (bất kỳ lớp con nào sẽ làm) để nó không thể được đưa lên phía trước. Tôi đang làm việc trên một chương trình Java "Samurize-like" xuất hiện bên dưới tất cả các cửa sổ ứng dụng và hiển thị Widget trên màn hình. Cũng giống như "Always on top windows with Java", tôi hy vọng cho một cái gì đó đơn giản, hy vọng chỉ là một cuộc gọi phương pháp duy nhất, nếu có thể, nhưng tôi đã kiểm tra thông qua các tài liệu API và tôi đã không có may mắn.

Chỉnh sửa: Xin lỗi, ý tôi là "luôn ở dưới cùng" thay vì chỉ đơn giản là "không tập trung".

Đây là trường hợp kiểm tra cơ bản. Khi nhấp vào cửa sổ, nó không nên đi trên bất kỳ những người khác hiện trên màn hình:

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

Trả lời

8

Bạn muốn sử dụng setFocusableWindowState(false)

(fwiw, đây là trong tài liệu API được liên kết bởi các câu trả lời trên của bạn đã đề cập đến)

chỉnh sửa: điều gì về việc thêm người nghe vào thay đổi trạng thái cửa sổ thực thi toBack()?

chỉnh sửa: bạn cũng có thể xem xét ghi đè phương pháp toFront để ngăn mọi thứ kéo cửa sổ lên phía trước.

+0

Điều đó sẽ làm việc, nhưng ít nhất trên OS X Hệ thống máy Mac của tôi 10,6 cửa sổ vẫn có thể được tập trung. –

+0

Đã chỉnh sửa, xin lỗi. Xem bài đăng. –

+0

Aha! Tôi đã hiểu rồi. Bạn chỉ cần trì hoãn việc gọi 'toBack()' bằng 'NSTimer' khi thực hiện xử lý WindowStateListener. –

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

Các vấn đề liên quan