首页 > 解决方案 > NumberAxis.getDisplayPosition(...) 返回 0

问题描述

我正在尝试获取 a 上一个点的 x,yLineChart<Number, Number>以便我可以在其上添加 a Line。因此,我尝试通过执行以下操作来获得 9 的 x 值:

Group root = new Group(); 
NumberAxis xAxis = new NumberAxis();
xAxis.setAutoRanging(false);
xAxis.setLowerBound(0);
xAxis.setUpperBound(9);
LineChart<Number, Number> lineChart = new LineChart<Number, Number>(xAxis, yAxis);
System.out.println(xAxis.getDisplayPosition(9)); // 0
root.getChildren().add(lineChart);
Scene scene = new Scene(root, 800, 800); 
stage.setScene(scene);
stage.show();

在互联网上环顾四周后,我遇到了一个假设的解决方案

Node chartPlotArea = lineChart.lookup(".chart-plot-background");
double chartZeroX = chartPlotArea.getLayoutX();
System.out.println(xAxis.getDisplayPosition(9) + chartZeroX);

这也最终输出了0,为什么会这样?如何获得 x,y 的显示位置以便将其传递到Line.setStartX(...)/ Line.setStartY(...)

标签: javajavafx

解决方案


如果它不是关键 - 你不需要在这里以编程方式生成布局传递,你可以让 javaFX 计算它并在显示你的阶段后更新。只是移动你的逻辑之后stage.show();

你会得到正确的输出

  stage.show();

  System.out.println(xAxis.getDisplayPosition(9)); 

我有:443.0


推荐阅读