JavaFX程序正在运行,但仍然出现异常
我正在编写一个JavaFX程序,它使用滑块计算小费。它工作得相当好,但当我更改滑块的值时,会出现一系列错误,例如线程“JavaFX Application thread”java.lang.RuntimeException:Label.text:无法设置绑定值。 我的错误似乎与绑定到滑块有关,但不确定是什么问题,因为它按我预期的方式运行。任何指点都将不胜感激。谢谢 节目如下: TipCalculator.javaJavaFX程序正在运行,但仍然出现异常,java,javafx,bigdecimal,Java,Javafx,Bigdecimal,我正在编写一个JavaFX程序,它使用滑块计算小费。它工作得相当好,但当我更改滑块的值时,会出现一系列错误,例如线程“JavaFX Application thread”java.lang.RuntimeException:Label.text:无法设置绑定值。 我的错误似乎与绑定到滑块有关,但不确定是什么问题,因为它按我预期的方式运行。任何指点都将不胜感激。谢谢 节目如下: TipCalculator.java import javafx.application.Application; im
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;
public class TipCalculator extends Application
{
@Override
public void start(Stage stage) throws Exception
{
Parent root = FXMLLoader.load(getClass().getResource("TipCalculator.fxml"));
Scene scene = new Scene(root);
stage.setTitle("Tip Calculator");
stage.setScene(scene);
stage.show();
}
public static void main(String[] args)
{
launch(args);
}
}
TipCalculatorController.java
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.text.NumberFormat;
import javafx.beans.property.Property;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.fxml.FXML;
import javafx.scene.control.Label;
import javafx.scene.control.Slider;
import javafx.scene.control.TextField;
public class TipCalculatorController
{
private static final NumberFormat currency =
NumberFormat.getCurrencyInstance();
private static final NumberFormat percent =
NumberFormat.getPercentInstance();
private BigDecimal tipPercentage = new BigDecimal(0.15);
@FXML
private TextField amountTextField;
@FXML
private Label tipPercentageLabel;
@FXML
private Slider tipPercentageSlider;
@FXML
private TextField tipTextField;
@FXML
private TextField totalTextField;
@FXML
private void initialize()
{
try
{
BigDecimal amount = new BigDecimal(amountTextField.getText());
BigDecimal tip = amount.multiply(tipPercentage);
BigDecimal total = amount.add(tip);
tipTextField.setText(currency.format(tip));
totalTextField.setText(currency.format(total));
}
catch (NumberFormatException ex)
{
amountTextField.setText("Enter amount");
amountTextField.selectAll();
amountTextField.requestFocus();
}
tipPercentageLabel.textProperty().bind
(tipPercentageSlider.valueProperty().asString("%.0f"));
currency.setRoundingMode(RoundingMode.HALF_UP);
tipPercentageSlider.valueProperty().addListener(
new ChangeListener<Number>()
{
@Override
public void changed(ObservableValue<? extends Number> ov,
Number oldValue, Number newValue)
{
tipPercentage =
BigDecimal.valueOf(newValue.intValue() / 100.0);
tipPercentageLabel.setText(percent.format(tipPercentage));
}
}
);
}
}
import java.math.BigDecimal;
导入java.math.RoundingMode;
导入java.text.NumberFormat;
导入javafx.beans.property.property;
导入javafx.beans.value.ChangeListener;
导入javafx.beans.value.observeValue;
导入javafx.fxml.fxml;
导入javafx.scene.control.Label;
导入javafx.scene.control.Slider;
导入javafx.scene.control.TextField;
公共类TipCalculator控制器
{
私有静态最终数字格式货币=
NumberFormat.getCurrencyInstance();
专用静态最终数字格式百分比=
NumberFormat.getPercentInstance();
私有BigDecimal TipperEntage=新的BigDecimal(0.15);
@FXML
私有TextField amountTextField;
@FXML
自有品牌tipPercentageLabel;
@FXML
私人滑杆式翻斗车;
@FXML
私有文本字段tipTextField;
@FXML
私有文本字段totalTextField;
@FXML
私有void初始化()
{
尝试
{
BigDecimal金额=新的BigDecimal(amountTextField.getText());
BigDecimal tip=金额乘以(TipperEntage);
BigDecimal合计=金额。添加(提示);
tipTextField.setText(currency.format(tip));
totalTextField.setText(currency.format(total));
}
捕获(NumberFormatException ex)
{
amountTextField.setText(“输入金额”);
amountTextField.selectAll();
amountTextField.requestFocus();
}
tipPercentageLabel.textProperty().bind
(tipPercentageSlider.valueProperty().asString(“%.0f”);
货币。设置舍入模式(舍入模式。向上舍入一半);
TipperEntageSlider.valueProperty().addListener(
新的ChangeListener()
{
@凌驾
public void changed(ObservalEvalue正如例外情况所述,无法设置绑定值。绑定的要点是,您声明一个属性应始终具有依赖于另一个属性的值。如果您可以直接设置绑定属性,您将能够违反绑定指定的约定
所以当你这么做的时候
tipPercentageLabel.textProperty().bind
(tipPercentageSlider.valueProperty().asString("%.0f"));
确保tipPercentageLabel
的文本属性始终具有tipPercentageSlider.getValue()
(格式为一个小数点等)。如果允许设置tipPercentageLabel
的文本,则可能会违反此绑定,因此现在禁止这样做
因此,
tipPercentageSlider.valueProperty().addListener(
new ChangeListener<Number>()
{
@Override
public void changed(ObservableValue<? extends Number> ov,
Number oldValue, Number newValue)
{
tipPercentage =
BigDecimal.valueOf(newValue.intValue() / 100.0);
tipPercentageLabel.setText(percent.format(tipPercentage));
}
}
);
或者你也可以这样做
tipPercentageLabel.textProperty().bind(tipPercentageSlider.asString("%.0f%%"));
(%%
格式字符串中给出了一个“%”
符号).如例外情况所述,无法设置绑定值。绑定的要点是您声明一个属性应始终具有依赖于另一个属性的值。如果您可以直接设置绑定属性,则可能会违反绑定指定的约定
所以当你这么做的时候
tipPercentageLabel.textProperty().bind
(tipPercentageSlider.valueProperty().asString("%.0f"));
确保tipPercentageLabel
的文本属性始终具有tipPercentageSlider.getValue()
(格式为一个小数点等)。如果允许设置tipPercentageLabel
的文本,则可能会违反此绑定,因此现在禁止这样做
因此,
tipPercentageSlider.valueProperty().addListener(
new ChangeListener<Number>()
{
@Override
public void changed(ObservableValue<? extends Number> ov,
Number oldValue, Number newValue)
{
tipPercentage =
BigDecimal.valueOf(newValue.intValue() / 100.0);
tipPercentageLabel.setText(percent.format(tipPercentage));
}
}
);
或者你也可以这样做
tipPercentageLabel.textProperty().bind(tipPercentageSlider.asString("%.0f%%"));
(%%
格式字符串中给出了一个“%”
符号)。谢谢,我没有意识到我试图显式设置tipPercentageLabel。我最终删除了最后一行代码:tipPercentageLabel.setText(percent.format(tipPercentage));
我所有的错误都消失了。谢谢!@greglaular\u 85但是现在监听器实际上没有做任何事情,所以你可以完全删除它。我明白你的意思。但是我不知道如何实现它。我试着插入你建议的代码,但我无法让它运行。@greglaular\u 85我要说的是你可以删除lis剩下的只是向您展示如何使用绑定在文本中包含一个%
符号(这似乎是您在侦听器中尝试做的事情)。但我需要侦听器!此练习的重点是实际拥有一个侦听器谢谢,我没有意识到我试图显式设置tipPercentageLabel。我最终删除了最后一行代码:tipPercentageLabel.setText(percent.format(tipPercentage));
我所有的错误都消失了。谢谢!@greglaular\u 85但是现在监听器实际上没有做任何事情,所以你可以完全删除它。我明白你的意思。但是我不知道如何实现它。我试着插入你建议的代码,但我无法让它运行。@greglaular\u 85我要说的是你可以删除lis完全是tener。其余部分只是向您展示如何使用绑定在文本中包含%
符号(这是您在侦听器中似乎要做的)。但我需要侦听器!此练习的重点是实际拥有一个侦听器