Java 使用转换边界的布局
我已缩放窗格中的节点。但是窗格的布局考虑了边界,没有任何转换。我希望它考虑到变换的边界 例如: 以及守则:Java 使用转换边界的布局,java,javafx-2,javafx-8,Java,Javafx 2,Javafx 8,我已缩放窗格中的节点。但是窗格的布局考虑了边界,没有任何转换。我希望它考虑到变换的边界 例如: 以及守则: 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.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;
}
}
一个解决方案可能是在圆圈和标签上应用翻译,但这种方式似乎太难做这么简单的事情,并且使用
窗格(HBox
)似乎比使用带有硬编码布局的基本组
更痛苦。如果在调用后台的show方法后更改设置layoutX和layoutY属性,则可以达到相同的效果
...
stage.show();
c2.setLayoutX(c2.getLayoutX()-c2.getRadius()*(1-c2.getScaleX()));
}
或者翻译一下:
c2.getTransforms().add(new Translate(-c2.getRadius()*(1-c2.getScaleX()), 0));
//Replace getScaleX() with scale for c3
由于节点的宽度不会因缩放而改变(检查)。我在帖子中找到了答案:
如果希望layoutBounds与节点的物理边界(包括效果、剪辑和变换)相匹配,则将其包装在一个组中(例如,如果希望节点在鼠标悬停时放大,并且希望其相邻节点快速移动,以便为放大的节点腾出空间)
为了解决我的问题,我写了:
...
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"));
...
我得到了预期的结果。谢谢你的帮助。结果并不完全相同。我已经更新了我的问题。看看圆圈和标签之间的空间。