我对getValue()
的实际返回值感到困惑,或者更准确地说:Eclipse似乎有这个问题。这是我的ComboBox:
ComboBox<Integer> intBox = new ComboBox<Integer>;
ObservableList<Integer> intList = FXCollections.observableArrayList();
我通过以下方式将ComboBox
填充为来自intList
的整数:
intBox.getItems().addAll(intList);
我也通过 setEditable(true)
将 ComboBox
设置为可编辑。
问题是,如果我尝试将 intBox.getValue()
的值存储在 Integer
或 int
变量中,在运行时会出现 "java.lang.ClassCastException: java.lang.String cannot be cast to java.lang.Integer" 错误。但如果我尝试将其存储到一个 String
中,Eclipse 给出了编译错误:"Type mismatch: cannot convert from Integer to String"。所以编译器告诉我它是一个整数,但我仍然无法将其存储在整数中,为什么呢?
我还尝试了各种解决方法,如 Integer.parseInt
、getValue().intValue()
和 Integer.toString()
,并将这些值存储在不同数据类型的各种变量中,但它们都给我带来了相同或类似的错误。