拥有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
的解决方案。