如何在 fxml 文件中实现 ToggleGroup,使用 Spring vs JavaFx

Val*_*kyi 5 spring javafx

MainController

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

    @FXML public void addContact() {
        boolean b = radioButton.isSelected();
        boolean b2 = radioButton.isSelected();
    }
}
Run Code Online (Sandbox Code Playgroud)

并且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>
Run Code Online (Sandbox Code Playgroud)

一切都很好,但我需要将两个单选按钮组合在一个组中,我找不到如何ToggleGroupmain.fxml.

000*_*laH 11

按照 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>
Run Code Online (Sandbox Code Playgroud)


fab*_*ian 10

在 fxml 中不仅可以创建Nodes,还ToggleGroup可以创建 s。使用<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>
...
Run Code Online (Sandbox Code Playgroud)

或者initialize,为此目的使用控制器的方法:

@FXML
private void initialize() {
    ToggleGroup group = new ToggleGroup();
    radioButton.setToggleGroup(group);
    radioButton2.setToggleGroup(group);
}
Run Code Online (Sandbox Code Playgroud)