您的位置:首页 > 编程语言 > Java开发

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

2011-11-02 00:28 429 查看
原文地址http://download.oracle.com/javafx/2.0/ui_controls/radio-button.htm#BABBJBDA

 

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

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

Figure 4-1 RadioButton Sample



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代码  



//A radio button with an empty string for its label   

RadioButton rb1 = new RadioButton();  

 //Setting a text label   

rb1.setText("Home");  

 //A radio button with the specified label  

 RadioButton rb2 = new RadioButton("Calendar");   

 

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


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

Example 4-2 Creating a Graphical Radio Button

Java代码  



Image image = new Image(getClass().getResourceAsStream("ok.jpg"));   

RadioButton rb = new RadioButton("Agree");   

rb.setGraphic(new ImageView(image));   

 

 


将Radio Button加入到组

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

Example 4-3 Creating a Group of Radio Buttons

Java代码  



final ToggleGroup group = new ToggleGroup();   

RadioButton rb1 = new RadioButton("Home");   

rb1.setToggleGroup(group);   

rb1.setSelected(true);   

RadioButton rb2 = new RadioButton("Calendar"); rb2.setToggleGroup(group);   

RadioButton rb3 = new RadioButton("Contacts");   

rb3.setToggleGroup(group);   

 

 

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

Figure 4-2 Three Radio Buttons Combined in a Group



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代码  



ImageView image = new ImageView();   

rb1.setUserData("Home");  

 rb2.setUserData("Calendar");  

 rb3.setUserData("Contacts");   

final ToggleGroup group = new ToggleGroup();   

group.selectedToggleProperty().addListener(   

new ChangeListener<Toggle>()  

{ public void changed(ObservableValue<? extends Toggle> ov,   

Toggle old_toggle, Toggle new_toggle)   

{ if (group.getSelectedToggle() != null)  

 { final Image image =   

new Image( getClass().getResourceAsStream( group.getSelectedToggle().getUserData().toString() + ".jpg" ) );  

 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



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



Description of "Figure 4-4 Setting Focus for the Selected Radio Button"

 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息