我在AnchorPane中添加了一个图表,我想得到它的图表的边界(图表图,我用青色标记它),这样我就可以在它上面添加一些文本,但我应该知道它的确切根据其祖先的界限.如果我手动执行,我可能会在调整大小等时更改节点的填充大小时失败.
import javafx.application.Application;
import javafx.geometry.Side;
import javafx.scene.Node;
import javafx.scene.Scene;
import javafx.scene.chart.LineChart;
import javafx.scene.chart.NumberAxis;
import javafx.scene.layout.AnchorPane;
import javafx.stage.Stage;
public class Main extends Application {
@Override
public void start(Stage primaryStage) throws Exception {
NumberAxis numberAxis = new NumberAxis();
LineChart chart = new LineChart(numberAxis, new NumberAxis());
chart.getYAxis().setSide(Side.RIGHT);
Node chartPlotArea = chart.lookup(".chart-plot-background");
chartPlotArea.setStyle("-fx-background-color: cyan");
AnchorPane anchorPane = new AnchorPane(chart);
AnchorPane.setTopAnchor(chart, 0.0);
AnchorPane.setRightAnchor(chart, 0.0);
AnchorPane.setBottomAnchor(chart, 0.0);
AnchorPane.setLeftAnchor(chart, 0.0);
Scene scene = new Scene(anchorPane);
primaryStage.setScene(scene);
primaryStage.setMaximized(true);
primaryStage.show();
}
}
Run Code Online (Sandbox Code Playgroud)
所以问题是如何在我的情况下根据它的祖先获得节点或图表的界限,无论有多少?