将应用程序移至javafx中的第二个窗口监视器

时间:2019-05-07 10:25:50

标签: java javafx monitor

我正在编写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();
}

`

1 个答案:

答案 0 :(得分:0)

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

    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();
相关问题