在 javafx 的 TextField 中显示浮点数
showing floating numbers in TextField in javafx
我正在尝试在我的测试应用程序中添加付款部分。
我的 TextField 接受按钮值(从 0 到 9)并将其添加到末尾。
TextField pamount;
String value = ((Button)event.getSource()).getText();
pamount.setText(pamount.getText()+value);
所以,如果我连续点击按钮 1 到 9,我会得到
123456789
在文本字段中
我的问题是我希望它显示为像
这样的浮点数
1234567.89
有什么方法可以格式化 TextField 来保存或显示这样的值吗?
提前致谢。
使用小数格式:
DecimalFormat format = new DecimalFormat("#.00");
String formattedText = format.format(value);
如果您希望通过按下按钮来更改某个数字的值,您应该定义一个变量来保存该数字。为避免重复执行浮点运算(在某些时候可能会累积舍入误差),最好让该变量保存美分数(或等值的货币),这是一个整数。
此外,为了方便文本字段(或标签)始终显示与该值对应的文本值,使用 JavaFX observable property.
会很方便
所以我会这样做:
private IntegerProperty totalCents = new SimpleIntegerProperty();
然后您可以让您的显示器将其文本绑定到 属性:
的格式化版本
display.textProperty().bind(totalCents.divide(100.0).asString("$ %.2f"));
最后,您的按钮只需更新数值:
private Button createNumberButton(int value) {
Button button = new Button(Integer.toString(value));
button.setOnAction(e -> totalCents.set(totalCents.get() * 10 + value));
return button ;
}
这是一个 SSCCE:
import javafx.application.Application;
import javafx.beans.property.IntegerProperty;
import javafx.beans.property.SimpleIntegerProperty;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.layout.GridPane;
import javafx.stage.Stage;
public class PaymentPad extends Application {
private IntegerProperty totalCents = new SimpleIntegerProperty();
@Override
public void start(Stage primaryStage) {
GridPane pad = new GridPane();
pad.setHgap(2);
pad.setVgap(2);
pad.add(createNumberButton(0), 0, 4);
for (int i = 1 ; i <= 9 ; i++) {
int columnIndex = (i-1) % 3;
int rowIndex = 3 - (i-1) /3 ;
pad.add(createNumberButton(i), columnIndex, rowIndex);
}
Button clearButton = createButton("C");
clearButton.setOnAction(e -> totalCents.set(0));
pad.add(clearButton, 1, 4, 2, 1);
Label display = new Label();
display.textProperty().bind(totalCents.divide(100.0).asString("$%.2f"));
display.setMaxWidth(Double.MAX_VALUE);
display.setAlignment(Pos.CENTER_RIGHT);
pad.add(display, 0, 0, 3, 1);
Scene scene = new Scene(pad);
primaryStage.setScene(scene);
primaryStage.show();
}
private Button createNumberButton(int value) {
Button button = createButton(Integer.toString(value));
button.setOnAction(e -> totalCents.set(totalCents.get()*10+value));
return button ;
}
private Button createButton(String text) {
Button button = new Button(text);
button.setMaxSize(Double.MAX_VALUE, Double.MAX_VALUE);
GridPane.setFillHeight(button, true);
GridPane.setFillWidth(button, true);
return button ;
}
public static void main(String[] args) {
launch(args);
}
}
我正在尝试在我的测试应用程序中添加付款部分。
我的 TextField 接受按钮值(从 0 到 9)并将其添加到末尾。
TextField pamount;
String value = ((Button)event.getSource()).getText();
pamount.setText(pamount.getText()+value);
所以,如果我连续点击按钮 1 到 9,我会得到
123456789
在文本字段中
我的问题是我希望它显示为像
这样的浮点数1234567.89
有什么方法可以格式化 TextField 来保存或显示这样的值吗?
提前致谢。
使用小数格式:
DecimalFormat format = new DecimalFormat("#.00");
String formattedText = format.format(value);
如果您希望通过按下按钮来更改某个数字的值,您应该定义一个变量来保存该数字。为避免重复执行浮点运算(在某些时候可能会累积舍入误差),最好让该变量保存美分数(或等值的货币),这是一个整数。
此外,为了方便文本字段(或标签)始终显示与该值对应的文本值,使用 JavaFX observable property.
会很方便所以我会这样做:
private IntegerProperty totalCents = new SimpleIntegerProperty();
然后您可以让您的显示器将其文本绑定到 属性:
的格式化版本display.textProperty().bind(totalCents.divide(100.0).asString("$ %.2f"));
最后,您的按钮只需更新数值:
private Button createNumberButton(int value) {
Button button = new Button(Integer.toString(value));
button.setOnAction(e -> totalCents.set(totalCents.get() * 10 + value));
return button ;
}
这是一个 SSCCE:
import javafx.application.Application;
import javafx.beans.property.IntegerProperty;
import javafx.beans.property.SimpleIntegerProperty;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.layout.GridPane;
import javafx.stage.Stage;
public class PaymentPad extends Application {
private IntegerProperty totalCents = new SimpleIntegerProperty();
@Override
public void start(Stage primaryStage) {
GridPane pad = new GridPane();
pad.setHgap(2);
pad.setVgap(2);
pad.add(createNumberButton(0), 0, 4);
for (int i = 1 ; i <= 9 ; i++) {
int columnIndex = (i-1) % 3;
int rowIndex = 3 - (i-1) /3 ;
pad.add(createNumberButton(i), columnIndex, rowIndex);
}
Button clearButton = createButton("C");
clearButton.setOnAction(e -> totalCents.set(0));
pad.add(clearButton, 1, 4, 2, 1);
Label display = new Label();
display.textProperty().bind(totalCents.divide(100.0).asString("$%.2f"));
display.setMaxWidth(Double.MAX_VALUE);
display.setAlignment(Pos.CENTER_RIGHT);
pad.add(display, 0, 0, 3, 1);
Scene scene = new Scene(pad);
primaryStage.setScene(scene);
primaryStage.show();
}
private Button createNumberButton(int value) {
Button button = createButton(Integer.toString(value));
button.setOnAction(e -> totalCents.set(totalCents.get()*10+value));
return button ;
}
private Button createButton(String text) {
Button button = new Button(text);
button.setMaxSize(Double.MAX_VALUE, Double.MAX_VALUE);
GridPane.setFillHeight(button, true);
GridPane.setFillWidth(button, true);
return button ;
}
public static void main(String[] args) {
launch(args);
}
}