<分区>
我遇到了一个奇怪的问题。我有一个包含一些项目的可编辑组合框。运行我的代码后,如果我在该 ComboBox 中键入内容并调用 getValue() 函数,那么它会给我 null值(value)。
这是我的代码 (thenewboston): 打包申请;
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;
public class Main 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 Demo");
button = new Button("Submit");
comboBox = new ComboBox<>();
comboBox.getItems().addAll(
"Good Will Hunting",
"St. Vincent",
"Blackhat"
);
comboBox.setPromptText("What is your favorite movie?");
comboBox.setEditable(true);
button.setOnAction(e -> printMovie());
//ComboBoxes also generate actions if you need to get value instantly
comboBox.setOnAction( e -> System.out.println("User selected " + comboBox.getValue()) );
VBox layout = new VBox(10);
layout.setPadding(new Insets(20, 20, 20, 20));
layout.getChildren().addAll(comboBox, button);
scene = new Scene(layout, 300, 250);
window.setScene(scene);
window.show();
}
private void printMovie(){
System.out.println(comboBox.getValue());
}
}
我正在使用 Windows 8.1、Eclipse Mars (4.5) 和 Java 1.8.0_66