java - 使用转换后的边界布局

标签 java javafx-2 javafx-8

我已经缩放了 Pane 中的一个节点。但是 Pane 的布局考虑了 bounds 没有任何转换。我希望它考虑转换后的边界。

例如:

enter image description here

和代码:

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 更痛苦。

最佳答案

我在帖子中找到了答案 JavaFX1.2: Understanding Bounds :

If you want layoutBounds to match a node's physical bounds (including effects, clip, and transforms) then wrap it in a Group (ex. if you want a node to scale up on mouse-over and you want its neighbors to scoot over to make room for the magnified node).

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

...
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"));
...

我得到了预期的结果。

关于java - 使用转换后的边界布局,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17699623/

相关文章:

java - 将参数传递给 Java 方法内定义的方法

java - 如何从动态html页面获取值?

javafx - JavaFX 下的全屏与 FXML

java - JavaFX 2.0 AudioClip无法在applet中播放吗?

java - 如何使用相机正确显示 3D 形状?

java - 如何以编程方式在 JavaFX 中设置选项卡标签的颜色或纹理?

java - JUnit 4.x : why is @Before never executed?

使用 Bouncy CaSTLe API 实现 Java 安全

javafx-2 - 在哪里可以下载JavaFX 2.2源代码?

java - 从 FXML 文件中分离文本