Java setFullScreenWindow() keep on top

自作多情 提交于 2019-11-30 18:29:19

Try this...

For Multiple Screen

GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
GraphicsDevice[] gs = ge.getScreenDevices();


// Get size of each screen

for (int i=0; i<gs.length; i++) {
    DisplayMode dm = gs[i].getDisplayMode();
    int screenWidth = dm.getWidth();
    int screenHeight = dm.getHeight();
}

Use public final void setAlwaysOnTop(boolean alwaysOnTop) for putting the window on top, If the window is visible, this includes bringing window toFront, then "sticking" it to the top-most position.

I run across the same problem. My way to solve it was to override the show() function in the jframe and by using a buffer strategy never return out of the show function. Thus something like this:

 @override
 public void show(){
        super.show();
        //Create a double buffering strategy
        createBufferStrategy(2);
        BufferStrategy bs = getBufferStrategy();
        while(true){
              //draw our frame
              Graphics g = bs.getGraphics();
              paint(g);
              //dispose of our graphics
              g.dispose();
              //Show our frame
              bs.show();
              try{
                  //Don't use all our cpu-power
                  Thread.sleep(10);
              }catch(Exception e){
                  //Do something (this probably will never happen)
              }
        }
 }

It would actually be better if one used setVisible(boolean) instead of show() (show is deprecated). The window won't always be on top (you can still drag another window on top of it), but it won't automatically hide when you give focus to another window. That is the behaviour you want I guess.

Note: Do not call show in the eventqueue, as this will render the eventqueue useless, and makes the jframe ignore all events. The function show should be called in a new thread, then all events will still be handled.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!