JavaFx 文本中心位于 X 和 Y
JavaFx Text center to be at X and Y
如何让文字的中心动态定位在圆心?目前,文本创建时给定的 X 和 Y 坐标位于左下角,但我希望它们位于文本的中心。 (红点是圆的中心,也就是我想要文本中心的位置)。
当前:
预期:
public class CenteredText extends Application {
public static void main(String[] args) {
launch(args);
}
@Override
public void start(Stage mainStage) throws Exception {
Pane pane = new Pane();
Circle circle = new Circle(250, 250, 100);
Circle innerCircle = new Circle(250, 250,
98);
innerCircle.setFill(Color.WHITE);
Text text = new Text(250, 250, "test" );
// coords given to text
Circle coords = new Circle(250, 250, 1);
pane.getChildren().addAll(circle, innerCircle, text, coords);
BorderPane root = new BorderPane();
root.setCenter(pane);
mainStage.setScene(new Scene(root, 500, 500));
mainStage.show();
}
}
Label 控件将为您执行此操作:
public class Main extends Application {
public static void main(String[] args) {
launch(args);
}
@Override
public void start(Stage stage) {
stage.setTitle("Centered Text");
Circle circle = new Circle(100);
Circle innerCircle = new Circle(98);
innerCircle.setFill(Color.WHITE);
StackPane circles = new StackPane(circle, innerCircle);
Label label = new Label("test", circles);
label.setContentDisplay(ContentDisplay.CENTER);
Pane p = new Pane(label);
Scene scene = new Scene(p);
stage.setScene(scene);
stage.show();
}
}
要使文本垂直居中,set its text origin to CENTER。
您需要自己计算 X 坐标。由于您希望文本总宽度的一半留在 250 的左侧,因此您需要知道总宽度才能进行数学计算。
CSS 在节点处于场景中之前不会应用于节点。但是您可以放弃 CSS 和 set the font directly,这样程序可以立即知道 Text 对象的适当首选宽度:
Text text = new Text(250, 250, "test");
text.setFont(Font.font("Arial", 24));
double width = text.prefWidth(-1);
text.setX(250 - width / 2);
text.setTextOrigin(VPos.CENTER);
如何让文字的中心动态定位在圆心?目前,文本创建时给定的 X 和 Y 坐标位于左下角,但我希望它们位于文本的中心。 (红点是圆的中心,也就是我想要文本中心的位置)。
当前:
public class CenteredText extends Application {
public static void main(String[] args) {
launch(args);
}
@Override
public void start(Stage mainStage) throws Exception {
Pane pane = new Pane();
Circle circle = new Circle(250, 250, 100);
Circle innerCircle = new Circle(250, 250,
98);
innerCircle.setFill(Color.WHITE);
Text text = new Text(250, 250, "test" );
// coords given to text
Circle coords = new Circle(250, 250, 1);
pane.getChildren().addAll(circle, innerCircle, text, coords);
BorderPane root = new BorderPane();
root.setCenter(pane);
mainStage.setScene(new Scene(root, 500, 500));
mainStage.show();
}
}
Label 控件将为您执行此操作:
public class Main extends Application {
public static void main(String[] args) {
launch(args);
}
@Override
public void start(Stage stage) {
stage.setTitle("Centered Text");
Circle circle = new Circle(100);
Circle innerCircle = new Circle(98);
innerCircle.setFill(Color.WHITE);
StackPane circles = new StackPane(circle, innerCircle);
Label label = new Label("test", circles);
label.setContentDisplay(ContentDisplay.CENTER);
Pane p = new Pane(label);
Scene scene = new Scene(p);
stage.setScene(scene);
stage.show();
}
}
要使文本垂直居中,set its text origin to CENTER。
您需要自己计算 X 坐标。由于您希望文本总宽度的一半留在 250 的左侧,因此您需要知道总宽度才能进行数学计算。
CSS 在节点处于场景中之前不会应用于节点。但是您可以放弃 CSS 和 set the font directly,这样程序可以立即知道 Text 对象的适当首选宽度:
Text text = new Text(250, 250, "test");
text.setFont(Font.font("Arial", 24));
double width = text.prefWidth(-1);
text.setX(250 - width / 2);
text.setTextOrigin(VPos.CENTER);