ChoiceBox(选择框)

JavaFX ChoiceBox 控件使用户能够从预定义的选项列表中选择一个选项。JavaFX ChoiceBox 控件由 javafx.scene.control.ChoiceBox 类表示。这个 JavaFX ChoiceBox 教程将解释如何使用 ChoiceBox 类。

The JavaFX ChoiceBox control enables users to choose an option from a predefined list of choices. The JavaFX ChoiceBox control is represented by the class javafx.scene.control.ChoiceBox . This JavaFX ChoiceBox tutorial will explain how to use the ChoiceBox class.

创建一个 ChoiceBox

您只需通过创建 ChoiceBox 类的新实例来创建 ChoiceBox。这是一个 JavaFX ChoiceBox 实例化的示例:

You create a ChoiceBox simply by creating a new instance of the ChoiceBox class. Here is a JavaFX ChoiceBox instantiation example:

  1. ChoiceBox choiceBox = new ChoiceBox();

将选项添加到 ChoiceBox

您可以通过获取其选项集合并向其添加项目来将选项添加到 ChoiceBox。这是一个向 JavaFX ChoiceBox 添加选项的示例:

You can add choices to a ChoiceBox by obtaining its item collection and add items to it. Here is an example that adds choices to a JavaFX ChoiceBox:

  1. choiceBox.getItems().add("Choice 1");
  2. choiceBox.getItems().add("Choice 2");
  3. choiceBox.getItems().add("Choice 3");

向场景图中添加 ChoiceBox

要使 ChoiceBox 可见,您必须将其添加到场景图中。这意味着您必须将 ChoiceBox 添加到 Scene 对象或某些布局组件,然后将其附加到 Scene 对象。

To make a ChoiceBox visible you must add it to the scene graph. This means that you must add the ChoiceBox to a Scene object or to some layout component which is then attached to the Scene object.

下面是一个示例,展示了如何将 JavaFX ChoiceBox 添加到场景图中:

Here is an example showing how to add a JavaFX ChoiceBox to the scene graph:

  1. package com.jenkov.javafx.controls;
  2. import javafx.application.Application;
  3. import javafx.scene.Scene;
  4. import javafx.scene.control.ChoiceBox;
  5. import javafx.scene.layout.HBox;
  6. import javafx.stage.Stage;
  7. public class ChoiceBoxExperiments extends Application {
  8. @Override
  9. public void start(Stage primaryStage) throws Exception {
  10. primaryStage.setTitle("ChoiceBox Experiment 1");
  11. ChoiceBox choiceBox = new ChoiceBox();
  12. choiceBox.getItems().add("Choice 1");
  13. choiceBox.getItems().add("Choice 2");
  14. choiceBox.getItems().add("Choice 3");
  15. HBox hbox = new HBox(choiceBox);
  16. Scene scene = new Scene(hbox, 200, 100);
  17. primaryStage.setScene(scene);
  18. primaryStage.show();
  19. }
  20. public static void main(String[] args) {
  21. Application.launch(args);
  22. }
  23. }

运行此示例生成的应用程序将类似于以下内容:

The application resulting from running this example would look similar to this:

A JavaFX ChoiceBox displayed via the scene graph

读取选定的值

您可以通过 getValue() 方法读取 ChoiceBox 的选定值。如果未选择任何选项,则 getValue() 方法将返回 null。以下是调用 getValue() 的示例:

You can read the selected value of a ChoiceBox via its getValue() method. If no choice is selected, the getValue() method returns null. Here is an example of calling getValue():

  1. String value = (String) choiceBox.getValue();

侦听选择

可以通过其 setOnAction() 方法在 ChoiceBox 上设置一个动作侦听器来侦听 JavaFX ChoiceBox 中选择的更改。下面是在 ChoiceBox 上设置动作侦听器的示例,该侦听器读取在 ChoiceBox 中选择的值:

It is possible to listen for selection changes in a JavaFX ChoiceBox by setting an action listener on the ChoiceBox via its setOnAction() method. Here is an example of setting an action listener on a ChoiceBox which reads what value was selected in the ChoiceBox:

  1. ChoiceBox choiceBox = new ChoiceBox();
  2. choiceBox.getItems().add("Choice 1");
  3. choiceBox.getItems().add("Choice 2");
  4. choiceBox.getItems().add("Choice 3");
  5. choiceBox.setOnAction((event) -> {
  6. int selectedIndex = choiceBox.getSelectionModel().getSelectedIndex();
  7. Object selectedItem = choiceBox.getSelectionModel().getSelectedItem();
  8. System.out.println("Selection made: [" + selectedIndex + "] " + selectedItem);
  9. System.out.println(" ChoiceBox.getValue(): " + choiceBox.getValue());
  10. });