2017-03-27 3 views
-1

整数を変更すると、整数が変更されると、その文字の位置が変更されます。あなたは数が75であるとき、無用があり、写真からもわかるようにテキストが固定されている場所を、それがまだ中央にあるように拡大するように設定するにはどうすればよいですか?

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)); 

:私はそれはそれはここでは5に等しい場合でも、intは100に等しいときと同じ位置にあることを望むが、私のコードです私が赤い線で示したスペース。しかし、数字が100のとき、それは中心に完全に位置しています。だから、番号が変わったときにテキストが自動的にその位置を変えるようにするには

enter image description here enter image description here

+1

StackPaneを使用します。 StackPaneにラベルを追加します。この場合は、TextFlowをStackPaneに追加する必要があります。 StackPaneの幅を親の幅(親の幅にバインドされた幅)と同じにすることができると思います。次に、TextFlowの幅をStackPaneの最小幅より小さく設定します。 – Sedrick

答えて

0

最後に、私は解決策を発見した:に追加された新しい数字がある場合に

price.addListener((c, o, n)->{ 
    space.setPrefWidth(120-30*String.valueOf(n).length()); 
}); 

を私は価格(テキストではなく、しかしIntegerProperty)と毎回にリスナーを追加しましたnumber、space(Region)は、新しい桁のスペースのサイズだけ縮小されます。それは30に等しいです。それで、長さを定義するために新しい数値を文字列に変換し、それを30倍にしてスペースを減らします。

関連する問題