ComboBox与数字

时间:2016-03-15 19:33:22

标签: javafx combobox

如何制作一个包含4个数字的ComboBox? 例如:

玩家数量:[此处为ComboBox],有4个选项“1”,“2”,“3”和“4”作为答案。

我知道如何为字符串制作它:

package sample;

import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.layout.GridPane;
import javafx.stage.Stage;


/**
* Created by E on 15/03/2016.
*/
public class Main extends Application{
public static void main(String[] args) {
    launch(args);
}
final Label notification = new Label ();


@Override public void start(Stage stage) {
    stage.setTitle("ComboBoxSample");
    Scene scene = new Scene(new Group(), 250, 100);

    final ComboBox numbers = new ComboBox();
    numbers.getItems().addAll(
            "One",
            "Two",
            "Three",
            "Four"

    );

    GridPane grid = new GridPane();
    grid.setVgap(4);
    grid.setHgap(10);
    grid.setPadding(new Insets(5, 5, 5, 5));
    grid.add(new Label("Number of players: "), 0, 0);
    grid.add(numbers, 1, 0);
    grid.add (notification, 1, 3, 3, 1);
    Group root = (Group)scene.getRoot();
    root.getChildren().add(grid);
    stage.setScene(scene);
    stage.show();
}
}

但我可以使用数字吗?

1 个答案:

答案 0 :(得分:1)

您可以使用组合框的通用参数来指定它应该使用“¯nt`s:

ComboBox<Integer> numbers = new ComboBox<Integer>();

然后你应该能够插入数字。