我正面臨一個奇怪的問題。我有一個可編輯的組合框與一些項目。運行我的代碼,如果我輸入一些東西到該組合框,並呼籲的getValue()之後函數,那麼它給了我空值。JAVAFX可編輯組合框給出空值
這裏是我的代碼(新波士頓): 包應用程序;
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的火星(4.5)和Java 1.8.0_66
假設點擊提交按鈕,當你得到空,我關閉了一個問題,[HTTP ://stackoverflow.com/q/32620739/203657](重複) - 如果我的假設不正確,請編輯 – kleopatra