我正在尝试安排文本的位置。如果应该显示的文本不那么长,它工作正常,但如果有更长的措辞,定位不再起作用。任何想法?
Group root = new Group();
Pane holder = new Pane();
holder.setLayoutX(10);
holder.setLayoutY(10);
Scene scene = new Scene(root, 400, 400);
scene.getStylesheets().add(getClass().getResource("myStyle.css").toExternalForm());
primaryStage.setScene(scene);
Text txt = new Text("This is a Long Text");
txt.getStyleClass().add("roundedNavText");
double tWidth = Math.round(txt.getBoundsInLocal().getWidth());
double tHeight = Math.round(txt.getBoundsInLocal().getHeight());
int textOffsetFromBottom = 10;
int rectangleTextOffset = 35;
Rectangle r = new Rectangle(tWidth + (rectangleTextOffset * 2),200);
r.setArcHeight(20);
r.setArcWidth(20);
r.setFill(Color.web("#668845"));
txt.setLayoutX(r.getLayoutX()+r.getWidth()/2-tWidth);
txt.setLayoutY(r.getLayoutY()+r.getHeight()-tHeight-textOffsetFromBottom);
holder.getChildren().addAll(r, txt);
root.getChildren().add(holder);
//------------------------------------------------------
// myStyle.css
//------------------------------------------------------
.roundedNavText{
-fx-font-family: Arial;
-fx-font-size: 18px;
-fx-text-fill: #311c09;
-fx-font-weight: bold;
-fx-font-smoothing-type: gray;
}