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

huangapple go评论90阅读模式
英文:

Getting the max and min out of heightProperty() and widthProperty()

问题

有没有类似于 ```Math.max(num1, num2)``` 这样的代码,但是用于比较两个 DoubleProperty?
我目前正在尝试显示一个圆(继承自 Pane),它可以根据窗口的大小自动调整大小。我想尝试获取这两者之间的较小值,以设置圆的半径。

```java
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));

         // 需要帮助来设置半径并进行绑定
         
         getChildren().add(c);
      }
   }
}
英文:

Is there any code, like Math.max(num1, num2) but for comparing two DoubleProperty s ?
I am currently trying to displaying a circle (that extends from Pane) that can automatically resize itself according to the size of the window. I would like to try getting the smaller value between the two for setting the radius of the circle.

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

得分: 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()));
英文:

You can use Bindings.max(ObservableNumberValue op1, ObservableNumberValue op2) and Bindings.min(ObservableNumberValue op1, ObservableNumberValue op2) to bind an ObservableNumberValue to the minimum or maximum of two other observable values:

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

min.bind(Bindings.min(widthProperty(), heightProperty()));
max.bind(Bindings.max(widthProperty(), heightProperty()));

huangapple
  • 本文由 发表于 2020年4月6日 16:06:14
  • 转载请务必保留本文链接:https://go.coder-hub.com/61055416.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定