如何在javafx中从左侧增加矩形的宽度

vcudknz3  于 2021-08-20  发布在  Java
关注(0)|答案(0)|浏览(131)

我正在编写一个javafx应用程序,其中我想在一个特定的位置(坐标100100)放置一个矩形,并希望它的宽度在矩形的左侧增加(我希望在我的程序中使用锚窗格。)这是我的代码,我从这个链接中获得:
javafx矩形宽度动画

public class RectangleWidthAnimation extends Application{
    @Override
    public void start(Stage primaryStage) throws Exception {
        Rectangle statusBar = new Rectangle(300, 100);
        Button animationButton = new Button("Animate width increase by 25");
        animationButton.setOnAction(event -> {
            System.out.println("Animation start: width = " + statusBar.getWidth());
            KeyValue widthValue = new KeyValue(statusBar.widthProperty(), statusBar.getWidth() + 25);
            KeyFrame frame = new KeyFrame(Duration.seconds(2), widthValue);
            Timeline timeline = new Timeline(frame);
            timeline.play();
            timeline.setOnFinished(finishedEvent -> System.out.println("Animation end: width = " + statusBar.getWidth()));
        });

        VBox container = new VBox(10, statusBar, animationButton);

        container.setAlignment(Pos.CENTER_RIGHT);

        primaryStage.setScene(new Scene(container, 350, 150));
        primaryStage.show();
    }
}

我想改变的是,我不想将animationbutton和statusbar组合在一起。我想控制状态栏本身。所以我把这个换成:

VBox container = new VBox(statusBar);
container.setLayoutX(100);
container.setLayoutY(100);
container.setAlignment(Pos.CENTER_RIGHT);
primaryStage.setScene(new Scene(new Group(container, animationButton), 350, 150));

但是当我这样做的时候,矩形的宽度从右边开始增加,这不是我想要的我希望宽度从左边开始增加。换句话说,我想固定矩形的右上角坐标,并使其看起来矩形的左上角坐标正在向左移动。)
第一个代码使statusbar向左增长,但statusbar和animationbutton都位于一个vbox(容器)内。但是我想把statusbar放在一个vbox或hbox中,然后再次应用前面的代码。我尝试了这段代码,但奇怪的是,它使状态栏向右扩展。

public class RectangleWidthAnimation extends Application {
    @Override
    public void start(Stage primaryStage) throws Exception {
        Rectangle statusBar = new Rectangle(300, 100);
        Button animationButton = new Button("Animate width increase by 25");
        animationButton.setOnAction(event -> {
            System.out.println("Animation start: width = " + statusBar.getWidth());
            KeyValue widthValue = new KeyValue(statusBar.widthProperty(), statusBar.getWidth() + 25);
            KeyFrame frame = new KeyFrame(Duration.seconds(0.4), widthValue);
            Timeline timeline = new Timeline(frame);
            timeline.play();
            timeline.setOnFinished(finishedEvent -> System.out.println("Animation end: width = " + statusBar.getWidth()));
        });
        VBox container = new VBox(statusBar);
        container.setLayoutX(100);
        container.setLayoutY(100);
        container.setAlignment(Pos.CENTER_RIGHT);
 AnchorPane anchorPane = new AnchorPane();
        anchorPane.getChildren().add(container);
        anchorPane.getChildren().add(animationButton);
        primaryStage.setScene(new Scene(anchorPane, 500, 350));
        primaryStage.show();
    }
}

对如何解决这个问题有什么建议吗?

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题