繁体   English   中英

我很难在JavaFX中调整某些元素的大小

[英]I'm having a hard time getting some elements to resize in JavaFX

我有一系列要动态调整大小的元素。

我在较大的AnchorPane设置了一个小的StackPane AnchorPane根据窗口大小的变化AnchorPane调整大小。 我正在尝试调整StackPane的大小,以使其与AnchorPane调整大小之前的大小保持AnchorPane

基本上,如果我的AnchorPaneAnchorPane ,我的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如何使用我的ChangeListener做到这一点; oldVal / newVal更新非常快。

我遇到一个奇怪的问题,它似乎在扩大时会适当调整大小,但是如果在缩小时拖动得足够慢,该元素根本不会调整大小。

您可以将Binding用于此类要求。 StackPane的高度和宽度绑定到AnchorPane的高度和宽度。

为了将StackPane's 宽度高度绑定到StackPane's 宽度的正好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);
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM