如何在javafx中定位窗口阶段?
我做了一个简单的游戏,当游戏结束时,它将显示一个新的如何在javafx中定位窗口阶段?,java,javafx,stage,Java,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(&quo
阶段
,其中包含一些信息:
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();
}
我不希望这个窗口显示在屏幕中间,因为它隐藏了游戏内容。
是否可以在屏幕中间显示一个阶段<强>不>强>?,可以使用“和”方法手动设置窗口位置:
// 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()