使用变换后的边界进行布局

使用变换后的边界进行布局

本文介绍了使用变换后的边界进行布局的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经缩放了窗格中的一个节点.但是,窗格的布局考虑了边界,没有进行任何变换.我希望它考虑到转换后的边界.

I have scaled a node in a Pane. But the layout of the Pane take into account the bounds without any transformation. I want it to take into account the transformed bounds.

例如:

和代码:

import javafx.application.Application;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.scene.shape.Circle;
import javafx.scene.transform.Scale;
import javafx.scene.transform.Translate;
import javafx.stage.Stage;

public class HBoxApp extends Application {
    public static void main(String[] args) {
        launch(args);
    }

    @Override
        public void start(Stage stage) throws Exception {
            double scale = 0.75;

            HBox box1 = createBox();
            box1.getChildren().add(new Circle(20));
            box1.getChildren().add(new Label("Test without any scale"));

            HBox box2 = createBox();
            Circle c2 = new Circle(20);
            c2.setScaleX(scale);
            c2.setScaleY(scale);
            box2.getChildren().add(c2);
            box2.getChildren().add(new Label("Test with the setScaleX/Y methods"));

            HBox box3 = createBox();
            Circle c3 = new Circle(20);
            c3.getTransforms().add(new Scale(scale, scale));
            box3.getChildren().add(c3);
            box3.getChildren().add(new Label("Test with the Scale transform"));

            HBox box4 = createBox();
            Circle c4 = new Circle(20);
            c4.getTransforms().addAll(new Scale(scale, scale), new Translate(-20*(1-scale), 0));
            box4.getChildren().add(c4);
            box4.getChildren().add(new Label("Test with the Scale and Translate transform"));

            HBox box5 = createBox();
            box5.getChildren().add(new Circle(20 * scale));
            box5.getChildren().add(new Label("My Goal"));

            VBox vBox = new VBox(10);
            vBox.getChildren().addAll(box1, box2, box4, box5);
            stage.setScene(new Scene(vBox, 300, 200));
            stage.show();
        }

    private HBox createBox() {
        HBox box = new HBox(5);
        box.setAlignment(Pos.CENTER_LEFT);
        return box;
    }
}

一个解决方案可能是在圆和标签上应用平移,但是这种方式对于做这么简单的事情似乎太困难了,并且使用Pane(HBox)似乎比使用基本的Group具有硬编码布局.

A solution could be to apply translations on the circle and on the label, but this way seems too hard for doing a so simple thing, and using a Pane(HBox) seems more painful than using a basic Group with an hardcoded layout.

推荐答案

我在帖子 JavaFX1.2:了解界限:

为了解决我的问题,我写道:

So for solving my problem, i wrote :

...
HBox box5 = createBox();
Circle c5 = new Circle(20);
c5.setScaleX(scale);
c5.setScaleY(scale);
box5.getChildren().add(new Group(c5));
box5.getChildren().add(new Label("Test with the Scale transform and a group"));
...

我得到了预期的结果.

这篇关于使用变换后的边界进行布局的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-21 12:18