当更改整数时,包含它的文本会更改其位置。我希望它与int等于100时处于相同的位置。即使它等于5.这是我的代码:
Stage priceStage = new Stage();
priceStage.setResizable(false);
priceStage.setWidth(450);
priceStage.setHeight(450);
priceStage.setTitle(windowTitle);
IntegerProperty price, maxIncome, adLevel;
price = new SimpleIntegerProperty(priceDifference);
maxIncome = new SimpleIntegerProperty();
adLevel = new SimpleIntegerProperty(1);
Button goButton = new Button("Организовать!");
Text assignPriceText = new Text(" Цена на одного студента: ");
assignPriceText.setFont(Font.font("Arial", FontWeight.BOLD, 30));
Text plus = new Text("+");
Text minus = new Text("-");
Text priceInfo = new Text();
Text priceText = new Text();
Text studentsAmmountText = new Text();
Text maxIncomeText = new Text();
Text adLevelText = new Text();
priceText.textProperty().bind(price.asString("%d $"));
studentsAmmountText.textProperty().bind(studentsAmmount.asString("\nКоличество студентов: %d"));
maxIncomeText.textProperty().bind(maxIncome.asString("\nДоход: %d $"));
adLevelText.textProperty().bind(adLevel.asString("\nРеклама: %d %% \n"));
TextFlow priceTextFlow = new TextFlow(minus, priceText, plus, studentsAmmountText, maxIncomeText, adLevelText);
priceTextFlow.setPadding(new Insets(0,0,0,50));
plus.setFill(Color.RED);
minus.setFill(Color.RED);
plus.setFont(Font.font(80));
minus.setFont(Font.font(80));
priceText.setFont(Font.font(100));
studentsAmmountText.setFont(Font.font(30));
maxIncomeText.setFont(Font.font(30));
adLevelText.setFont(Font.font(30));
goButton.setFont(Font.font(30));
从图片中可以看出,当数字是75时,我用红线表示一个无用的空间。但是当数字为100时,它完全位于中心。那么当数字改变时,如何使文本自动改变其位置?
答案 0 :(得分:0)
最后,我找到了解决方案:
price.addListener((c, o, n)->{
space.setPrefWidth(120-30*String.valueOf(n).length());
});
我已经添加了监听器(不是文本,但是IntegerProperty),并且每当有新数字添加到数字时,空间(Region)就会减少新数字空间的大小。它等于30.所以我将新数字转换为字符串以定义它的长度,然后我将它加倍到30并减少空间。