如何使用按钮内的其他语句写入Txt文件

时间:2017-04-29 18:03:41

标签: java if-statement javafx scenebuilder

修复了问题,感谢所有帮助c:

2 个答案:

答案 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以选择匹配的那个并不是太环节。