![](/img/trans.png)
[英]How to translate children elements of different size outside of their parents that have also different size and align them at the top of their parents
[英]Translating a node outside of parents bounds changes minimum size to current size
當我在父節點的邊界之外翻譯節點時。 父母的父母的最小尺寸被設置為其當前尺寸。 您可以通過此演示看到它:
package com.neonorb.test;
import javafx.application.Application;
import javafx.application.Platform;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.VBox;
import javafx.scene.paint.Color;
import javafx.scene.shape.Rectangle;
import javafx.stage.Stage;
import java.io.IOException;
/**
* Created by chris on 7/20/15.
*/
public class Test extends Application {
public static void main(String[] args) {
launch(args);
}
@Override
public void start(Stage primaryStage) throws IOException {
Label label = new Label("translating label");
Label markerLabel = new Label("marker label");
Button button = new Button("button");
VBox leftSpace = new VBox();
Label leftLabel = new Label("left space");
leftSpace.getChildren().add(leftLabel);
Rectangle rectangle = new Rectangle();
rectangle.setFill(Color.RED);
rectangle.heightProperty().bind(leftSpace.heightProperty());
rectangle.widthProperty().bind(leftSpace.widthProperty());
button.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent actionEvent) {
new Thread() {
public void run() {
Platform.runLater(() -> label.setTranslateY(1000.0));
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
Platform.runLater(() -> label.setTranslateY(0.0));
}
}.start();
}
});
BorderPane borderPane = new BorderPane();
BorderPane center = new BorderPane();
center.setCenter(label);
center.setBottom(markerLabel);
borderPane.setCenter(center);
borderPane.setTop(button);
borderPane.setLeft(leftSpace);
borderPane.setRight(rectangle);
primaryStage.setScene(new Scene(borderPane));
primaryStage.show();
}
}
出現側欄內容( VBox
和Rectangle
)的原因是因為它們存在於我的實際應用程序中。 VBox
只能容納更多內容,而Rectangle
可以使中心組件居中放置(通常是透明的,但是這里為便於查看而着色)。 如您所見,矩形的寬度和高度綁定到VBox
的高度:
rectangle.heightProperty().bind(leftSpace.heightProperty());
rectangle.widthProperty().bind(leftSpace.widthProperty());
要重現該問題,您可以將窗口的高度稍微增加一點(大約一英寸),然后點擊按鈕。 該節點將向下平移1000像素。 現在嘗試縮小窗口,底部的文本(“標記標簽”)將開始被窗口底部隱藏。
我通過使用Region
而不是Rectangle
並設置了它的首選大小來修復它。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.