我在javafx中有一个对话框,它接受一些用户名和密码以及主机名来创建连接。 但Textfield值不会出现在后端。
private void popupCredentials(){
final Stage dialog = new Stage();
dialog.setTitle("Credentials");
dialog.initModality(Modality.NONE);
dialog.initOwner((Stage) tabpane.getScene().getWindow());
TextField textField = new TextField();
PasswordField passwordField = new PasswordField();
TextField textField1 = new TextField();
Label label2 = new Label("Enter Hostname");
label2.setFont(Font.font(null, FontWeight.BOLD, 14));
Label label = new Label("Enter Username");
label.setFont(Font.font(null, FontWeight.BOLD, 14));
Label label1 = new Label("Enter Password");
label1.setFont(Font.font(null, FontWeight.BOLD, 14));
Button button = new Button ("OK");
button.setFont(Font.font(null, FontWeight.BOLD, 14));
button.setStyle("-fx-text-fill: black;");
VBox vBox = new VBox(4);
VBox vBox1 = new VBox(1);
vBox1.getChildren().addAll(label,textField,label1,passwordField,label2,textField1);
vBox1.getChildren().add(button);
final String hostIP = textField1.getText();
final String userName = textField.getText();
final String password = passwordField.getText();
System.out.println(hostIP+"=="+userName+"=="+password);
button.addEventHandler(MouseEvent.MOUSE_CLICKED,
new EventHandler<MouseEvent>() {
@Override
public void handle(MouseEvent e) {
isSShConnected = sshManager.connect(hostIP, userName, password);
isSFTPConnected = sftpManager.connect(hostIP, userName, password);
if(isSFTPConnected && isSShConnected){
System.out.println("Connected");
}
}
});
HBox hBox = new HBox(2);
hBox.getChildren().addAll(vBox,vBox1);
hBox.setPadding(new Insets(15, 12, 15, 12));
hBox.setSpacing(10);
Scene dialogScene = new Scene(hBox, 200,180);
dialogScene.getStylesheets().add("/css/Style.css");
dialog.setScene(dialogScene);
dialog.show();
}
打印主机名,用户名和密码的行总是空白,我无法在这里找出问题。
答案 0 :(得分:2)
您在显示场景(TextField
)之前阅读了dialog.show();
s中的文字,这导致从每个String
读取空TextField
。您需要在用户提交输入后阅读文本,即在EventHandler
Button
事件中。您应该使用onAction
事件而不是btw。
final TextField textField = new TextField();
final PasswordField passwordField = new PasswordField();
final TextField textField1 = new TextField();
...
button.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent e) {
String hostIP = textField1.getText();
String userName = textField.getText();
String password = passwordField.getText();
isSShConnected = sshManager.connect(hostIP, userName, password);
isSFTPConnected = sftpManager.connect(hostIP, userName, password);
if(isSFTPConnected && isSShConnected){
System.out.println("Connected");
}
}
});