代码之家  ›  专栏  ›  技术社区  ›  Laplie Anderson

使用Java的“始终在顶部”Windows

  •  78
  • Laplie Anderson  · 技术社区  · 17 年前

    3 回复  |  直到 17 年前
        1
  •  165
  •   tstec OscarRyz    10 年前

    试试这个方法 Window 类别:

    Window.setAlwaysOnTop(boolean)

    它的工作方式与Windows TaskManager中的默认设置相同:切换到其他应用程序,但它始终显示在顶部。

    示例代码:

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

    alt text

        2
  •  11
  •   pinkpanther    12 年前

    从我的观察中,我发现最新的流程总是被赋予最优先的特权,要求始终处于最优先地位。

    所以,如果你有一个应用程序 setAlwaysOnTop(true) 之后,另一个应用程序使用此选项,特权将授予第二个应用程序。为了解决这个问题,我设置了 setAlwaysOnTop(false) setAlwaysOnTop(真) 每当任何窗口位于当前窗口的顶部时。

    wordweb 在里面 windows AlwaysOnTop 来自 OS

    我不确定它是否适合你的游戏场景。

    警告 :我不知道副作用。

    import java.awt.event.*;
    
    import javax.swing.*;
    
    public class MainWindow extends JFrame implements WindowFocusListener
    {
        public MainWindow()
        {
            addWindowFocusListener(this);
            setAlwaysOnTop(true);
            this.setFocusable(true);
           // this.setFocusableWindowState(true);
            panel = new JPanel();
            //setSize(WIDTH,HEIGHT);
            setUndecorated(true);
            setLocation(X,Y);
            setExtendedState(MAXIMIZED_BOTH);
            setVisible(true);
        }
    
        public void windowGainedFocus(WindowEvent e){}
        public void windowLostFocus(WindowEvent e)
        {
            if(e.getNewState()!=e.WINDOW_CLOSED){
                //toFront();
                //requestFocus();
                setAlwaysOnTop(false);
                setAlwaysOnTop(true);
                //requestFocusInWindow();
                System.out.println("focus lost");
            }
    
        }
    
        private JPanel panel;
        private static final int WIDTH = 200;
        private static final int HEIGHT = 200;
        private static final int X = 100;
        private static final int Y = 100;
    
        public static void main(String args[]){
                  new MainWindow();}
        }
    
        3
  •  0
  •   X.Zhang    8 年前

    不要使用setFullScreenWindow,只需获取屏幕大小,然后设置大小,一切都会很好。