从heightProperty()和widthProperty()中获取最大值和最小值

时间:2020-04-06 08:06:14

标签: java javafx java-8

是否有任何代码,例如Math.max(num1, num2),但用于比较两个DoubleProperty? 我目前正在尝试显示一个圆(从Pane延伸),该圆可以根据窗口的大小自动调整大小。我想尝试在两者之间取较小的值来设置圆的半径。

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.shape.Circle;
import javafx.scene.layout.Pane;
import javafx.stage.Stage;

public class DisplayCircle extends Application {
   @Override
   public void start(Stage primaryStage) {
      Scene scene = new Scene(ResizableCircle(), 400, 300);
      primaryStage.setTitle("DisplayCircle");
      primaryStage.setScene(scene);
      primaryStage.show();
   }

   public class ResizableCircle extends Pane {
      public ResizableCircle() {
         Circle c = new Circle(getWidth()/2, getHeight()/2);
         c.centerXProperty().bind(widthProperty().subtract(10));
         c.centerYProperty().bind(heightProperty().subtract(10));


         // Need help setting the radius and binding it


         getChildren().add(c);
   }
}

1 个答案:

答案 0 :(得分:4)

您可以使用Bindings.max(ObservableNumberValue op1, ObservableNumberValue op2)Bindings.min(ObservableNumberValue op1, ObservableNumberValue op2)ObservableNumberValue绑定到其他两个可观察值的最小值或最大值:

DoubleProperty min = new SimpleDoubleProperty();
DoubleProperty max = new SimpleDoubleProperty();

min.bind(Bindings.min(widthProperty(), heightProperty()));
max.bind(Bindings.max(widthProperty(), heightProperty()));
相关问题