java - 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(...)
?
解决方案
如果它不是关键 - 你不需要在这里以编程方式生成布局传递,你可以让 javaFX 计算它并在显示你的阶段后更新。只是移动你的逻辑之后stage.show();
你会得到正确的输出
stage.show();
System.out.println(xAxis.getDisplayPosition(9));
我有:443.0
推荐阅读
- java - 使用 JavaFX -8 自动调整 TableView 中 TableColumn 的 TableColumn 大小
- azure-devops - 记录错误但管道通过时管道失败
- javascript - 反应忽略css转换?
- slider - UIKit (GetUIKit) - 滑块图像直到转换才加载
- r - 关于spls包中稀疏偏最小二乘(DA)和mixOmics的区别
- java - 每次滚动/平移/拖动时,相机都会移回原始位置
- javascript - 从javascript中的字符串拆分数字(仅限ID)
- python - 如何用新维度重塑 xarray 数据
- django - 将 FCM 与 django 和颤振一起使用
- python - 具有多个条件的字典的逆模糊匹配