如何在fxml文件中使用Spring和JavaFx实现ToggleGroup?

6

拥有MainController

public class MainController {
    @FXML private RadioButton radioButton;
    @FXML private RadioButton radioButton2;

    @FXML public void addContact() {
        boolean b = radioButton.isSelected();
        boolean b2 = radioButton.isSelected();
    }
}

并且 main.fxml:

<AnchorPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="350.0" prefWidth="755.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="ru.habrahabr.ui.MainController">
      <TableView fx:id="table" editable="true" prefHeight="200.0" prefWidth="405.0" tableMenuButtonVisible="true" AnchorPane.bottomAnchor="50.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0">
          <columnResizePolicy>
              <TableView fx:constant="CONSTRAINED_RESIZE_POLICY" />
          </columnResizePolicy>
      </TableView>

      <HBox alignment="CENTER" layoutX="21.0" layoutY="207.0" prefHeight="50.0" prefWidth="200.0" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="10.0" AnchorPane.rightAnchor="10.0">
            <RadioButton fx:id="radioButton" text="Male">
               <HBox.margin>
                  <Insets right="3.0"/>
               </HBox.margin>
            </RadioButton>

            <RadioButton fx:id="radioButton2" text="Female">
               <HBox.margin>
                  <Insets right="30.0"/>
                  <Insets bottom="10.0"/>
               </HBox.margin>
            </RadioButton>

            <Button minWidth="-Infinity" mnemonicParsing="false" onAction="#addContact" text="Add" />
      </HBox>
</AnchorPane>

一切都很好,但我需要将两个单选按钮组合在一起,而我找不到如何在main.fxml中实现类似ToggleGroup的解决方案。

2个回答

15

按照 Fabian 的回答,您可以在 FXML 中定义您的开关组,然后通过 toggleGroup 属性调用它。它的工作方式相同,但它要稍微简短一些。

<HBox ...>
    <fx:define>
        <ToggleGroup fx:id="group" />
    </fx:define>

    <RadioButton fx:id="radioButton" text="Male" toggleGroup="$group">
        <HBox.margin>
            <Insets right="3.0"/>
        </HBox.margin>
    </RadioButton>

    <RadioButton fx:id="radioButton2" text="Female" toggleGroup="$group">
        <HBox.margin>
            <Insets right="30.0" bottom="10.0"/>
        </HBox.margin>
    </RadioButton>

    <Button ... />
</HBox>

1
我一直喜欢这种语法,直到最近。我将SceneBuilder从11更新到16后,它无法加载带有fx:define ToggleGroup的文件,但使用Fabian的方法可以解决问题。 - Nebril
单选按钮的操作怎么样?如何在FXML中实现方法触发? - undefined

12

不仅可以在fxml中创建Node,还可以创建ToggleGroup。使用<fx:reference>标签,您可以使用现有的对象:

<?import javafx.scene.control.ToggleGroup?>

...
<RadioButton fx:id="radioButton" text="Male">
   <HBox.margin>
      <Insets right="3.0"/>
   </HBox.margin>
   <toggleGroup>
       <ToggleGroup fx:id="group"/>
   </toggleGroup>
</RadioButton>
<RadioButton fx:id="radioButton2" text="Female">
   <HBox.margin>
      <Insets right="30.0"/>
      <Insets bottom="10.0"/>
   </HBox.margin>
   <toggleGroup>
       <fx:reference source="group"/>
   </toggleGroup>
</RadioButton>
...

或者使用控制器的initialize方法来实现此目的:

@FXML
private void initialize() {
    ToggleGroup group = new ToggleGroup();
    radioButton.setToggleGroup(group);
    radioButton2.setToggleGroup(group);
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接