一、RadioButton Controls
(1)基础
RadioButton Controls经常以组的形式出现,它可以让用户进行选择。
radiobutton控件在radiobutton类中,需要导javafx.scene.control包。
RadioButton radio1 = new RadioButton("Choice 1");
注意,我们在RadioButton旁边显示的字符串作为参数传递给RadioButton 类的构造函数的参数。你可以选择省略字符串参数来创建一个没有文本的RadioButton控件。
RadioButton radio1 = new RadioButton();
RadioButton控件通常被分组在一个toggle(切换)组中。一个toggle组中只有一个RadioButton 控件可以被选中。点击一个RadioButton可以选择这个控件,并自动地取消这个toggle中已选择的其他RadioButton。因为一个toggle组中只有一个RadioButton 可以被选中,所以RadioButton控件被认为是互斥的。
(2)一组RadioButton
要创建toggle类,我们需要ToggleGroup class,它在javafx.scene.control package中。
ToggleGroup myToggleGroup = new ToggleGroup();
创建一个ToggleGroup对象后,你必须调用每个RadioButton控件的setToggleGroup方法 以将它们添加到ToggleGroup中。
// Create some RadioButtons.
RadioButton radio1 = new RadioButton("Option 1");
RadioButton radio2 = new RadioButton("Option 2");
RadioButton radio3 = new RadioButton("Option 3");
// Create a ToggleGroup.
ToggleGroup radioGroup = new ToggleGroup();
// Add the RadioButtons to the ToggleGroup.
radio1.setToggleGroup(radioGroup);
radio2.setToggleGroup(radioGroup);
radio3.setToggleGroup(radioGroup);
(3) RadioButton的其他方法
为了确定一个RadioButton是否被选中,你可以调用RadioButton类的isSelected方法。该方法返回一个布尔值,表示该RadioButton是否被选中。如果RadioButton被选中,该方法返回true。否则,它返回false。
if (radio1.isSelected())
{
// Code here executes if the radio
// button is selected.
}
除了在用户点击完之后选中RadioButton控件,还可以程序选择控件,比如有时候我们希望默认选择某个控件。我们可以用setSelected方法在代码中选择。 如果你向该方法传递true,RadioButton将被选中,就像用户点击它一样,如果你把false作为参数传给setSelected方法,RadioButton将被取消选择。
radio1.setSelected(true);
(4)例子
以我们之前的转换器为例:
import javafx.application.Application;
import javafx.stage.Stage;
import javafx.scene.Scene;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.geometry.Pos;
import javafx.geometry.Insets;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.control.Button;
import javafx.scene.control.RadioButton;
import javafx.scene.control.ToggleGroup;
import javafx.event.EventHandler;
import javafx.event.ActionEvent;
/**
Metric Converter application
*/
public class MetricConverter extends Application
{
// Fields
private TextField kiloTextField;
private Label resultLabel;
private RadioButton milesButton;
private RadioButton feetButton;
private RadioButton inchesButton;
public static void main(String[] args)
{
// Launch the application.
launch(args);
}
@Override
public void start(Stage primaryStage)
{
// Create a Label to display a prompt.
Label promptLabel = new Label("Enter a distance in kilometers:");
// Create a TextField for input.
kiloTextField = new TextField();
// Create the RadioButton controls.
milesButton = new RadioButton("Convert to Miles");
feetButton = new RadioButton("Convert to Feet");
inchesButton = new RadioButton("Convert to Inches");
// Select the milesButton control.
milesButton.setSelected(true);
// Add the RadioButton controls to a ToggleGroup.
ToggleGroup radioGroup = new ToggleGroup();
milesButton.setToggleGroup(radioGroup);
feetButton.setToggleGroup(radioGroup);
inchesButton.setToggleGroup(radioGroup);
// Create a Button to perform the conversion.
Button calcButton = new Button("Convert");
// Register the event handler.
calcButton.setOnAction(new CalcButtonHandler());
// Create an empty Label to display the result.
resultLabel = new Label();
// Put the promptLabel and the kiloTextField in an HBox.
HBox promptHBox = new HBox(10, promptLabel, kiloTextField);
// Put the RadioButtons in an HBox.
HBox radioHBox = new HBox(20, milesButton, feetButton,
inchesButton);
// Put everything in a VBox.