JavaFX 中的 BigInteger 属性 是什么?
What is the BigInteger property in JavaFX?
如何在 JavaFX 中将 BigInteger
用作 属性,就像将 String
用作 属性 一样用作 SimpleStringProperty
?
JavaFX 中没有 BigIntegerProperty
(或任何 BigInteger
属性 实现),但您可以将 ObjectProperty<T>
用作 ObjectProperty<BigInteger>
:
ObjectProperty<BigInteger> bigIntProp = new SimpleObjectProperty<>();
这个属性存储了一个BigInteger
实例,可以被监听和绑定。
例子
使用此类型的 Application
,绑定到 TextArea
:
的文本
public class Main extends Application {
@Override
public void start(Stage primaryStage) throws Exception {
BorderPane root = new BorderPane();
ObjectProperty<BigInteger> bigIntProp = new SimpleObjectProperty<>();
bigIntProp.addListener((obs, oldval, newval) -> System.out.println(newval));
TextArea ta = new TextArea();
bigIntProp.bind(Bindings.createObjectBinding(() ->
(!ta.getText().isEmpty()) ? new BigInteger(ta.getText()) : BigInteger.ZERO
, ta.textProperty()));
root.setCenter(ta);
primaryStage.setScene(new Scene(root, 300, 275));
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
java中没有built-inBigInteger属性class像SimpleStringPropertyclass.
所以我为您创建了一个 SimpleBigInteger属性,它可以像那些 built-in 属性一样使用 classes。
import java.math.BigInteger;
import javafx.beans.property.SimpleObjectProperty;
/**
*
* This class provides a full implementation of a {@link Property} wrapping an
* arbitrary {@code BigInteger}.
*/
public class SimpleBigIntegerProperty extends SimpleObjectProperty<BigInteger>{
private static final Object DEFAULT_BEAN = null;
private static final String DEFAULT_NAME = "";
private final Object bean;
private final String name;
/**
* {@inheritDoc}
*/
@Override
public Object getBean() {
return bean;
}
/**
* {@inheritDoc}
*/
@Override
public String getName() {
return name;
}
/**
* The constructor of {@code BigIntegerProperty}
*/
public SimpleBigIntegerProperty() {
this(DEFAULT_BEAN, DEFAULT_NAME);
}
/**
* The constructor of {@code BigIntegerProperty}
*
* @param initialValue
* the initial value of the wrapped value
*/
public SimpleBigIntegerProperty(BigInteger initialValue) {
this(DEFAULT_BEAN, DEFAULT_NAME, initialValue);
}
/**
* The constructor of {@code BigIntegerProperty}
*
* @param bean
* the bean of this {@code BigIntegerProperty}
* @param name
* the name of this {@code BigIntegerProperty}
*/
public SimpleBigIntegerProperty(Object bean, String name) {
this.bean = bean;
this.name = (name == null) ? DEFAULT_NAME : name;
}
/**
* The constructor of {@code BigIntegerProperty}
*
* @param bean
* the bean of this {@code BigIntegerProperty}
* @param name
* the name of this {@code BigIntegerProperty}
* @param initialValue
* the initial value of the wrapped value
*/
public SimpleBigIntegerProperty(Object bean, String name, BigInteger initialValue) {
super(initialValue);
this.bean = bean;
this.name = (name == null) ? DEFAULT_NAME : name;
}
}
示例 1:
一个简单的例子,
SimpleBigIntegerProperty bigInteger = new SimpleBigIntegerProperty(BigInteger.valueOf(123456789));
System.out.println(bigInteger.getValue());
示例 2:
以ObservableList为例,
private final ObservableList<Person> data = FXCollections.observableArrayList(
new Person("Jon Skeet", BigInteger.valueOf(123456789)),
new Person("Michael Brown", BigInteger.valueOf(987654321))
);
人 class(具有人名和 age-in-seconds 属性)在哪里,
public class Person {
protected SimpleStringProperty personName;
protected SimpleBigIntegerProperty ageInSeconds;
public Person() {
this.personName = null;
this.ageInSeconds = null;
}
public Person(String person_name, BigInteger age_in_seconds) {
this.personName = new SimpleStringProperty(person_name);
this.ageInSeconds = new SimpleBigIntegerProperty(age_in_seconds);
}
public void setPersonName(String person_name) {
this.personName = new SimpleStringProperty(person_name);
}
public void setAgeInSeconds(BigInteger age_in_seconds) {
this.ageInSeconds = new SimpleBigIntegerProperty(age_in_seconds);
}
public String getPersonName() {
return this.personName.getValue();
}
public BigInteger getAgeInSeconds() {
return this.ageInSeconds.getValue();
}
}
如何在 JavaFX 中将 BigInteger
用作 属性,就像将 String
用作 属性 一样用作 SimpleStringProperty
?
JavaFX 中没有 BigIntegerProperty
(或任何 BigInteger
属性 实现),但您可以将 ObjectProperty<T>
用作 ObjectProperty<BigInteger>
:
ObjectProperty<BigInteger> bigIntProp = new SimpleObjectProperty<>();
这个属性存储了一个BigInteger
实例,可以被监听和绑定。
例子
使用此类型的 Application
,绑定到 TextArea
:
public class Main extends Application {
@Override
public void start(Stage primaryStage) throws Exception {
BorderPane root = new BorderPane();
ObjectProperty<BigInteger> bigIntProp = new SimpleObjectProperty<>();
bigIntProp.addListener((obs, oldval, newval) -> System.out.println(newval));
TextArea ta = new TextArea();
bigIntProp.bind(Bindings.createObjectBinding(() ->
(!ta.getText().isEmpty()) ? new BigInteger(ta.getText()) : BigInteger.ZERO
, ta.textProperty()));
root.setCenter(ta);
primaryStage.setScene(new Scene(root, 300, 275));
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
java中没有built-inBigInteger属性class像SimpleStringPropertyclass.
所以我为您创建了一个 SimpleBigInteger属性,它可以像那些 built-in 属性一样使用 classes。
import java.math.BigInteger;
import javafx.beans.property.SimpleObjectProperty;
/**
*
* This class provides a full implementation of a {@link Property} wrapping an
* arbitrary {@code BigInteger}.
*/
public class SimpleBigIntegerProperty extends SimpleObjectProperty<BigInteger>{
private static final Object DEFAULT_BEAN = null;
private static final String DEFAULT_NAME = "";
private final Object bean;
private final String name;
/**
* {@inheritDoc}
*/
@Override
public Object getBean() {
return bean;
}
/**
* {@inheritDoc}
*/
@Override
public String getName() {
return name;
}
/**
* The constructor of {@code BigIntegerProperty}
*/
public SimpleBigIntegerProperty() {
this(DEFAULT_BEAN, DEFAULT_NAME);
}
/**
* The constructor of {@code BigIntegerProperty}
*
* @param initialValue
* the initial value of the wrapped value
*/
public SimpleBigIntegerProperty(BigInteger initialValue) {
this(DEFAULT_BEAN, DEFAULT_NAME, initialValue);
}
/**
* The constructor of {@code BigIntegerProperty}
*
* @param bean
* the bean of this {@code BigIntegerProperty}
* @param name
* the name of this {@code BigIntegerProperty}
*/
public SimpleBigIntegerProperty(Object bean, String name) {
this.bean = bean;
this.name = (name == null) ? DEFAULT_NAME : name;
}
/**
* The constructor of {@code BigIntegerProperty}
*
* @param bean
* the bean of this {@code BigIntegerProperty}
* @param name
* the name of this {@code BigIntegerProperty}
* @param initialValue
* the initial value of the wrapped value
*/
public SimpleBigIntegerProperty(Object bean, String name, BigInteger initialValue) {
super(initialValue);
this.bean = bean;
this.name = (name == null) ? DEFAULT_NAME : name;
}
}
示例 1:
一个简单的例子,
SimpleBigIntegerProperty bigInteger = new SimpleBigIntegerProperty(BigInteger.valueOf(123456789));
System.out.println(bigInteger.getValue());
示例 2: 以ObservableList为例,
private final ObservableList<Person> data = FXCollections.observableArrayList(
new Person("Jon Skeet", BigInteger.valueOf(123456789)),
new Person("Michael Brown", BigInteger.valueOf(987654321))
);
人 class(具有人名和 age-in-seconds 属性)在哪里,
public class Person {
protected SimpleStringProperty personName;
protected SimpleBigIntegerProperty ageInSeconds;
public Person() {
this.personName = null;
this.ageInSeconds = null;
}
public Person(String person_name, BigInteger age_in_seconds) {
this.personName = new SimpleStringProperty(person_name);
this.ageInSeconds = new SimpleBigIntegerProperty(age_in_seconds);
}
public void setPersonName(String person_name) {
this.personName = new SimpleStringProperty(person_name);
}
public void setAgeInSeconds(BigInteger age_in_seconds) {
this.ageInSeconds = new SimpleBigIntegerProperty(age_in_seconds);
}
public String getPersonName() {
return this.personName.getValue();
}
public BigInteger getAgeInSeconds() {
return this.ageInSeconds.getValue();
}
}