java - 在 Java 中的双显示器配置上保持 jframe 打开

标签 java multiple-monitors

我有双显示器配置,并且我设法在所需的屏幕上打开我的 Jframe

但问题是,每次我单击主屏幕时,另一个窗口都会最小化,但我需要始终位于顶部。

PS:我选中了始终位于顶部复选框

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

这就是我使用另一个屏幕的方式。

最佳答案

只要我使用 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);
    }
}

这会在一台显示器上显示两个框架,并且在使用 ALT-Tab 或单击桌面时,框架不会最小化。

关于java - 在 Java 中的双显示器配置上保持 jframe 打开,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32048428/

相关文章:

java - 如何在 for 循环中使用 invalidate()

java - JPQL:在更新中将列设置为空

java - 关于官方 GWT MVP 框架的任何教程?

java - 在多显示器上居中 Java 启动画面

javascript - 需要做些什么才能实现对 Javascript 的多屏幕支持?

excel - 在主监视器上显示 Excel

c++ - 使用 win32/C++ 检索多个显示信息

java - 自定义 Spring Oauth 错误

c++ - Linux下双显示器设置的SDL假全屏模式

java - RSA 和 Base64 编码字节过多