Javafx 简明教程
JavaFX - RadioButton
按钮是一个组件,它在按下时执行操作,如提交和登录。通常会在上面标记文本或图像以指定相应的操作。 radio button 是按钮的一种类型,其形状为圆形。它有两种状态,选中和未选中。下图显示了一组单选按钮 −
A button is a component, which performs an action like submit and login when pressed. It is usually labeled with a text or an image specifying the respective action. A radio button is a type of button, which is circular in shape. It has two states, selected and deselected. The figure below shows a set of radio buttons −
RadioButton in JavaFX
在 JavaFX 中, RadioButton 类表示单选按钮,它是名为 javafx.scene.control 的包的一部分。它是 ToggleButton 类的子类。每当按下或释放单选按钮时,就会生成操作。通常情况下,单选按钮使用切换组分组,你只能选择其中一个。我们可以使用 setToggleGroup() 方法将单选按钮设置为组。
In JavaFX, the RadioButton class represents a radio button which is a part of the package named javafx.scene.control. It is the subclass of the ToggleButton class. Action is generated whenever a radio button is pressed or released. Generally, radio buttons are grouped using toggle groups, where you can only select one of them. We can set a radio button to a group using the setToggleGroup() method.
要创建一个单选按钮,请使用以下构造函数 −
To create a radio button, use the following constructors −
-
RadioButton() − This constructor will create radio button without any label.
-
RadioButton(String str) − It is the parameterized constructor which constructs a radio button with the specified label text.
Example
以下是将创建 RadioButton 的 JavaFX 程序。将此代码保存在一个名为 JavafxRadiobttn.java 的文件中。
Following is the JavaFX program that will create a RadioButton. Save this code in a file with the name JavafxRadiobttn.java.
import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.RadioButton;
import javafx.scene.control.ToggleGroup;
import javafx.scene.layout.VBox;
import javafx.scene.paint.Color;
import javafx.stage.Stage;
public class JavafxRadiobttn extends Application {
@Override
public void start(Stage stage) {
//Creating toggle buttons
RadioButton button1 = new RadioButton("Java");
RadioButton button2 = new RadioButton("Python");
RadioButton button3 = new RadioButton("C++");
//Toggle button group
ToggleGroup group = new ToggleGroup();
button1.setToggleGroup(group);
button2.setToggleGroup(group);
button3.setToggleGroup(group);
//Adding the toggle button to the pane
VBox box = new VBox(5);
box.setFillWidth(false);
box.setPadding(new Insets(5, 5, 5, 50));
box.getChildren().addAll(button1, button2, button3);
//Setting the stage
Scene scene = new Scene(box, 400, 300, Color.BEIGE);
stage.setTitle("Toggled Button Example");
stage.setScene(scene);
stage.show();
}
public static void main(String args[]){
launch(args);
}
}
使用以下命令从命令提示符编译并执行已保存的 Java 文件 −
Compile and execute the saved Java file from the command prompt using the following commands −
javac --module-path %PATH_TO_FX% --add-modules javafx.controls JavafxRadiobttn.java
java --module-path %PATH_TO_FX% --add-modules javafx.controls JavafxRadiobttn
执行后,上述程序将生成以下输出 −
On executing, the above program will generate the following output −