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

查看:180
本文介绍了在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);
    }
}


推荐答案

strong>建议的方法

Suggested approach

对于您的特定代码段,可能使用窗格,而不是 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天全站免登陆