[英]how to position the window stage in javafx?
我做了一個簡單的游戲,當它結束時,它會顯示一個包含一些信息的新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();
}
我不希望這個窗口顯示在屏幕中間,因為它隱藏了游戲的一些內容。 是否可以顯示不在屏幕中間的舞台?
您可以使用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()
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.