【问题标题】:Getting the max and min out of heightProperty() and widthProperty()从 heightProperty() 和 widthProperty() 中获取最大值和最小值
【发布时间】:2020-04-06 08:06:14
【问题描述】:

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

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);
   }
}

【问题讨论】:

标签: java javafx java-8


【解决方案1】:

您可以使用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()));

【讨论】:

猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-11-25
  • 1970-01-01
  • 2020-08-16
相关资源
最近更新 更多