我做了一个简单的游戏,当它结束时,它会显示一个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();
}
Run Code Online (Sandbox Code Playgroud)
我不希望这个窗口显示在屏幕中间,因为它隐藏了游戏的一些内容。是否可以显示不在屏幕中间的舞台?
You can use the stage.setX() and stage.setY() methods to set the window position manually:
// create and init stage
stage.setX(200);
stage.setY(200);
stage.showAndWait();
Run Code Online (Sandbox Code Playgroud)
If you want to calculate the position based on the screen size you can use the following to get the size:
Rectangle2D bounds = Screen.getPrimary().getVisualBounds();
Run Code Online (Sandbox Code Playgroud)
如果要使用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);
Run Code Online (Sandbox Code Playgroud)
在这种情况下,您应该使用stage.show()代替stage.showAndWait().