【问题标题】:How do I add a value to items in a ComboBox in JavaFX如何为 JavaFX 中的 ComboBox 中的项目添加值
【发布时间】:2016-11-16 22:50:54
【问题描述】:

如何为组合框中的项目添加值,以便当用户从 ComboBox 中选择项目时,我能够显示该项目的价格

例如。如果用户选择一种动物,我可以显示该动物的价格。这 用户选择dog然后我可以显示$45的价格。

public class comboBox extends Application {

    Stage window;
    Scene scene;
    Button button;
    ComboBox<String> comboBox;

    public static void main(String[] args) {
        launch(args);
    }

    @Override
    public void start(Stage primaryStage) throws Exception {
        window = primaryStage;
        window.setTitle("ComboBox");
        button = new Button("Submit");

        comboBox = new ComboBox<>();
        comboBox.getItems().addAll(
            "cat",
            "dog",
            "bird"
        );

        comboBox.setPromptText("Please select one");
        button.setOnAction(e -> printPrice());

        VBox layout = new VBox(10);
        layout.setPadding(new Insets(60, 60, 60, 60));
        layout.getChildren().addAll(comboBox, button);

        scene = new Scene(layout, 450, 350);
        window.setScene(scene);
        window.show();
    }

    private void printPrice(){
        System.out.println(comboBox.getValue());
    }
}

我已经尝试修复代码,这就是我得到的仍然有一些错误有人知道我做错了什么吗?

import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.ComboBox;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
import javafx.collections.FXCollections;

public class animals extends Application {

Stage window;
Scene scene;
Button button;
ComboBox<String> comboBox;




public static void main(String[] args) {
    launch(args);
}

@Override
public void start(Stage primaryStage) throws Exception {
    window = primaryStage;
    window.setTitle("ComboBox ");
    button = new Button("Submit");

    comboBox.setConverter(new StringConverter<Animal>() {
@Override
public String toString(Animal object) {
    return object.getName();
}

@Override
public Animal fromString(String string) {
    return null;
}
});


ComboBox<Animal> comboBox = new ComboBox<Animal>();
comboBox.setItems(FXCollections.observableArrayList(new Animal("Dog", 30.12),  new Animal("Cat", 23.23), new Animal("Bird", 15.0)));

comboBox.valueProperty().addListener((obs, oldVal, newVal) ->  System.out.println("Price of the " + newVal.getName() + " is : "  +  newVal.getPrice()));    }

VBox layout = new VBox(10);
    layout.setPadding(new Insets(60, 60, 60, 60));
    layout.getChildren().addAll(comboBox, button);

    scene = new Scene(layout, 500, 350);
    window.setScene(scene);
    window.show();

}

public class Animal {
private String name;
private Double price;

public Double getPrice() {
    return price;
}

public String getName() {
    return name;
}

public Animal(String name, Double price) {
    this.name = name;
    this.price = price;

}
}

另外,在用户选择动物后,如何在组合框下方显示价格?所以它会说“动物成本的价格”

【问题讨论】:

  • 请使用java命名约定

标签: java javafx combobox javafx-8


【解决方案1】:

您应该向ComboBox 提供一个数据模型,其中存储了动物的名称和价格,例如Animal 类的实例。

public class Animal {
    private String name;
    private Double price;

    public Double getPrice() {
        return price;
    }

    public String getName() {
        return name;
    }

    public Animal(String name, Double price) {
        this.name = name;
        this.price = price;
    }
}

然后在您的ComboBox 中,您可以显示这些Animal 实例:

ComboBox<Animal> comboBox = new ComboBox<Animal>();
comboBox.setItems(FXCollections.observableArrayList(
    new Animal("Dog", 30.12),
    new Animal("Cat", 23.23), 
    new Animal("Bird", 15.0)));

comboBox.valueProperty().addListener((obs, oldVal, newVal) -> 
    System.out.println("Price of the " + newVal.getName() + " is : " + newVal.getPrice()));

唯一剩下的就是在ComboBox 上显示动物的名字,而不是物体本身。为此,您可以使用例如StringConverter:

comboBox.setConverter(new StringConverter<Animal>() {
    @Override
    public String toString(Animal object) {
        return object.getName();
    }

    @Override
    public Animal fromString(String string) {
        return null;
    }
});

值改变时,输出如下:

Price of the Cat is : 23.23
Price of the Dog is : 30.12
Price of the Bird is : 15.0

MCVE:

public class Animals extends Application {
    private ComboBox<Animal> comboBox = new ComboBox<>();
    private Text textNamePrice = new Text();

    public static void main(String[] args) { launch(args); }

    @Override
    public void start(Stage primaryStage) throws Exception {

        comboBox.setConverter(new StringConverter<Animal>() {
            @Override
            public String toString(Animal object) {
                return object.getName();
            }

            @Override
            public Animal fromString(String string) {
                return null;
            }
        });

        comboBox.setItems(FXCollections.observableArrayList(new Animal("Dog", 30.12),
                new Animal("Cat", 23.23),
                new Animal("Bird", 15.0)));

        comboBox.valueProperty().addListener((obs, oldVal, newVal) -> {
            String selectionText = "Price of the " + newVal.getName() + " is : " + newVal.getPrice();

            System.out.println(selectionText);
            textNamePrice.setText(selectionText);
        });

        VBox layout = new VBox(10);
        layout.setPadding(new Insets(60, 60, 60, 60));
        layout.getChildren().addAll(comboBox, textNamePrice);

        Scene scene = new Scene(layout, 500, 350);
        primaryStage.setScene(scene);
        primaryStage.show();
    }

    public class Animal {
        private String name;
        private Double price;

        public Double getPrice() { return price; }

        public String getName() { return name; }

        public Animal(String name, Double price) {
            this.name = name;
            this.price = price;
        }
    }
}

【讨论】:

  • 谢谢我是否需要为 FXCollections 导入任何内容,因为我收到此错误找不到符号:FXCollections
  • 是的,你应该导入javafx.collections.FXCollections
  • 我现在有一个错误animals.java:52:错误:类Animal是公共的,应该在一个名为Animal.java public class Animal {的文件中声明
  • 然后按照错误提示做,创建一个 Animal.java 文件并复制类:)
  • 为您添加了完整的示例。
猜你喜欢
  • 2021-10-17
  • 1970-01-01
  • 1970-01-01
  • 2017-06-15
  • 2015-08-14
  • 1970-01-01
  • 2016-12-08
  • 2015-10-09
  • 2012-11-15
相关资源
最近更新 更多