【发布时间】:2018-02-16 15:59:29
【问题描述】:
最近我开始学习 Java FX。现在我正在练习属性绑定。我目前使用 Inetellij IDE。我有一个我无法解决的问题,我不明白出了什么问题。我声明了一些属性,例如。 Person 类中的 StringProperty 并创建了构造函数以及 getter 和 setter。我正在尝试在初始化方法中绑定(或绑定双向)这些属性,但我不能。当我将(如代码中的) person.getNameProperty 作为方法 bind 或 bindBidirectional 的参数时,会出现错误。谁能帮我解决这个问题?我应该怎么办?我在Netbeans IDE中看到了非常相似的代码,没有这样的问题。
主要
package sample;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;
public class Main extends Application {
@Override
public void start(Stage primaryStage) throws Exception{
Parent root = FXMLLoader.load(getClass().getResource("sample.fxml"));
primaryStage.setTitle("Hello World");
primaryStage.setScene(new Scene(root, 300, 350));
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
控制器:
import javafx.scene.control.TextField;
import java.net.URL;
import java.util.ResourceBundle;
public class Controller implements Initializable {
@FXML
private TextField nameTextField;
//other declarations
private Person person = new Person();
@Override
public void initialize(URL location, ResourceBundle resources) {
nameTextField.textProperty().bindBidirectional(person.getNameProperty());
}
}
班级人员:
package sample;
import javafx.beans.property.*;
import java.time.LocalDate;
/**
* Created by User on 2018-02-16.
*/
public class Person {
//person's name
private StringProperty nameProperty = new SimpleStringProperty();
//Other properties
//consturctor
public Person() {
}
public String getNameProperty() {
return nameProperty.get();
}
public StringProperty namePropertyProperty() {
return nameProperty;
}
public void setNameProperty(String nameProperty) {
this.nameProperty.set(nameProperty);
} }
FXML 文件看起来不错 - 我只是运行布局。
【问题讨论】:
-
"有一个错误" ....你想和我们分享吗?
标签: java javafx properties binding