[英]I'm having a hard time getting some elements to resize in JavaFX
I have a series of elements I wish to resize dynamically. 我有一系列要动态调整大小的元素。
I have a small StackPane
set within a larger AnchorPane
. 我在较大的
AnchorPane
设置了一个小的StackPane
。 The AnchorPane
resizes based on the window size changing. AnchorPane
根据窗口大小的变化AnchorPane
调整大小。 I am trying to resize the StackPane
to stay the same size relative to the size it was before the AnchorPane
resized. 我正在尝试调整
StackPane
的大小,以使其与AnchorPane
调整大小之前的大小保持AnchorPane
。
Basically, if my AnchorPane
is 1000px and my Stackpane
is 100px, if the AnchorPane
enlarges to 1200px, my StackPane
should be 120px. 基本上,如果我的
AnchorPane
是AnchorPane
,我的Stackpane
是100px,如果AnchorPane
放大到1200px,则我的StackPane
应该是120px。
ChangeListener parentResized = new ChangeListener<Number>() {
@Override
public void changed(ObservableValue<? extends Number> oe, Number oldVal, Number newVal) {
double diffBetween = sc.getWidth() / parent.getWidth();
double toAdd = (newVal.doubleValue() - oldVal.doubleValue()) * diffBetween;
sc.setPrefWidth(sc.getWidth() + toAdd);
}
};
I cannot work out how to do this using my ChangeListener
; 我
ChangeListener
如何使用我的ChangeListener
做到这一点; the oldVal
/ newVal
updates very quickly. oldVal
/ newVal
更新非常快。
I am getting an odd issue where it seems to resize properly upon widening, however if I drag slowly enough when narrowing, the element doesn't resize at all. 我遇到一个奇怪的问题,它似乎在扩大时会适当调整大小,但是如果在缩小时拖动得足够慢,该元素根本不会调整大小。
You can use Binding for these kind of requirements. 您可以将Binding用于此类要求。 Bind the height and width of the
StackPane
to that of the AnchorPane
. 将
StackPane
的高度和宽度绑定到AnchorPane
的高度和宽度。
For binding the StackPane's
width and height to exactly 1/10th of that of AnchorPane, you can use the following : 为了将
StackPane's
宽度和高度绑定到StackPane's
宽度的正好1/10,可以使用以下命令:
stackPane.prefHeightProperty().bind(anchorPane.heightProperty().divide(10));
stackPane.prefWidthProperty().bind(anchorPane.widthProperty().divide(10));
I have created a MCVE where the StackPane's height and width is 1/10th of that of the AnchorPane. 我创建了一个MCVE,其中StackPane的高度和宽度是AnchorPane的高度和宽度的1/10。 There are labels which show the current size of both the layouts.
有些标签显示了两种布局的当前大小。
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.StackPane;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
public class Main extends Application {
@Override
public void start(Stage primaryStage) {
// Labels for AnchorPane's size
Label apHeight = new Label();
Label apWidth = new Label();
// Labels for StackPane's size
Label spHeight = new Label();
Label spWidth = new Label();
AnchorPane anchorPane = new AnchorPane();
anchorPane.setStyle("-fx-background-color : CORNSILK");
anchorPane.setPrefSize(500, 500);
StackPane stackPane = new StackPane();
stackPane.setStyle("-fx-background-color : AQUA");
stackPane.getChildren().add(new VBox(spWidth, spHeight));
// Add labels and StackPane to AnchorPane
VBox box = new VBox(apHeight, apWidth);
anchorPane.getChildren().addAll(box, stackPane);
// Set StackPane location in AnchorPane
AnchorPane.setTopAnchor(stackPane, 200.0);
AnchorPane.setLeftAnchor(stackPane, 200.0);
// Listeners for updating the Label
anchorPane.widthProperty().addListener((ov, oldValue, newValue) -> apWidth.setText("Width : " + String.valueOf(newValue)));
anchorPane.heightProperty().addListener((ov, oldValue, newValue) -> apHeight.setText("Height : " + String.valueOf(newValue)));
stackPane.widthProperty().addListener((ov, oldValue, newValue) -> spWidth.setText("W : " + String.valueOf(newValue)));
stackPane.heightProperty().addListener((ov, oldValue, newValue) -> spHeight.setText("H : " + String.valueOf(newValue)));
// Bind StackPane's Height and Width to that of AnchorPane's
stackPane.prefHeightProperty().bind(anchorPane.heightProperty().divide(10));
stackPane.prefWidthProperty().bind(anchorPane.widthProperty().divide(10));
Scene scene = new Scene(anchorPane);
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.