Properties(属性)

details 目录

JavaFX 属性(Properties) 是 JavaFX 控件的一种特殊类型的成员变量。JavaFX 属性通常用于表示控件的一些属性,例如 X 和 Y 位置、宽度和高度、文本、子节点和 JavaFX 控件的其它核心属性。您可以将更改侦听器附加到 JavaFX 属性,以便其它组件可以在属性值更改时收到通知,并且您可以将属性相互绑定,以便当一个属性值更改时,另一个也会更改。在这个 JavaFX 属性教程中,我将解释 JavaFX 属性是如何工作的,以及如何使用它们。

A JavaFX Property is a special kind member variable of JavaFX controls. JavaFX properties are typically used to contain control properties such as X and Y position, width and height, text, children and other central properties of JavaFX controls. You can attach change listeners to JavaFX properties so other components can get notified when the value of the property changes, and you can bind properties to each other so when one property value changes, so does the other. In this JavaFX property tutorial I will explain how JavaFX properties work, and how to use them.

Property 示例

这是一个 JavaFX GUI 示例,展示了如何访问 窗格(Pane)widthPropertyprefWidthProperty 属性,以及向这两个属性附加更改侦听器。请注意,其中一个更改侦听器的实现方法为使用 Java 匿名(实现 / 子)类,另一个是使用 Java Lambda 表达式。这只是为了向您展示实现将事件侦听器附加到 JavaFX 属性的两种不同方法。

Here is a JavaFX GUI example showing how to access the widthProperty and prefWidthProperty of a Pane, as well as adding a change listener to both. Notice how one of the change listeners is implemented as an anonymous Java class, and the other as a Java Lambda Expression. This is just to show you two different ways of achieving the same goal of attaching an event listener to a JavaFX property.

  1. import javafx.application.Application;
  2. import javafx.beans.property.DoubleProperty;
  3. import javafx.beans.property.ReadOnlyDoubleProperty;
  4. import javafx.beans.value.ChangeListener;
  5. import javafx.beans.value.ObservableValue;
  6. import javafx.scene.Scene;
  7. import javafx.scene.layout.Pane;
  8. import javafx.stage.Stage;
  9. public class PropertyExample extends Application {
  10. public static void main(String[] args) {
  11. launch(args);
  12. }
  13. public void start(Stage primaryStage) {
  14. Pane pane = new Pane();
  15. ReadOnlyDoubleProperty widthProperty = pane.widthProperty();
  16. widthProperty.addListener( new ChangeListener<Number> (){
  17. @Override
  18. public void changed(
  19. ObservableValue<? extends Number> observableValue,
  20. Number oldVal, Number newVal) {
  21. System.out.println("widthProperty changed from "
  22. + oldVal.doubleValue() + " to " + newVal.doubleValue());
  23. }
  24. }); // 使用 Java 匿名实现类的方式附加更改侦听器
  25. DoubleProperty prefWidthProperty = pane.prefWidthProperty();
  26. prefWidthProperty.addListener(
  27. (ObservableValue<? extends Number> prop,
  28. Number oldVal, Number newVal) -> {
  29. System.out.println("prefWidthProperty changed from "
  30. + oldVal.doubleValue() + " to " + newVal.doubleValue());
  31. }); // 使用 Java Lambda 表达式的方式附加更改侦听器
  32. prefWidthProperty.set(123);
  33. Scene scene = new Scene(pane, 1024, 800, true);
  34. primaryStage.setScene(scene);
  35. primaryStage.setTitle("2D Example");
  36. primaryStage.show();
  37. }
  38. }

当代码 prefWidthProperty.set(123); 执行时,将调用 prefWidthProperty 属性的更改侦听器。此外,每次调整 UI 大小时,窗格大小也会改变,所以会调用 widthProperty 属性的更改侦听器。

When the instruction prefWidthProperty.set(123); is called, the prefWidthProperty change listener will get called. Additionally, every time the UI is resized then the Pane is resized too, and the widthProperty change listener will get called.