修复了问题,感谢所有帮助c:
答案 0 :(得分:0)
要跟踪选定的iceCream,只需添加新字段iceCream
即可。并在选择后为其指定值。对你toppingsCost
执行相同的操作:
public class FXMLDocumentController implements Initializable {
private String iceCream;
private double toppingsCost;
....
private void handleButtonSaving(ActionEvent event) {
if(vanilla.isSelected()) {
iceCream = "Vanilla";
System.out.println(iceCream);
}
if(chocolate.isSelected()) {
iceCream = "Chocolate";
System.out.println(iceCream);
}
...
private void handleButtonCalculateCost(ActionEvent event) {
double myTotal = 0.0;
myTotal += retrieveIceCreamCost();
toppingsCost = retrieveToppingsCost();
myTotal += toppingsCost;
...
答案 1 :(得分:0)
考虑RadioButton
如何按ToggleGroup
进行分组。在代码中:
final ToggleGroup icecream = new ToggleGroup();
RadioButton rb1 = new RadioButton("Vanilla");
rb1.setToggleGroup(icecream);
rb1.setSelected(true);
RadioButton rb2 = new RadioButton("Chocolate");
rb2.setToggleGroup(icecream);
RadioButton rb3 = new RadioButton("Strawberry");
rb3.setToggleGroup(icecream);
您可以通过ToggleGroup组件处理所有选择。
RadioButton selected = (RadioButton) icecream.getSelectedToggle();
ObservableList<Toggle> radioButtons = icecream.getToggles();
写下selected
单选按钮的标签(说&#34; Vanilla&#34;),然后阅读&#34; Vanilla&#34;并循环遍历所有radioButtons
以选择匹配的那个并不是太环节。