【问题标题】:how to position the window stage in javafx?如何在javafx中定位窗口舞台?
【发布时间】:2019-08-05 22:13:09
【问题描述】:

我做了一个简单的游戏,当它结束时会显示一个新的Stage 和一些信息:

 public static void d(String m){
    Stage stage = new Stage();
    stage.setTitle("GAME FINISHED");
    Label label = new Label(m);
    label.setTextFill(Color.PURPLE);
    label.setFont(new Font("Cambria",14));

    Button button = new Button("Close");
    VBox vb = new VBox();
    button.setOnAction(e-> p.close());
    vb.getChildren().addAll(label,button);
    vb.setSpacing(50);
    vb.setPadding(new Insets(5,0,0,12));
    Scene scene = new Scene(v,200,300);
    stage.setScene(scene);
    stage.setResizable(false);
    stage.showAndWait();
}

我不希望这个窗口显示在屏幕中间,因为它隐藏了一些游戏内容。 是否可以在屏幕中间显示不的舞台?

【问题讨论】:

    标签: java javafx stage


    【解决方案1】:

    您可以使用stage.setX()和stage.setY()方法手动设置窗口位置:

    // create and init stage
    stage.setX(200);
    stage.setY(200);
    stage.showAndWait();
    

    如果要根据屏幕大小计算位置,可以使用以下方法获取大小:

    Rectangle2D bounds = Screen.getPrimary().getVisualBounds();
    

    如果你想使用stage.getWidth()或stage.getHeight()来计算你必须展示之前舞台的位置:

    stage.show();
    Rectangle2D bounds = Screen.getPrimary().getVisualBounds();
    double x = bounds.getMinX() + (bounds.getWidth() - scene.getWidth()) * 0.3;
    double y = bounds.getMinY() + (bounds.getHeight() - scene.getHeight()) * 0.7;
    stage.setX(x);
    stage.setY(y);
    

    在这种情况下,您应该使用stage.show() 而不是stage.showAndWait()。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-10-12
      • 2013-11-30
      • 2013-08-12
      • 1970-01-01
      • 1970-01-01
      • 2017-12-17
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多