的默认实现
centeronScreen()如下:
Rectangle2D bounds = getWindowScreen().getVisualBounds();double centerX = bounds.getMinX() + (bounds.getWidth() - getWidth()) * CENTER_ON_SCREEN_X_FRACTION;double centerY = bounds.getMinY() + (bounds.getHeight() - getHeight()) * CENTER_ON_SCREEN_Y_FRACTION;x.set(centerX);y.set(centerY);
哪里
CENTER_ON_SCREEN_X_FRACTION = 1.0f / 2;CENTER_ON_SCREEN_Y_FRACTION = 1.0f / 3;
centerY总是将舞台设置为比中心高一点。
要将舞台定位在精确的中心,可以使用您设置的 自定义X和Y值 。
public class Main extends Application { @Override public void start(Stage primaryStage) { Button btn = new Button(); btn.setText("Say 'Hello World'"); btn.setonAction((ActionEvent event) -> { System.out.println("Hello World!"); }); StackPane root = new StackPane(); root.getChildren().add(btn); Scene scene = new Scene(root, 300, 250); primaryStage.setTitle("Hello World!"); primaryStage.setScene(scene); primaryStage.show(); Rectangle2D primScreenBounds = Screen.getPrimary().getVisualBounds(); primaryStage.setX((primScreenBounds.getWidth() - primaryStage.getWidth()) / 2); primaryStage.setY((primScreenBounds.getHeight() - primaryStage.getHeight()) / 2); } public static void main(String[] args) { launch(args); }}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)