Java 爪哇语;意外返回值“;在返回int时为int
我在另一个文件中执行这个类,java告诉我Java 爪哇语;意外返回值“;在返回int时为int,java,class,return,Java,Class,Return,我在另一个文件中执行这个类,java告诉我 "Unexpected return value", 我找了又找,看不出我做错了什么。我是一个巨大的Java Noob,因此任何帮助都将不胜感激 import javafx.stage.*; import javafx.scene.*; import javafx.scene.layout.*; import javafx.scene.control.*; import javafx.geometry.*; public class PopUpB
"Unexpected return value",
我找了又找,看不出我做错了什么。我是一个巨大的Java Noob,因此任何帮助都将不胜感激
import javafx.stage.*;
import javafx.scene.*;
import javafx.scene.layout.*;
import javafx.scene.control.*;
import javafx.geometry.*;
public class PopUpBox {
public static void display(String title, String message) {
Stage window = new Stage();
TextField textField = new TextField();
//Block events to other windows
window.initModality(Modality.APPLICATION_MODAL);
window.setTitle(title);
window.setMinWidth(250);
//Make a label that closes the window
Label label = new Label();
label.setText(message);
Button closeButton = new Button("Done");
closeButton.setOnAction(e -> {
Integer test = Integer.parseInt(textField.getText());
//return test;
System.out.println(test.getClass());
window.close();
}
);
VBox layout = new VBox(10);
layout.getChildren().addAll(textField, closeButton);
layout.setAlignment(Pos.CENTER);
//Display window and wait for it to be closed before returning
Scene scene = new Scene(layout);
window.setScene(scene);
window.showAndWait();
}
}
检查您正在使用的方法的文档: 只有一个EventHandler类型的参数。(按钮从ButtonBase继承setOnAction) 是具有单个方法的接口
无效句柄(T事件)
这就是lambda表达式所实现的。这是一个void方法,因此您无法从中返回int值。ActionHandler\handle
是一个void方法,您无法从该方法返回值(这是我不喜欢lambda表达式的原因之一,它们对没有经验的开发人员隐藏了这些重要细节),从中您将获得“意外的返回值”。如果您遇到异常,请共享stacktrace或共享您拥有的任何日志。