JavaFX Marquee go out of my node

2020-03-06 03:47发布

I have a issue with my Marquee animation with JavaFX. I have a HBox with three Nodes and in the second node I have a Text node inside that I need do the Marquee transformation, but when the text goes out of the second node I need it doesn't be visible.

I'll go to set a picture to show my issue (the text is visible in the white area).

text introduces inside white node

My Hbox code:

    HBox bill = new HBox(0);
    bill.getChildren().addAll(logoPane,product,total);
    bill.setBackground(new Background(new BackgroundFill(Color.web("#FFFFFF"), CornerRadii.EMPTY, Insets.EMPTY)));
    bill.setHgrow(product, Priority.ALWAYS);

Animation:

    timelineAnimation = new Timeline();
    final KeyValue kv = new KeyValue(productLabel.translateXProperty(), -1000);
    final KeyFrame kf = new KeyFrame(Duration.millis(2000), kv);
    timelineAnimation.getKeyFrames().add(kf);

And how I define my product node:

productLabel.setFont(new Font("Times New Roman",30));

    product = new StackPane();
    product.setMaxWidth(2000);
    product.setMaxHeight(100);
    product.setMinWidth(574);
    product.setMinHeight(100);

    product.getChildren().add(productLabel);
    product.setBackground(new Background(new BackgroundFill(Color.RED, CornerRadii.EMPTY, Insets.EMPTY)));
    product.setAlignment(productLabel, Pos.CENTER);

Hope it was enough information.

Thanks!

1条回答
我只想做你的唯一
2楼-- · 2020-03-06 04:05

Simply add a Rectangle as clip for the product pane and bind it's size to the size of the pane:

Rectangle clip = new Rectangle();
product.layoutBoundsProperty().addListener((observable, oldValue, newValue) -> {
    clip.setWidth(newValue.getWidth());
    clip.setHeight(newValue.getHeight());
});
product.setClip(clip);

This will make sure no descendants of product are drawn outside the bounds of this node.

查看更多
登录 后发表回答