JavaFX单选按钮

单选按钮用于向用户提供各种选项。用户只能在所有选项中选择一个。单选按钮被选中或取消选中。它可以用于测验中选择题多的情况, 学生只需选择一个选项。
以下代码显示了如何从切换组中选择一个单选按钮。

package application; import javafx.application.Application; import javafx.scene.Scene; import javafx.scene.control.RadioButton; import javafx.scene.control.ToggleGroup; import javafx.scene.layout.VBox; import javafx.stage.Stage; public class RadioButtonTest extends Application { public static void main(String[] args) {launch(args); }@Overridepublic void start(Stage primaryStage) throws Exception { // TODO Auto-generated method stub ToggleGroup group = new ToggleGroup(); RadioButton button1 = new RadioButton("option 1"); RadioButton button2 = new RadioButton("option 2"); RadioButton button3 = new RadioButton("option 3"); RadioButton button4 = new RadioButton("option 4"); button1.setToggleGroup(group); button2.setToggleGroup(group); button3.setToggleGroup(group); button4.setToggleGroup(group); VBox root=new VBox(); root.setSpacing(10); root.getChildren().addAll(button1, button2, button3, button4); Scene scene=new Scene(root, 400, 300); primaryStage.setScene(scene); primaryStage.setTitle("Radio Button Example"); primaryStage.show(); }}

【JavaFX单选按钮】输出:
JavaFX单选按钮

文章图片

    推荐阅读