📜  JavaFX |带有示例的组合框

📅  最后修改于: 2022-05-13 01:55:48.714000             🧑  作者: Mango

JavaFX |带有示例的组合框

ComboBox 是 JavaFX 库的一部分。 JavaFX ComboBox 是一个简单 ComboBox 的实现,它显示了一个项目列表,用户最多可以从中选择一个项目,它继承了 ComboBoxBase 类。

组合框的构造函数:

  1. ComboBox() : 创建一个默认的空组合框
  2. ComboBox(ObservableList i) : 创建一个包含给定项目的组合框

常用方法:

MethodExplanation
getEditor()This method gets the value of the property editor
getItems()This method returns the items of the combo box
getVisibleRowCount()This method returns the value of the property visibleRowCount.
setItems(ObservableList v)This method Sets the items of the combo box
setVisibleRowCount(int v)This method sets the value of the property VisibleRowCount

下面的程序说明了 JavaFX 的 ComboBox 类:

  • 创建组合框并向其添加项目的程序:该程序创建一个名为 combo_box 的组合框,并使用 ChoiceBox(FXCollections.observableArrayList(week_days)) 向其中添加字符串列表。我们将组合框和标签(描述)添加到 tilepane(getChildren().add()函数)。我们将创建一个舞台(容器)并将瓷砖添加到场景中,并将场景添加到舞台中。我们将使用 show()函数显示舞台。
Java
// Java Program to create a combo Box and add items to it
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.layout.*;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.collections.*;
import javafx.stage.Stage;
import javafx.scene.text.Text.*;
import javafx.scene.paint.*;
import javafx.scene.text.*;
public class combo_box_1 extends Application {
 
    // Launch the application
    public void start(Stage stage)
    {
        // Set title for the stage
        stage.setTitle("creating combo box ");
 
        // Create a tile pane
        TilePane r = new TilePane();
 
        // Create a label
        Label description_label =
                     new Label("This is a combo box example ");
 
        // Weekdays
        String week_days[] =
                   { "Monday", "Tuesday", "Wednesday",
                                    "Thursday", "Friday" };
 
        // Create a combo box
        ComboBox combo_box =
                     new ComboBox(FXCollections
                                 .observableArrayList(week_days));
 
        // Create a tile pane
        TilePane tile_pane = new TilePane(combo_box);
 
        // Create a scene
        Scene scene = new Scene(tile_pane, 200, 200);
 
        // Set the scene
        stage.setScene(scene);
 
        stage.show();
    }
 
    public static void main(String args[])
    {
        // Launch the application
        launch(args);
    }
}


Java
// Java program to create a combo box and add event handler to it
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.layout.*;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.collections.*;
import javafx.stage.Stage;
import javafx.scene.text.Text.*;
import javafx.scene.paint.*;
import javafx.scene.text.*;
public class combo_box_2 extends Application {
 
    // Launch the application
    public void start(Stage stage)
    {
        // Set title for the stage
        stage.setTitle("creating combo box ");
 
        // Create a tile pane
        TilePane r = new TilePane();
 
        // Create a label
        Label description_label =
                         new Label("This is a combo box example ");
 
        // Weekdays
        String week_days[] =
                   { "Monday", "Tuesday", "Wednesday",
                                   "Thursday", "Friday" };
 
        // Create a combo box
        ComboBox combo_box =
                    new ComboBox(FXCollections
                              .observableArrayList(week_days));
 
        // Label to display the selected menuitem
        Label selected = new Label("default item selected");
 
        // Create action event
        EventHandler event =
                  new EventHandler() {
            public void handle(ActionEvent e)
            {
                selected.setText(combo_box.getValue() + " selected");
            }
        };
 
        // Set on action
        combo_box.setOnAction(event);
 
        // Create a tile pane
        TilePane tile_pane = new TilePane(combo_box, selected);
 
        // Create a scene
        Scene scene = new Scene(tile_pane, 200, 200);
 
        // Set the scene
        stage.setScene(scene);
 
        stage.show();
    }
 
    public static void main(String args[])
    {
        // Launch the application
        launch(args);
    }
}


输出:

  • 创建组合框并向其添加事件处理程序的程序:该程序创建一个名为 combo_box 的 ComboBox 并使用 (ChoiceBox(FXCollections.observableArrayList(week_days))) 向其添加字符串列表。我们将组合框和标签(描述)添加到 tilepane(getChildren().add()函数)。我们将创建一个舞台(容器)并将瓷砖添加到场景中,并将场景添加到舞台中。我们将使用 show()函数显示舞台。我们将添加一个事件处理程序事件来处理 combo_box 的事件,该事件会将所选标签的文本更改为所选项目。我们还将选择的标签添加到平铺窗格中。

Java

// Java program to create a combo box and add event handler to it
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.layout.*;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.collections.*;
import javafx.stage.Stage;
import javafx.scene.text.Text.*;
import javafx.scene.paint.*;
import javafx.scene.text.*;
public class combo_box_2 extends Application {
 
    // Launch the application
    public void start(Stage stage)
    {
        // Set title for the stage
        stage.setTitle("creating combo box ");
 
        // Create a tile pane
        TilePane r = new TilePane();
 
        // Create a label
        Label description_label =
                         new Label("This is a combo box example ");
 
        // Weekdays
        String week_days[] =
                   { "Monday", "Tuesday", "Wednesday",
                                   "Thursday", "Friday" };
 
        // Create a combo box
        ComboBox combo_box =
                    new ComboBox(FXCollections
                              .observableArrayList(week_days));
 
        // Label to display the selected menuitem
        Label selected = new Label("default item selected");
 
        // Create action event
        EventHandler event =
                  new EventHandler() {
            public void handle(ActionEvent e)
            {
                selected.setText(combo_box.getValue() + " selected");
            }
        };
 
        // Set on action
        combo_box.setOnAction(event);
 
        // Create a tile pane
        TilePane tile_pane = new TilePane(combo_box, selected);
 
        // Create a scene
        Scene scene = new Scene(tile_pane, 200, 200);
 
        // Set the scene
        stage.setScene(scene);
 
        stage.show();
    }
 
    public static void main(String args[])
    {
        // Launch the application
        launch(args);
    }
}

输出:

注意:以上程序可能无法在在线 IDE 中运行,请使用离线转换器。

参考: https://docs.oracle.com/javase/8/javafx/api/javafx/scene/control/ComboBox.html