带场景生成器的javafx无法从textField中获取文本
问题描述:
我是javafx的入门者。我正在尝试创建一个可以通过单击按钮存储输入的表单。但是,它有错误,我无法继续。 这里是我的代码:带场景生成器的javafx无法从textField中获取文本
在FXML文件:
<VBox prefHeight="300.0" prefWidth="400.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="ass.InputDataController">
<children>
<Label text=" Input" />
<TextField id="Input" fx:id="Input" onAction="#handleButtonAction" />
<Button fx:id="enter" onAction="#handleButtonAction" prefHeight="57.0" prefWidth="707.0" text="Enter" />
</children>
</VBox>
在InputDataController.java:
public class InputDataController implements Initializable {
@FXML
private TextField Input;
@FXML
private void handleButtonAction(ActionEvent event) {
String t = Input->getText();
}
@Override
public void initialize(URL url, ResourceBundle rb) {
// TODO
}
}
错误说:
error: incompatible types: String is not a functional interface
String t = Input->getText();
我试图实现另一个功能:
@FunctionalInterface
interface String {
String getText(TextField t);
}
但它仍然指出:
error: cannot find symbol
有没有解决这个问题的方法?如果我不需要实现一个功能接口,会更好!
答
正确的语法是
String t = Input.getText();
(在Java中,所有对象通过引用访问,因此没有必要对不同的非关联化运营.
和->
,引用和指针之间的区别在其他语言,您可以使用reference.member
访问对象的成员,Java中的->
运算符用于定义一个“lambda表达式”,该表达式可以分配给一个类型,该类型是一个功能接口(一个抽象方法接口),它是为什么编译器抱怨说你把这个分配给了一个不是的东西功能接口。)