如何在JavaFX中的ComboBox中为项添加值

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

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

 public class comboBox extends Application { Stage window; Scene scene; Button button; ComboBox 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 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() { @Override public String toString(Animal object) { return object.getName(); } @Override public Animal fromString(String string) { return null; } }); ComboBox comboBox = new ComboBox(); 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; } } 

另外,在用户选择动物后,我如何才能在combobox下显示价格? 所以它会说“动物费用的价格”

您应该为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 comboBox = new ComboBox(); 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() { @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 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() { @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; } } }