英文:
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()));
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论