我有一系列想要动态调整大小的元素。
我在一个较大的 AnchorPane
中设置了一个较小的 StackPane
。 AnchorPane
根据窗口大小的变化调整大小。我正在尝试调整 StackPane
的大小,以保持相对于调整 AnchorPane
大小之前的大小相同的大小。
基本上,如果我的 AnchorPane
是 1000px,我的 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);
}
};
我不知道如何使用我的 ChangeListener
来做到这一点; oldVal
/newVal
更新得非常快。
我遇到一个奇怪的问题,它似乎在加宽时正确调整大小,但是如果我在缩小时拖动得足够慢,则该元素根本不会调整大小。
最佳答案
您可以使用Binding对于此类要求。将 StackPane
的高度和宽度绑定(bind)到 AnchorPane
的高度和宽度。
要将 StackPane 的 宽度 和 高度 绑定(bind)到 AnchorPane 的 1/10,您可以使用以下命令:
stackPane.prefHeightProperty().bind(anchorPane.heightProperty().divide(10));
stackPane.prefWidthProperty().bind(anchorPane.widthProperty().divide(10));
我创建了一个 MCVE,其中 StackPane 的高度和宽度是 AnchorPane 的 1/10。有一些标签显示两种布局的当前大小。
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);
}
}
关于java - 我很难在 JavaFX 中调整某些元素的大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31307795/