代码之家  ›  专栏  ›  技术社区  ›  bryniek

Java FX绑定属性问题

  •  0
  • bryniek  · 技术社区  · 7 年前

    最近我开始学习Java FX。现在我正在练习属性绑定。目前我使用Inetellij IDE。我有一个我无法解决的问题,我不知道是什么错了。我在Person类中声明了一些属性,例如StringProperty,并创建了构造函数、getter和setter。我试图在initialize方法中绑定(或bindbiodictional)这些属性,但我不能。当我在代码中放置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文件看起来不错-我只是运行布局。

    1 回复  |  直到 7 年前
        1
  •  2
  •   Philip Vaughn    7 年前

    您没有绑定到绑定到属性本身的字符串。

    nameTextField.textProperty().bindBidirectional(person.namePropertyProperty());
    

    我想在这里提出一些个人意见;)IntelliJ是Java开发的最佳IDE。这是你今天的插头。