欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

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

程序员文章站 2022-07-14 09:57:50
...

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

 

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

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

Figure 4-1 RadioButton Sample

(翻译)第十回 JavaFX2.0单选框Radio Button
            
    
    博客分类: 血泪体验翻译JavaFX2  
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

//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

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

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

(翻译)第十回 JavaFX2.0单选框Radio Button
            
    
    博客分类: 血泪体验翻译JavaFX2  
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

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

(翻译)第十回 JavaFX2.0单选框Radio Button
            
    
    博客分类: 血泪体验翻译JavaFX2  
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

(翻译)第十回 JavaFX2.0单选框Radio Button
            
    
    博客分类: 血泪体验翻译JavaFX2  
Description of "Figure 4-4 Setting Focus for the Selected Radio Button"