简体   繁体   English

在Java中的双监视器配置下保持jframe打开

[英]Keep the jframe open on a dual monitor configuration in Java

I have a dual monitor configuration and I managed to open my Jframe on the desired screen. 我具有双监视器配置,并且设法在所需的屏幕上打开了Jframe

But the problem is, every time I click on my main screen the other window minimizes, but I needed to always be on top. 但是问题是,每次我单击主屏幕时,另一个窗口都会最小化,但是我必须始终位于最上面。

PS: I checked the always on top check box PS:我选中了始终位于最前面的复选框

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

that's how I'm using the other screen. 这就是我使用其他屏幕的方式。

As long as I use setFullScreenWindow I do get the same issue. 只要我使用setFullScreenWindow,我都会遇到相同的问题。 Replacing the frames by dialogs solves the issue but I guess you really want to have a frame, not a dialog. 通过对话框替换框架可以解决此问题,但我想您真的想拥有一个框架,而不是一个对话框。

So the solution is to manually maximize the frame instead of using setFullScreenWindow: 因此解决方案是手动最大化框架,而不是使用setFullScreenWindow:

import java.awt.*;
import javax.swing.*;

public class MultiMonitorFrame extends JFrame {

    public static void showFrameOnScreen(Window frame, int screen) {
        GraphicsEnvironment graphicsEnvironment = GraphicsEnvironment.getLocalGraphicsEnvironment();
        GraphicsDevice[] graphicsDevices = graphicsEnvironment.getScreenDevices();
        GraphicsDevice graphicsDevice = ( screen > -1 && screen < graphicsDevices.length ) ? graphicsDevices[screen] : graphicsDevices.length > 0 ? graphicsDevices[0] : null;
        if (graphicsDevice == null)
        {
            throw new RuntimeException( "There are no screens !" );
        }
        Rectangle bounds = graphicsDevice.getDefaultConfiguration().getBounds();
        frame.setSize(bounds.width, bounds.height);
        frame.setLocation(bounds.x, bounds.y);
    }

    public static void main(String[] args) {
        JFrame frame1 = new JFrame("First frame");
        frame1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame1.setVisible(true);
        frame1.setAlwaysOnTop(true);
        JFrame frame2 = new JFrame("Second frame");
        frame2.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame2.setVisible(true);
        frame2.setAlwaysOnTop(true);
        showFrameOnScreen(frame1, 1);
        showFrameOnScreen(frame2, 2);
    }
}

This shows the two frames each on one monitor and the frames do not get minimized when using ALT-Tab or clicking on the desktop. 这样可以在一个监视器上显示两个帧,并且在使用ALT-Tab或单击桌面时,这些帧不会被最小化。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM