代码之家  ›  专栏  ›  技术社区  ›  FunctionUndefined

数组到组合框-JavaFx

  •  1
  • FunctionUndefined  · 技术社区  · 2 年前

    我正在做一个学校的项目,我试图把一个字符串数组直接放入一个组合框中,结果发现一个错误。

    错误:java:不兼容的类型:java。lang.String[]无法转换为javafx。收藏。观察者<JAVAlang.String>

    ComboBox comboDecks;
    ComboBox comboTrucks;
    ComboBox comboWheels;
    private final String[] deckArray = {"The Master Thrasher - $60 ", "The Dictator - $ 45", "The Street King - $ 50"};
    private final String[] trucksArray = {"7.75-inch axle - $35 ", "8-inch axle - $40 ", "8.5-inch axle - $45 "};
    private final String[] wheelsArray = {"51mm - $20", "55mm - $22", "58mm - $24", "61mm - $28"};
    
    
    @Override
    public void start(Stage stage) throws IOException {
    
        title1 = new Text("Skateboard Designer");
        title2 = new Text("Choose from the following Decks, Trucks, Wheels, and Accessories to design and price your own skateboard!");
        lblDecks = new Label("Decks: ");
        lblTrucks = new Label("Trucks: ");
        lblWheels = new Label("Wheels: ");
        lblAccess = new Label("Accessories: ");
        comboDecks = new ComboBox(deckArray);
        comboTrucks = new ComboBox(trucksArray);
        comboWheels = new ComboBox(wheelsArray);
    
    1 回复  |  直到 2 年前
        1
  •  1
  •   Giovanni Contreras    2 年前

    那个 ComBoBox 构造函数需要一个 ObservableList 作为论据。 如果要添加字符串数组,请使用 addAll() 方法

    comboDecks = new ComboBox();
    comboDecks.getItems.addAll(deckArray)
    comboTrucks = new ComboBox();
    comboTrucks .getItems.addAll(trucksArray)
    comboWheels = new ComboBox();
    comboWheels.getItems.addAll(wheelsArray)