javafx各种控件

一、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.
  • 9
    点赞
  • 24
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值