This is how you do it before the stage was made visible:
double width = 640;
double height = 480;
Rectangle2D screenBounds = Screen.getPrimary().getVisualBounds();
stage.setX((screenBounds.getWidth() - width) / 2);
stage.setY((screenBounds.getHeight() - height) / 2);
final Scene scene = new Scene( new Group(), width, height);
stage.setScene(scene);
stage.show();
and this after the stage was made visible, i. e. you can use the properties of the stage:
double width = 640;
double height = 480;
final Scene scene = new Scene( new Group(), width, height);
stage.setScene(scene);
stage.show();
Rectangle2D screenBounds = Screen.getPrimary().getVisualBounds();
stage.setX((screenBounds.getWidth() - stage.getWidth()) / 2);
stage.setY((screenBounds.getHeight() - stage.getHeight()) / 2);
If you have multiple screens, you can calculate the position manually using the list of Screen objects which is returned by the Screen.getScreens() method.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…