App下載
首頁javadata_bindingJavaFX - 如何創(chuàng)建用戶對(duì)象(POJO)frp,bindable屬性

JavaFX - 如何創(chuàng)建用戶對(duì)象(POJO)frp,bindable屬性

我們想知道如何創(chuàng)建用戶對(duì)象(POJO)frp,bindable屬性。
import javafx.beans.property.SimpleStringProperty;
import javafx.beans.property.StringProperty;

public class Main {
    public static void main(String[] args) {
        Student contact = new Student("FirstName", "LastName");
        StringProperty fname = new SimpleStringProperty();
        fname.bindBidirectional(contact.firstNameProperty());
        StringProperty lname = new SimpleStringProperty();
        lname.bindBidirectional(contact.lastNameProperty());

        System.out.println(fname.getValue() + " " + lname.getValue());
        System.out.println(contact.getFirstName() + " " + contact.getLastName());
        
        fname.setValue("new FirstName");
        lname.setValue("new LastName");

        System.out.println(fname.getValue() + " " + lname.getValue());
        System.out.println(contact.getFirstName() + " " + contact.getLastName());
    }
}

class Student {

    private SimpleStringProperty firstName = new SimpleStringProperty();
    private SimpleStringProperty lastName = new SimpleStringProperty();

    public Student(String fn, String ln) {
        firstName.setValue(fn);
        lastName.setValue(ln);
    }

    public final String getFirstName() {
        return firstName.getValue();
    }

    public StringProperty firstNameProperty() {
        return firstName;
    }

    public final void setFirstName(String firstName) {
        this.firstName.setValue(firstName);
    }

    public final String getLastName() {
        return lastName.getValue();
    }

    public StringProperty lastNameProperty() {
        return lastName;
    }

    public final void setLastName(String lastName) {
        this.lastName.setValue(lastName);
    }
}