gon*_*ard 5 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.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;
}
}
Run Code Online (Sandbox Code Playgroud)
一种解决方案可能是在圆和标签上应用平移,但是这种方式对于做一件如此简单的事情似乎太困难了,并且使用Pane(HBox)比使用Group带有硬编码布局的基本版更痛苦。
我在JavaFX1.2: Understanding Bounds帖子中找到了答案:
如果您希望布局边界匹配节点的物理边界(包括效果、剪辑和变换),则将其包装在一个组中(例如,如果您希望一个节点在鼠标悬停时放大,并且希望其邻居滑过以使其放大节点的空间)。
因此,为了解决我的问题,我写道:
...
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"));
...
Run Code Online (Sandbox Code Playgroud)
我得到了预期的结果。
| 归档时间: |
|
| 查看次数: |
1060 次 |
| 最近记录: |