【问题标题】:Display two windows at the same time "on fullscreen" with JavaFx Scene Builder 2.0使用 JavaFx Scene Builder 2.0 同时“全屏”显示两个窗口
【发布时间】:2014-12-13 05:18:52
【问题描述】:

我正在开发一个迷你应用程序,我需要同时向用户显示 2 个窗口但全屏(该应用程序将为双屏用户制作)。

我正在 NetBeans 8.0.1 上使用 JavaFx Scene Builder 2.0

我试过了,但只有第二个窗口全屏显示。

public void showTwoScreens() {
    try {
        Parent root = FXMLLoader.load(getClass().getResource("ClientsOperationsWindow.fxml"));
        Scene scene = new Scene(root);

        globalStage.setScene(scene);
        globalStage.setFullScreen(true);
        globalStage.setResizable(true);
        globalStage.show();

        Stage anotherStage = new Stage();
        Parent secondRoot = FXMLLoader.load(getClass().getResource("ClientsSearchWindow.fxml"));

        Scene secondStage = new Scene(secondRoot);
        secondStage.setScene(anotherScene);
        secondStage.setFullScreen(true);
        secondStage.show();

    } catch (Exception ex) {
        System.out.println(ex.getMessage());
    }
}

是否可以全屏显示两个窗口?

谢谢!

【问题讨论】:

  • 我认为这是不可能的。我在我的 Mac 上测试了一些想法。在 Java 1.8.0_20 及更早版本中,JDK 的 Mac 实现使用它自己的全屏实现,但强制执行独占模式,因此如果您使用全屏,则只能激活一个窗口。 Java 1.8.0_40(使用抢先体验版)使用原生操作系统全屏模式。在这个版本中,如果我在两个阶段调用setFullScreen(true),当第二个进入它时,我会看到第一个退出全屏模式 - 所以我猜操作系统在这里强制执行排他性。我不知道在 Windows 上会发生什么,但我认为它会是一样的。

标签: netbeans javafx javafx-2 scenebuilder


【解决方案1】:

我认为你不能同时在两台显示器上全屏设置两个舞台,但你可以通过强制舞台尺寸获得相同的结果。

为此,我们将使用javafx.stage.Screen 来获取连接的每个不同显示器的特征。然后我们将 fxml 文件加载到每个场景中,并在其舞台上显示每个场景。使用Screen.getBounds(),我们现在是矩形的原点和尺寸,参考主屏幕。所以我们用这些矩形的边界来设置每个阶段的边界。最后我们将样式设置为未装饰。现在唯一缺少的功能是退出“全屏”模式组合键。

private Screen secondaryScreen;

@Override
public void start(Stage primaryStage) throws IOException {

    Screen primaryScreen = Screen.getPrimary();

    Parent root = FXMLLoader.load(getClass().getResource("Screen1.fxml"));
    Scene scene = new Scene(root);
    primaryStage.setScene(scene);
    Rectangle2D bounds = primaryScreen.getBounds();
    primaryStage.setX(bounds.getMinX());
    primaryStage.setY(bounds.getMinY());
    primaryStage.setWidth(bounds.getWidth());
    primaryStage.setHeight(bounds.getHeight());
    primaryStage.initStyle(StageStyle.UNDECORATED);
    primaryStage.show();

    // look for a second screen
    Screen.getScreens().stream()
            .filter(s->!s.equals(primaryScreen))
            .findFirst().ifPresent(s->secondaryScreen = s);

    if(secondaryScreen!=null){
        Stage secondaryStage = new Stage();
        Parent root2 = FXMLLoader.load(getClass().getResource("Screen2.fxml"));
        Scene scene2 = new Scene(root2);
        secondaryStage.setScene(scene2);
        Rectangle2D bounds2 = secondaryScreen.getBounds();
        secondaryStage.setX(bounds2.getMinX());
        secondaryStage.setY(bounds2.getMinY());
        secondaryStage.setWidth(bounds2.getWidth());
        secondaryStage.setHeight(bounds2.getHeight());
        secondaryStage.initStyle(StageStyle.UNDECORATED);
        secondaryStage.show();  
    } 
}

【讨论】:

  • 谢谢!我尝试了你的建议,它正在工作:) 除了“primaryStage.initStyle(StageStyle.UNDECORATED);”和“secondaryStage.initStyle(StageStyle.UNDECORATED);”这些行在应用程序执行期间引发了异常,因此我不得不将它们从我的代码中取出;)
猜你喜欢
  • 2017-11-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多