为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

暮色呼如 2019-12-13 14:10:55
我有一个双监视器配置,如果找到它,我想在特定的监视器中运行我的GUI。我试图创建JFrame一个通过GraphicConfiguration屏幕设备对象的窗口,但是它不起作用,框架仍显示在主屏幕上。如何设置必须显示框架的屏幕?
查看完整描述

3 回答

?
慕标5832272

TA贡献1966条经验 获得超4个赞

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" );

    }

}



查看完整回答
反对 回复 2019-12-14
?
阿波罗的战车

TA贡献1862条经验 获得超6个赞

以允许在不强制全屏的情况下实现此目的:


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移至所需的屏幕。



查看完整回答
反对 回复 2019-12-14
?
繁星点点滴滴

TA贡献1803条经验 获得超3个赞

阅读屏幕2上有关JFrame.setLocationRelativeTo的文档后,一种更清洁的解决方案


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);

}

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



查看完整回答
反对 回复 2019-12-14
  • 3 回答
  • 0 关注
  • 323 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信