如何使用JavaFX创建ComboBox?
下面的示例演示 ComboBox的创建。
import javafx.application.Application;
import javafx.collections.ObservableList;
import javafx.geometry.Insets;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.control.ComboBox;
import javafx.scene.control.Label;
import javafx.stage.Stage;
import javafx.scene.layout.HBox;
import javafx.scene.paint.Color;
import javafx.scene.text.Font;
import javafx.scene.text.FontPosture;
import javafx.scene.text.FontWeight;
public class ComboBoxExample extends Application {
public void start(Stage stage) {
//Setting the label
Label label = new Label("Select Desired Course:");
Font font = Font.font("verdana", FontWeight.BOLD, FontPosture.REGULAR, 12);
label.setFont(font);
//Creating a combo box
ComboBox<String> combo = new ComboBox<String>();
//Setting the prompt text of the combo box
combo.setPromptText("Select Course");
//Getting the observable list of the combo box
ObservableList<String> list = combo.getItems();
//Adding items to the combo box
list.add("Java");
list.add("C++");
list.add("Python");
list.add("Big Data");
list.add("Machine Learning");
HBox hbox = new HBox(15);
//Setting the space between the nodes of a HBox pane
hbox.setPadding(new Insets(75, 150, 50, 60));
hbox.getChildren().addAll(label, combo);
//Creating a scene object
Scene scene = new Scene(new Group(hbox), 595, 280, Color.BEIGE);
stage.setTitle("Combo Box");
stage.setScene(scene);
stage.show();
}
public static void main(String args[]){
launch(args);
}
}输出结果
热门推荐
10 对患者生日祝福语简短
11 结婚祝福语简短装备
12 周岁祝福语学生文案简短
13 订婚领证祝福语简短精辟
14 导师获奖祝福语大全简短
15 新婚购房祝福语简短精辟
16 牛年祝福语简短的爱人
17 送芒果的祝福语简短
18 送给学长毕业祝福语简短