对 TextFields 执行算术运算的事件监听器
Event Listener to Performing Arithmetic Operations on TextFields
我对代码块的执行感到困惑。我有 4 个文本字段,每个文本字段是 quantity
、price
、vat
和 amount
。
我想向 price
和 quantity
文本字段添加一个事件侦听器,以便每当我更改每个文本字段时,新的 vat
计算和新的 amount
值会自动显示在各自的文本字段中。
我在想这样的事情:
private void addEventListeners() {
txtInvoiceQuantity.textProperty().addListener((ChangeListener) (observable, oldVal, newVal) -> calculateVATAmount((String) oldVal, (String) newVal));
}
private void calculateVATAmount(String oldVal, String newVal) {
if (newVal.length() > oldVal.length()) {
if (txtInvoiceQuantity.getText().length() > 0 && txtInvoicePrice.getText().length() > 0) {
try {
double quantity = Double.parseDouble(newVal);
double price = Double.parseDouble(txtInvoicePrice.getText());
double vat = Double.parseDouble(lblVAT.getText()) / 100;
double vatamount = quantity * price * vat;
txtInvoiceVAT.setText(String.valueOf(vatamount));
double invoiceAmount = (price * quantity) + vatamount;
txtInvoiceAmount.setText(String.valueOf(invoiceAmount));
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
我显然不能使用 txtInvoiceQuantity
上的那个事件。那么我怎样才能改变 calculateVATAmount(oldVal,newVal)
让它做我想做的事呢?
如何将 oldVal 也考虑在内(以防用户按回 space)?
嗯,据我了解,您必须复制该代码块,因为您要从每个处理程序中的不同文本字段传递参数。
至于考虑旧值,只需删除 if(newVal.length() > oldVal.length()) {}
即可。它应该是这样的:
private void quantityHandler(String oldVal, String newVal) {
if (txtInvoiceQuantity.getText().length() > 0 && txtInvoicePrice.getText().length() > 0) {
try {
double quantity = Double.parseDouble(newVal);
double price = Double.parseDouble(txtInvoicePrice.getText());
double vat = Double.parseDouble(lblVAT.getText()) / 100;
double vatamount = quantity * price * vat;
txtInvoiceVAT.setText(String.valueOf(round(vatamount,2)));
double invoiceAmount = (price * quantity) + vatamount;
txtInvoiceAmount.setText(String.valueOf(round(invoiceAmount,2)));
} catch (Exception e) {
e.printStackTrace();
}
}
}
private void priceHandler(String oldVal, String newVal) {
if (txtInvoiceQuantity.getText().length() > 0 && txtInvoicePrice.getText().length() > 0) {
try {
double quantity = Double.parseDouble(txtInvoiceQuantity.getText());
double price = Double.parseDouble(newVal);
double vat = Double.parseDouble(lblVAT.getText()) / 100;
double vatamount = quantity * price * vat;
txtInvoiceVAT.setText(String.valueOf(round(vatamount,2)));
double invoiceAmount = (price * quantity) + vatamount;
txtInvoiceAmount.setText(String.valueOf(round(invoiceAmount,2)));
} catch (Exception e) {
e.printStackTrace();
}
}
}
由于您使用的是货币值,我找到了一种转换为指定小数位数的方法,请在此处找到它
Rounding to 2 decimal places
我对代码块的执行感到困惑。我有 4 个文本字段,每个文本字段是 quantity
、price
、vat
和 amount
。
我想向 price
和 quantity
文本字段添加一个事件侦听器,以便每当我更改每个文本字段时,新的 vat
计算和新的 amount
值会自动显示在各自的文本字段中。
我在想这样的事情:
private void addEventListeners() {
txtInvoiceQuantity.textProperty().addListener((ChangeListener) (observable, oldVal, newVal) -> calculateVATAmount((String) oldVal, (String) newVal));
}
private void calculateVATAmount(String oldVal, String newVal) {
if (newVal.length() > oldVal.length()) {
if (txtInvoiceQuantity.getText().length() > 0 && txtInvoicePrice.getText().length() > 0) {
try {
double quantity = Double.parseDouble(newVal);
double price = Double.parseDouble(txtInvoicePrice.getText());
double vat = Double.parseDouble(lblVAT.getText()) / 100;
double vatamount = quantity * price * vat;
txtInvoiceVAT.setText(String.valueOf(vatamount));
double invoiceAmount = (price * quantity) + vatamount;
txtInvoiceAmount.setText(String.valueOf(invoiceAmount));
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
我显然不能使用 txtInvoiceQuantity
上的那个事件。那么我怎样才能改变 calculateVATAmount(oldVal,newVal)
让它做我想做的事呢?
如何将 oldVal 也考虑在内(以防用户按回 space)?
嗯,据我了解,您必须复制该代码块,因为您要从每个处理程序中的不同文本字段传递参数。
至于考虑旧值,只需删除 if(newVal.length() > oldVal.length()) {}
即可。它应该是这样的:
private void quantityHandler(String oldVal, String newVal) {
if (txtInvoiceQuantity.getText().length() > 0 && txtInvoicePrice.getText().length() > 0) {
try {
double quantity = Double.parseDouble(newVal);
double price = Double.parseDouble(txtInvoicePrice.getText());
double vat = Double.parseDouble(lblVAT.getText()) / 100;
double vatamount = quantity * price * vat;
txtInvoiceVAT.setText(String.valueOf(round(vatamount,2)));
double invoiceAmount = (price * quantity) + vatamount;
txtInvoiceAmount.setText(String.valueOf(round(invoiceAmount,2)));
} catch (Exception e) {
e.printStackTrace();
}
}
}
private void priceHandler(String oldVal, String newVal) {
if (txtInvoiceQuantity.getText().length() > 0 && txtInvoicePrice.getText().length() > 0) {
try {
double quantity = Double.parseDouble(txtInvoiceQuantity.getText());
double price = Double.parseDouble(newVal);
double vat = Double.parseDouble(lblVAT.getText()) / 100;
double vatamount = quantity * price * vat;
txtInvoiceVAT.setText(String.valueOf(round(vatamount,2)));
double invoiceAmount = (price * quantity) + vatamount;
txtInvoiceAmount.setText(String.valueOf(round(invoiceAmount,2)));
} catch (Exception e) {
e.printStackTrace();
}
}
}
由于您使用的是货币值,我找到了一种转换为指定小数位数的方法,请在此处找到它
Rounding to 2 decimal places