在双显示器配置中的特定屏幕中显示 JFrame

IT小君   2021-09-26T06:26:00

我有一个双显示器配置,如果找到的话,我想在特定的显示器上运行我的 GUI。我试图JFrame通过GraphicConfiguration我的屏幕设备的一个对象来创建我的窗口,但它不起作用,框架仍然显示在主屏幕上。

如何设置必须显示我的框架的屏幕?

点击广告,支持我们为你提供更好的服务
评论(12)
IT小君
public static void showOnScreen( int screen, JFrame frame )
{
    GraphicsEnvironment ge = GraphicsEnvironment
        .getLocalGraphicsEnvironment();
    GraphicsDevice[] gs = ge.getScreenDevices();
    if( screen > -1 && screen < gs.length )
    {
        gs[screen].setFullScreenWindow( frame );
    }
    else if( gs.length > 0 )
    {
        gs[0].setFullScreenWindow( frame );
    }
    else
    {
        throw new RuntimeException( "No Screens Found" );
    }
}
2021-09-26T06:26:00   回复
IT小君

我已经修改了@Joseph-gordon 的答案,以便在不强制全屏的情况下实现这一点:

public static void showOnScreen( int screen, JFrame frame ) {
    GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
    GraphicsDevice[] gd = ge.getScreenDevices();
    if( screen > -1 && screen < gd.length ) {
        frame.setLocation(gd[screen].getDefaultConfiguration().getBounds().x, frame.getY());
    } else if( gd.length > 0 ) {
        frame.setLocation(gd[0].getDefaultConfiguration().getBounds().x, frame.getY());
    } else {
        throw new RuntimeException( "No Screens Found" );
    }
}

在这段代码中,我假设getDefaultConfiguration()永远不会返回 null。如果不是这种情况,那么请有人纠正我。但是,此代码可以将您移动JFrame到所需的屏幕。

2021-09-26T06:26:00   回复
IT小君

在阅读了 JFrame.setLocationRelativeTo Display on screen 2 的文档后,一个更清晰的解决方案

public void moveToScreen() {
    setVisible(false);
    GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
    GraphicsDevice[] screens = ge.getScreenDevices();
    int n = screens.length;
    for (int i = 0; i < n; i++) {
        if (screens[i].getIDstring().contentEquals(settings.getScreen())) {
            JFrame dummy = new JFrame(screens[i].getDefaultConfiguration());
            setLocationRelativeTo(dummy);
            dummy.dispose();
        }
    }
    setVisible(true);
}

此功能可用于在屏幕之间切换应用程序窗口

2021-09-26T06:26:00   回复
IT小君

我已经修改了 @Joseph-gordon 和 @ryvantage 的答案,以便在不强制全屏、固定屏幕配置位置并将其在选择屏幕上居中的情况下实现这一点:

public void showOnScreen(int screen, JFrame frame ) {
    GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
    GraphicsDevice[] gd = ge.getScreenDevices();
    int width = 0, height = 0;
    if( screen > -1 && screen < gd.length ) {
        width = gd[screen].getDefaultConfiguration().getBounds().width;
        height = gd[screen].getDefaultConfiguration().getBounds().height;
        frame.setLocation(
            ((width / 2) - (frame.getSize().width / 2)) + gd[screen].getDefaultConfiguration().getBounds().x, 
            ((height / 2) - (frame.getSize().height / 2)) + gd[screen].getDefaultConfiguration().getBounds().y
        );
        frame.setVisible(true);
    } else {
        throw new RuntimeException( "No Screens Found" );
    }
}
2021-09-26T06:26:01   回复
IT小君

请参考GraphicsDevice API,你有一个很好的例子。

2021-09-26T06:26:01   回复
IT小君

每个人都有自己的风格,基于其他风格,我添加了我的风格,因为其他人在选定屏幕上的窗口定位方面锁定了你。

这简直是​​最好的。它也允许您在另一个屏幕上设置位置。

public void setLocation( int screen, double x, double y ) {
        GraphicsEnvironment g = GraphicsEnvironment.getLocalGraphicsEnvironment();
        GraphicsDevice[]    d = g.getScreenDevices();

        if ( screen >= d.length ) {
                screen = d.length - 1;
        }

        Rectangle bounds = d[screen].getDefaultConfiguration().getBounds();
        
        // Is double?
        if ( x == Math.floor(x) && !Double.isInfinite(x) ) {
                x *= bounds.x;  // Decimal -> percentage
        }
        if ( y == Math.floor(y) && !Double.isInfinite(y) ) {
                y *= bounds.y;  // Decimal -> percentage
        }

        x = bounds.x      + x;
        y = jframe.getY() + y;

        if ( x > bounds.x) x = bounds.x;
        if ( y > bounds.y) y = bounds.y;

        // If double we do want to floor the value either way 
        jframe.setLocation((int)x, (int)y);
}

