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

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

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

问题

  1. 有没有类似于 ```Math.max(num1, num2)``` 这样的代码,但是用于比较两个 DoubleProperty
  2. 我目前正在尝试显示一个圆(继承自 Pane),它可以根据窗口的大小自动调整大小。我想尝试获取这两者之间的较小值,以设置圆的半径。
  3. ```java
  4. import javafx.application.Application;
  5. import javafx.scene.Scene;
  6. import javafx.scene.shape.Circle;
  7. import javafx.scene.layout.Pane;
  8. import javafx.stage.Stage;
  9. public class DisplayCircle extends Application {
  10. @Override
  11. public void start(Stage primaryStage) {
  12. Scene scene = new Scene(ResizableCircle(), 400, 300);
  13. primaryStage.setTitle("DisplayCircle");
  14. primaryStage.setScene(scene);
  15. primaryStage.show();
  16. }
  17. public class ResizableCircle extends Pane {
  18. public ResizableCircle() {
  19. Circle c = new Circle(getWidth()/2, getHeight()/2);
  20. c.centerXProperty().bind(widthProperty().subtract(10));
  21. c.centerYProperty().bind(heightProperty().subtract(10));
  22. // 需要帮助来设置半径并进行绑定
  23. getChildren().add(c);
  24. }
  25. }
  26. }
英文:

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.

  1. import javafx.application.Application;
  2. import javafx.scene.Scene;
  3. import javafx.scene.shape.Circle;
  4. import javafx.scene.layout.Pane;
  5. import javafx.stage.Stage;
  6. public class DisplayCircle extends Application {
  7. @Override
  8. public void start(Stage primaryStage) {
  9. Scene scene = new Scene(ResizableCircle(), 400, 300);
  10. primaryStage.setTitle("DisplayCircle");
  11. primaryStage.setScene(scene);
  12. primaryStage.show();
  13. }
  14. public class ResizableCircle extends Pane {
  15. public ResizableCircle() {
  16. Circle c = new Circle(getWidth()/2, getHeight()/2);
  17. c.centerXProperty().bind(widthProperty().subtract(10));
  18. c.centerYProperty().bind(heightProperty().subtract(10));
  19. // Need help setting the radius and binding it
  20. getChildren().add(c);
  21. }
  22. }

答案1

得分: 4

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

  1. DoubleProperty min = new SimpleDoubleProperty();
  2. DoubleProperty max = new SimpleDoubleProperty();
  3. min.bind(Bindings.min(widthProperty(), heightProperty()));
  4. 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:

  1. DoubleProperty min = new SimpleDoubleProperty();
  2. DoubleProperty max = new SimpleDoubleProperty();
  3. min.bind(Bindings.min(widthProperty(), heightProperty()));
  4. 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:

确定