在 JavaFX 中显式定位节点 [英] Explicitly positioning nodes in JavaFX

查看:28
本文介绍了在 JavaFX 中显式定位节点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我点击一个按钮时,它会改变它的位置.

When I have clicked a button, it changes its position.

但是当我移动鼠标时,按钮又回到了场景的中心,为什么?

But when I move the mouse, the button comes back to the center of the scene, why?

我有以下代码:

public class HolaMundo extends Application {

    Button btn;
    Scene scene;

    @Override
    public void start(Stage primaryStage) {
        btn = new Button();
        btn.setText("Hola Mundo");

        StackPane root = new StackPane();
        root.getChildren().add(btn);

        scene = new Scene(root, 300, 250);

        primaryStage.setTitle("Hello World!");
        primaryStage.setScene(scene);
        primaryStage.show();

        scene.setOnMouseMoved(new EventHandler<MouseEvent>(){

            @Override
            public void handle(MouseEvent t) {
                 btn.setText(String.valueOf(t.getX() ));
            }
        });

        btn.setOnAction(new EventHandler<ActionEvent>() {

            @Override
            public void handle(ActionEvent event) {
                btn.setLayoutX(Math.random() * (300 - btn.getWidth()));
                btn.setLayoutY(Math.random() * (250 - btn.getHeight())); 
            }
        });
    }

    public static void main(String[] args) {
        launch(args);
    }
}

推荐答案

建议方法

对于您的特定代码片段,可能使用 Pane 而不是 StackPane 是最好的方法.

For your particular code snippet, perhaps using a Pane instead of a StackPane is the best approach.

为什么你的代码没有做你想要的

如果您要手动设置布局值,请不要使用自动为您设置布局值的布局窗格(例如 StackPane).如果您使用布局窗格,那么您明确设置的布局值将在下次布局窗格执行布局过程时自动覆盖(例如,它被调整大小或它在场景图中的某些内容或位置变脏).

If you are going to manually set layout values, don't use a layout pane (such as a StackPane) which automatically sets layout values for you. If you use a layout pane, then the layout values you explicitly set will be overridden automatically the next time layout pane performs a layout pass (e.g. it is resized or some its content or location in the scene graph becomes dirty).

显式布置节点的选项

如果您想在 JavaFX 中显式布局项目,请执行以下操作之一:

If you want to explicitly layout items in JavaFX do one of the following:

  1. 子类 Region 并覆盖 layoutChildren.
  2. 将您的内容放在窗格如果您需要使用 CSS 对容器进行样式设置或实现大小调整功能.
  3. 将您的内容放入(如果您不需要使用 CSS 对容器进行样式设置或实现大小调整功能.
  1. Subclass Region and override layoutChildren.
  2. Place your content in a Pane if you need the container to be styled with CSS or implement resizing functions.
  3. Place your content in a Group if you don't need the container to be styled with CSS or implement resizing functions.

节点定位相关建议

如果您想使用预定义的布局管理器自动布局组件,但您想从默认位置调整或临时修改某些组件的位置,您可以调整 translateX/Y 值而不是 layoutX/Y值.根据translateX:

If you want to have automatically laid out components using the predefined layout managers but you want to adjust or temporarily modify the locations of some of the components from their default positions, you can adjust the translateX/Y values rather than layoutX/Y values. According to the definition of translateX:

节点的最终平移将计算为 layoutX + translateX,其中 layoutX 建立节点的稳定位置,而 translateX 可选择对该位置进行动态调整.此变量可用于更改节点的位置,而不会影响其 layoutBounds,这对于为节点的位置设置动画很有用.

The node's final translation will be computed as layoutX + translateX, where layoutX establishes the node's stable position and translateX optionally makes dynamic adjustments to that position. This variable can be used to alter the location of a node without disturbing its layoutBounds, which makes it useful for animating a node's location.

这意味着布局管理器可以使用 layoutX 计算孩子的默认位置,并且您可以使用 translateX 从默认位置调整位置.

This means that the layout manager can compute a child's default position using layoutX, and you can adjust the position from the default using translateX.

这篇关于在 JavaFX 中显式定位节点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