Java setFullScreenWindow() 保持领先

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11497910/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 05:19:52  来源:igfitidea点击:

Java setFullScreenWindow() keep on top

javaswingjframefullscreen

提问by BitFiber

I'm writing an application that is intended to be run on a dual monitor setup, with a "Display" JFrame going fullscreen on one monitor and a "Control" JFrame on the other monitor, sending instructions to the Display. I've tried two separate methods of setting the Display fullscreen; the success of each seems to depend on the OS.

我正在编写一个旨在在双显示器设置上运行的应用程序,其中一个显示器上的“显示”JFrame 全屏显示,另一台显示器上的“控制”JFrame 向显示器发送指令。我尝试了两种不同的设置显示全屏的方法;每个的成功似乎都取决于操作系统。

display.setUndecorated(true);
display.setExtendedState(JFrame.MAXIMIZED_BOTH);

Works in Windows, but the JFrame gets hidden under the dock/panels in OS X and Linux.

在 Windows 中工作,但 JFrame 隐藏在 OS X 和 Linux 中的停靠/面板下。

My other method, utilizing

我的另一种方法,利用

GraphicsDevice.setFullScreenWindow(display);

Works in all three OSes that I tried, but in Windows, focusing the Control window on the other monitor makes the Display window hide, and calling

适用于我尝试过的所有三个操作系统,但在 Windows 中,将控制窗口聚焦在另一台显示器上会使显示窗口隐藏,并调用

display.setAlwaysOnTop(true);

Doesn't fix the problem. I'm kind of partial to the GraphicsDevicemethod because I don't have to deal with the issues in OS X or Linux, and I'm hoping that the Windows problem is a simple fix. Is it?

不解决问题。我有点偏爱这种GraphicsDevice方法,因为我不必处理 OS X 或 Linux 中的问题,而且我希望 Windows 问题是一个简单的解决方案。是吗?

采纳答案by Kumar Vivek Mitra

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.

使用public final void setAlwaysOnTop(boolean alwaysOnTop)了把最上面的窗口,如果窗口是可见的,这包括将窗口toFront,然后在“坚持”它最上面的位置。

回答by user23127

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 strategynever return out of the show function. Thus something like this:

我遇到了同样的问题。我解决它的方法是覆盖show()jframe 中的函数,并使用缓冲策略从不返回 show 函数。因此是这样的:

 @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()(showis 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.

如果使用一个setVisible(boolean)代替show()(show已被弃用)实际上会更好。窗口不会总是在顶部(您仍然可以将另一个窗口拖到它上面),但是当您将焦点放在另一个窗口时它不会自动隐藏。我猜这就是你想要的行为。

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

注意:不要show在 eventqueue 中调用,因为这会使 eventqueue 无用,并使 jframe 忽略所有事件。该函数show应在新线程中调用,然后所有事件仍将被处理。