简体   繁体   中英

Translating a node outside of parents bounds changes minimum size to current size

When I translate a node outside of the bounds of it's parent. The minimum size of the parent of the parent is set to it's current size. You can see it with this demo:

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();
    }
}

The reason for the side bar things (the VBox and Rectangle ) is because they exist in my real application. The VBox just holds more content, and the Rectangle is there to keep the center components centered (normally transparent, but here it is colored for visibility). As you can see, the width and height of the rectangle are binded to the VBox 's height:

rectangle.heightProperty().bind(leftSpace.heightProperty());
rectangle.widthProperty().bind(leftSpace.widthProperty());

To reproduce the problem, you can increase the height of the window a little (about an inch), then hit the button. The node will be translated down 1000 pixels and back. Now try to shrink the window, the text at the bottom, ("marker label"), will start to be hidden by the bottom of the window.

我通过使用Region而不是Rectangle并设置了它的首选大小来修复它。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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