我很难在 JavaFX 中调整一些元素的大小
I'm having a hard time getting some elements to resize in JavaFX
我有一系列要动态调整大小的元素。
我在较大的 AnchorPane
中设置了较小的 StackPane
。 AnchorPane
根据 window 大小的变化调整大小。我正在尝试调整 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
的高度和宽度绑定到 AnchorPane
.
的高度和宽度
要将 StackPane's
width 和 height 绑定到 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);
}
}
我有一系列要动态调整大小的元素。
我在较大的 AnchorPane
中设置了较小的 StackPane
。 AnchorPane
根据 window 大小的变化调整大小。我正在尝试调整 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
的高度和宽度绑定到 AnchorPane
.
要将 StackPane's
width 和 height 绑定到 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);
}
}