【问题标题】:JavaFX put RadioButtons into GridPaneJavaFX 将 RadioButtons 放入 GridPane
【发布时间】:2015-01-29 09:10:58
【问题描述】:

我有以下代码:

GridPane gp = new GridPane();

// filling GridPane with other nodes...

RadioButton maschio = new RadioButton("M");
RadioButton femmina = new RadioButton("F");
final ToggleGroup tg = new ToggleGroup();
maschio.setToggleGroup(tg);
femmina.setToggleGroup(tg);
gp.add(tg, 1, 3);

最后一行出现错误:ToggleGroup cannot be converted to Node

我能做什么?我也尝试过Vbox, Hbox,但没有成功。 尝试谷歌但没有找到解决方案。有什么建议吗?

【问题讨论】:

  • 添加了一个可能的解决方案

标签: javafx radio-button gridpane


【解决方案1】:
    ToggleGroup tg = new ToggleGroup();
    RadioButton male = new RadioButton("Male");
    male.setToggleGroup(tg);
    RadioButton female = new RadioButton("Female");
    female.setToggleGroup(tg);
    HBox box = new HBox(20, male,female);
    gp.add(box,1,3);

切换一个可以在选定和非选定状态之间切换的控件。此外,可以为 Toggle 分配一个 ToggleGroup,它管理所有分配的 Toggle,以便在任何时候只能选择 ToggleGroup 中的一个 Toggle。

【讨论】:

    【解决方案2】:

    我找到了以下解决方案:

        ToggleButton maschio = new RadioButton("M");
        ToggleButton femmina = new RadioButton("F");
        final ToggleGroup tg = new ToggleGroup();
        HBox rbContainer = new HBox(maschio, femmina);
        maschio.setToggleGroup(tg);
        femmina.setToggleGroup(tg);
        gp.add(rbContainer, 1, 3);
    

    没事吧?或者你有更好的解决方案?

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-08-07
    • 2021-03-17
    • 1970-01-01
    • 1970-01-01
    • 2016-07-31
    • 2016-03-02
    • 2017-01-20
    相关资源
    最近更新 更多