【问题标题】:Move application into second window monitor in javafx将应用程序移动到 javafx 中的第二个窗口监视器
【发布时间】:2019-05-07 10:25:50
【问题描述】:

我正在编写 java FX 应用程序。我有两台显示器连接到一台 PC。我想在第二个窗口监视器中打开应用程序。不知何故,我设法用 vbox 不加载它们的组件来做到这一点。但是当我尝试将我的 fxml 视图加载到 Parent 时,它根本没有反应。我在 stackoverflow 的回答后面复习了`

Choose which monitor does a JavaFX window open in

但是当您尝试加载 fxml 视图时它没有反应,为什么?我怎样才能使它工作?这是我的代码

@Override
public void start(Stage primaryStage) throws Exception
{
    AnchorPane root = FXMLLoader.load(getClass().getClassLoader().getResource("order_state_modal.fxml"));
    primaryStage.setTitle("Jasmin");
    Scene scene = new Scene(root);
    int ind = 1;
    for(Screen screen : Screen.getScreens())
    {
        Rectangle2D bounds =    screen.getVisualBounds();
        if(ind == 2)
        {
            primaryStage.setX(bounds.getMinX());
            primaryStage.setY(bounds.getMaxY());
        }
        ind ++;
    }

    primaryStage.setMaximized(true);
    primaryStage.setScene(scene);
    primaryStage.show();
}

`

【问题讨论】:

    标签: java javafx monitor


    【解决方案1】:

    我发现它是以下代码的答案

        AnchorPane root = new AnchorPane();
        Scene scene = new Scene(root);
        int index = 1;
        for (Screen screen : Screen.getScreens()) {
            Rectangle2D bounds = screen.getVisualBounds();
    
                if(index == 2)
                {
                primaryStage.setX(bounds.getMinX());
                primaryStage.setY(bounds.getMinY());
                }
            index ++;
        }
        root.getChildren().add(FXMLLoader.load(getClass().getClassLoader().getResource("order_state_modal.fxml")));
        primaryStage.setMaximized(true);
        primaryStage.setScene(scene);
        primaryStage.show();
    

    【讨论】:

    • 为什么不使用Screen.getScreens().get(2).getVisualBounds()(当然,在确保该索引处存在元素之后)?
    • 是的,这也是一个很好的方法,但不管怎样,谢谢你的帮助
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-10-30
    • 1970-01-01
    • 2015-01-12
    • 1970-01-01
    • 1970-01-01
    • 2018-10-28
    相关资源
    最近更新 更多