3 回答

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

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

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);
}
此功能可用于在屏幕之间切换应用程序窗口
添加回答
举报