java - 如何使用带有标签的形状位置

标签 java javafx javafx-8 shapes

如果我显示一个具有特定 x 和 y 坐标的圆,它就可以正常工作:

public class FxApplication extends Application {
    @Override
    public void start(Stage primaryStage) throws Exception {
        Group group = new Group();

        Circle circle = new Circle(100, 100, 2);
        group.getChildren().add(circle);

        Pane pane = new Pane(group);
        ScrollPane scrollPane = new ScrollPane(pane);
        BorderPane borderPane = new BorderPane(scrollPane);
        Scene scene = new Scene(borderPane);
        primaryStage.setScene(scene);
        primaryStage.show();
    }
}

only circle

但是如果我向圆圈添加标签,则圆圈的位置将被忽略。

public class FxApplication extends Application {
    @Override
    public void start(Stage primaryStage) throws Exception {
        Group group = new Group();

        Circle circle = new Circle(100, 100, 2);
        Label label = new Label("test", circle);
        group.getChildren().add(label);

        Pane pane = new Pane(group);
        ScrollPane scrollPane = new ScrollPane(pane);
        BorderPane borderPane = new BorderPane(scrollPane);
        Scene scene = new Scene(borderPane);
        primaryStage.setScene(scene);
        primaryStage.show();
    }
}

with label

如何保留圆圈的位置并仅添加标签或如何设置正确的圆圈位置(包含标签)?

例如有效的是:

Circle circle = new Circle(circle_center_x, circle_center_y, 3);
Text label = new Text("test");
double halfLabelHeight = label.getLayoutBounds().getHeight() / 2;
label.relocate(circle_center_x + 10, circle_center_y - halfLabelHeight);
this.getChildren().addAll(circle , label);

但我正在寻找更集成的解决方案。我认为 Label 对象可能有点聪明,可以自己执行此操作,但它会采用圆的 x 和 y 位置并将其应用到它自己的空间而不是父空间。

最佳答案

您现在实际上需要重新定位标签,而不仅仅是告诉圆圈要显示在哪里。当您指定new Circle(100,100,2)时您告诉圆形对象位于其父对象的 x=100 和 y=100 处。在第一种情况下,其父级是 group但在第二种情况下,它的父级现在是 Label 。为了将标签定位到组内的 x,y = 100,100,您需要调用:

label.relocate(100, 100);

现在不需要 Circle 初始化。即使您将圆放在 0,0 处,它仍然会显示在标签旁边,因为标签将管理节点位置。

PS。您可以通过 label.setNodeOrientation(NodeOrientation.RIGHT_TO_LEFT); 将 NodeOrientation 从 LEFT_TO_RIGHT 更改为 RIGHT_TO_LEFT或者如果您想更改“形状”位置,您可以执行 label.setContentDisplay(ContentDisplay.TOP); (或底部等)

我不确定我是否理解正确,你想在这里实现什么,但我猜你想让圆圈和标签彼此相邻。此外,您希望标签以高度为中心,具体取决于圆的位置。如果前面的假设是正确的,那么这里是实现这一点的代码:

import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.control.ScrollPane;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.Pane;
import javafx.scene.shape.Circle;
import javafx.stage.Stage;

public class FxApplication extends Application {

    private Group group;

    @Override
    public void start(Stage primaryStage) throws Exception {
        group = new Group();

        addCustomNode(100, 100, new Circle(2), new Label("Test"));

        Pane pane = new Pane(group);
        ScrollPane scrollPane = new ScrollPane(pane);
        BorderPane borderPane = new BorderPane(scrollPane);
        Scene scene = new Scene(borderPane);
        primaryStage.setScene(scene);
        primaryStage.show();
    }

    private void addCustomNode(int x, int y, Circle circle, Label label) {

        double labelDimensions[] = getLabelDimensions(label);

        circle.setCenterX(100);
        circle.setCenterY(100);

        label.relocate(circle.getCenterX() + labelDimensions[0] / 2.0, circle.getCenterY() - labelDimensions[1] / 2.0);

        group.getChildren().addAll(circle, label);

    }

    // find the height and width before we
    // add the label to the stage
    private double[] getLabelDimensions(Label label) {
        HBox h = new HBox();
        Label l = new Label("Hello");
        h.getChildren().add(l);
        Scene s = new Scene(h);
        l.impl_processCSS(true);

        return new double[] { l.prefWidth(-1), l.prefHeight(-1) };
    }

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

关于java - 如何使用带有标签的形状位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47079638/

相关文章:

java - ava.lang.NullPointerException : null at org. apache.catalina.loader.WebappClassLoader.findResources(WebappClassLoader.java:1368)~[na:na]

charts - JavaFx 饼图 : control size

JavaFX colspan 不适用于 GridPane 中的文本

maven - JavaFX 8 - 如何使用 Maven 和 INNO 构建 EXE

javafx - Stage.setIconified() 和 Stage.isIconified() 运行不正常

java - 两个 ObservableDoubleValue 的最大值作为 ObservableDoubleValue

java - 使用Map实现高效的搜索过程-java

java - 处理二叉搜索树中的 "Keys"

java - 使用基本适配器将一个目录中的文件夹列表显示为项目

定义大小的 Javafx 快照