例子:

setLocation(2, 200, 200);

甚至允许您传入屏幕位置的百分比!

setLocation(2, 0.5, 0);     // Place on right edge from the top down if combined with setSize(50%, 100%);

screen 必须大于 0,我相信这是一个艰难的要求!

要放在最后,只需使用Integer.MAX_VALUE调用

2021-09-26T06:26:01   回复
IT小君

我的经验是跨多个显示器扩展桌面,而不是将显示器配置为单独的 (X11) 显示器。如果这不是你想要做的,这将不适用。

我的解决方案有点小技巧:我调用了Toolkit.getScreenSize(),确定我是否处于多显示器情况(通过将高度与宽度进行比较并假设宽度 > 两倍高度表示多显示器),然后设置初始 X 和框架的 Y 位置。

2021-09-26T06:26:01   回复
IT小君

根据@ryvantage 的回答,我对其进行了改进,使其显示在屏幕中央:

private static void showOnScreen( int screen, Window frame ) {
    GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
    GraphicsDevice[] gd = ge.getScreenDevices();
    GraphicsDevice graphicsDevice;
    if( screen > -1 && screen < gd.length ) {
        graphicsDevice = gd[screen];
    } else if( gd.length > 0 ) {
        graphicsDevice = gd[0];
    } else {
        throw new RuntimeException( "No Screens Found" );
    }
    Rectangle bounds = graphicsDevice.getDefaultConfiguration().getBounds();
    int screenWidth = graphicsDevice.getDisplayMode().getWidth();
    int screenHeight = graphicsDevice.getDisplayMode().getHeight();
    frame.setLocation(bounds.x + (screenWidth - frame.getPreferredSize().width) / 2,
            bounds.y + (screenHeight - frame.getPreferredSize().height) / 2);
    frame.setVisible(true);
}
2021-09-26T06:26:02   回复
IT小君

对我来说也工作得很好(假设左显示器的尺寸为 1920x1200):

A) 设置在左侧监视器上的某个确切位置:

newFrame.setBounds(200,100,400,200)

B) 设置在正确位置的右侧显示器上:

newFrame.setBounds(2000,100,200,100)

C) 在左监视器上设置最大化:

newFrame.setBounds(200,100,400,200) newFrame.setExtendedState(JFrame.MAXIMIZED_BOTH)

D) 设置在右侧显示器上最大化

newFrame.setBounds(2000,100,200,100) newFrame.setExtendedState(JFrame.MAXIMIZED_BOTH)

2021-09-26T06:26:02   回复
IT小君

这里的许多解决方案适用于扩展显示。如果您使用单独的显示器,只需将所需图形设备的图形配置对象传递给 jframe 或 jdialog 的构造函数。

2021-09-26T06:26:02   回复
IT小君

Vickys 的答案包含正确的指针。它是新的 JFrame(GraphicsConfiguration gc) 做到的。

你可以这样做:

GraphicsDevice otherScreen = getOtherScreen(this);
JFrame frameOnOtherScreen = new JFrame(otherScreen.getDefaultConfiguration());

private GraphicsDevice getOtherScreen(Component component) {
    GraphicsEnvironment graphicsEnvironment = GraphicsEnvironment.getLocalGraphicsEnvironment();
    if (graphicsEnvironment.getScreenDevices().length == 1) {
        // if there is only one screen, return that one
        return graphicsEnvironment.getScreenDevices()[0];
    }

    GraphicsDevice theWrongOne = component.getGraphicsConfiguration().getDevice();
    for (GraphicsDevice dev : graphicsEnvironment.getScreenDevices()) {
        if (dev != theWrongOne) {
            return dev;
        }
    }

    return null;
}
2021-09-26T06:26:02   回复
IT小君

如果您想将其设置为左侧屏幕的中心:

int halfScreen = (int)(screenSize.width/2);
                frame.setLocation((halfScreen - frame.getSize().width)/2, (screenSize.height - frame.getSize().height)/2);

如果您想将其设置为右侧屏幕的中心:

int halfScreen = (int)(screenSize.width/2);
                frame.setLocation((halfScreen - frame.getSize().width)/2 + halfScreen, (screenSize.height - frame.getSize().height)/2);
2021-09-26T06:26:03   回复