(翻译)第十回 JavaFX2.0单选框Radio Button

原文地址http://download.oracle.com/javafx/2.0/ui_controls/radio-button.htm#BABBJBDA

 

RadioButton类是ToggleButton类的一个专业实现。一个单选按钮控件可以被选中和取消选中。典型的单选按钮是被放置在一个组里面,组里每次只能有一个按钮被选中。这种行为将它们和开关按钮区别开了,因为一个组中的所有开关按钮能同时被取消选中。

Figure 4-1是三幅RadioButton例子的截图,里面的三个单选按钮在同一个组中。

Figure 4-1 RadioButton Sample

A group of three radio buttons
Description of "Figure 4-1 RadioButton Sample"

通过研习下文能够了解更多关于在应用中实现单选按钮的信息。

 

 

创建Radio Button

RadioButton类位于JavaFX SDK的javafx.scene.control包中,提供了两个创建单选按钮的构造方法。Example 4-1是创建两个单选按钮。无参数构造方法用来创建rb1,它的标题通过setText方法设置。而rb2的标题直接定义在相应的构造方法中。

 Example 4-1 Creating Radio Buttons

Java代码   收藏代码
  1. //A radio button with an empty string for its label   
  2. RadioButton rb1 = new RadioButton();  
  3.  //Setting a text label   
  4. rb1.setText("Home");  
  5.  //A radio button with the specified label  
  6.  RadioButton rb2 = new RadioButton("Calendar");   

 

你可以通过为setSelected方法指定true值来明确地让一个单选按钮是选中状态。如果你想要检查一个特定的单选按钮是否被用户选中了,使用isSelected方法。

由于 RadioButton 类继承了Labeled 类,所以你不仅可以为其指定文本标题,还可以是图片。使用setGraphic方法来指定一副图片。Example 4-2演示了如何在应用中实现带图像的单选按钮。

Example 4-2 Creating a Graphical Radio Button

Java代码   收藏代码
  1. Image image = new Image(getClass().getResourceAsStream("ok.jpg"));   
  2. RadioButton rb = new RadioButton("Agree");   
  3. rb.setGraphic(new ImageView(image));   

 

 

将Radio Button加入到组

单选按钮的典型用法是在组中使用来提供几个互斥选项。ToggleGroup对象为所有的单选按钮提供了引用来关联自身,并且管理单选按钮来实现每次只能有一个被选中。Example 4-3创建了一个开关按钮组、三个单选按钮,把每个按钮都加入到组中,并指定了在程序启动后哪个要被选中。

Example 4-3 Creating a Group of Radio Buttons

Java代码   收藏代码
  1. final ToggleGroup group = new ToggleGroup();   
  2. RadioButton rb1 = new RadioButton("Home");   
  3. rb1.setToggleGroup(group);   
  4. rb1.setSelected(true);   
  5. RadioButton rb2 = new RadioButton("Calendar"); rb2.setToggleGroup(group);   
  6. RadioButton rb3 = new RadioButton("Contacts");   
  7. rb3.setToggleGroup(group);   

 

 

当这些单选按钮被它们的布局管理器添加到应用的内容上以后,输出应该类似于Figure 4-2.

Figure 4-2 Three Radio Buttons Combined in a Group

Three radio buttons: Home, Calendar, Contacts
Description of "Figure 4-2 Three Radio Buttons Combined in a Group"

 

处理Radio Button事件

当组中的某个单选按钮被选中时程序会处理该行为。研读Example 4-4中的代码块来了解怎么根据哪个单选按钮被选中来改变图标。

 

Example 4-4 Processing Action for Radio Buttons

Java代码   收藏代码
  1. ImageView image = new ImageView();   
  2. rb1.setUserData("Home");  
  3.  rb2.setUserData("Calendar");  
  4.  rb3.setUserData("Contacts");   
  5. final ToggleGroup group = new ToggleGroup();   
  6. group.selectedToggleProperty().addListener(   
  7. new ChangeListener<Toggle>()  
  8. public void changed(ObservableValue<? extends Toggle> ov,   
  9. Toggle old_toggle, Toggle new_toggle)   
  10. if (group.getSelectedToggle() != null)  
  11.  { final Image image =   
  12. new Image( getClass().getResourceAsStream( group.getSelectedToggle().getUserData().toString() + ".jpg" ) );  
  13.  icon.setImage(image); } } });   

 

 

比如,当rb3被选中时,getSelectedToggle方法返回"rb3,"getUserData方法返回"Contacts"。因此,getResourceAsStream方法接收了"Contacts.jpg."Figure 4-1是应用的输出。

 

为Radio Button请求焦点

在单选按钮组中,默认第一个按钮具有焦点。当你为组中的第二个单选按钮使用setSelected 方法后,你期望的结果是像Figure 4-3.

Figure 4-3 Default Focus Settings

The second radio button is selected.
Description of "Figure 4-3 Default Focus Settings"

第二个按钮被选中了,但焦点依然在第一个按钮上。使用requestFocus函数可以改变焦点位置,见Example 4-5.

Example 4-5 Requesting Focus for the Second Radio Button

rb2.setSelected(true); rb2.requestFocus();
 

这样,代码产生的结果如Figure 4-4.

Figure 4-4 Setting Focus for the Selected Radio Button

The second radio button gets focused.
Description of "Figure 4-4 Setting Focus for the Selected Radio Button"

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
JavaFXRadioButton是一种用户界面控件,用于在多个选项中选择一个选项。它通常与其他RadioButton合在一起,形成一个单选按钮。当用户选择其中一个RadioButton时,其他RadioButton将自动取消选择。 JavaFXRadioButton具有以下特点: 1. 可以通过设置文本或图像来标识每个RadioButton。 2. 可以使用ToggleGroup将多个RadioButton合在一起,确保它们之间是互斥的,即只能选择其中一个。 3. 可以通过设置选中状态来确定哪个RadioButton被选中。 4. 可以通过添加事件监听器来响应RadioButton的选择状态变化。 以下是一个简单的JavaFX RadioButton的示例代码: ```java 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 RadioButtonExample extends Application { @Override public void start(Stage primaryStage) { RadioButton radioButton1 = new RadioButton("Option 1"); RadioButton radioButton2 = new RadioButton("Option 2"); RadioButton radioButton3 = new RadioButton("Option 3"); ToggleGroup toggleGroup = new ToggleGroup(); radioButton1.setToggleGroup(toggleGroup); radioButton2.setToggleGroup(toggleGroup); radioButton3.setToggleGroup(toggleGroup); VBox vbox = new VBox(radioButton1, radioButton2, radioButton3); Scene scene = new Scene(vbox, 200, 200); primaryStage.setScene(scene); primaryStage.show(); } public static void main(String[] args) { launch(args); } } ``` 这个示例创建了三个RadioButton,并将它们添加到一个垂直布局的VBox中。然后,通过ToggleGroup将它们合在一起。最后,将VBox添加到场景中,并显示在舞台上。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值