Choose which monitor does a JavaFX window open in

后端 未结 1 1394
予麋鹿
予麋鹿 2020-12-16 02:44

I have two monitors. I have Eclipse open on the second monitor but when I run my JavaFX code, the JavaFX window always opens up in the first monitor and every time I have to

相关标签:
1条回答
  • 2020-12-16 02:59

    You can iterate Screen.getScreens() and move your stage to required one. See example below.

    public class FXScreens extends Application {
    
        @Override
        public void start(Stage stage) {
            VBox root = new VBox(10);
            root.setAlignment(Pos.CENTER);
            Scene scene = new Scene(root, 200, 250);
    
            int index = 1;
            for (Screen screen : Screen.getScreens()) {
                Rectangle2D bounds = screen.getVisualBounds();
    
                Button btn = new Button("Move me to Screen " + index++);
                btn.setOnAction((e) -> {
                    stage.setX(bounds.getMinX() + 100);
                    stage.setY(bounds.getMinY() + 100);
                });
                root.getChildren().add(btn);
            }
    
            stage.setTitle("Screen Jumper");
            stage.setScene(scene);
            stage.show();
        }
    
        public static void main(String[] args) {
            launch(args);
        }
    }
    

    Unfortunately I can't help with components not loading on primary screen. Please provide more details to address that (which components, how are they created, code sample, etc)

    0 讨论(0)
提交回复
热议问题