亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何根據JavaFX 8中的特定祖先獲取節點邊界?

如何根據JavaFX 8中的特定祖先獲取節點邊界?

肥皂起泡泡 2021-11-24 18:32:37
我在 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();    }}所以問題是如何在我的情況下根據其祖先獲得節點或圖表圖的邊界,無論有多少?
查看完整描述

1 回答

?
ibeautiful

TA貢獻1993條經驗 獲得超6個贊

您已經找到了 Chart-Plot 背景節點,要根據其祖先獲取坐標,您只需調用


chartPlotArea.getBoundsInParent();

如果它們之間有多個祖先,您可以像這樣在 AnchorPane 坐標系中獲得字符圖邊界


Bounds bounds =

            anchorPane.sceneToLocal(chartPlotArea.localToScene(chartPlotArea.getBoundsInLocal()));

這里的一個小技巧是,在您顯示舞臺并讓 javaFX 布局節點之前,它們將是 0,因此您需要在.show()方法之后更新它,因此結果可能如下所示:


 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");


    Text text = new Text();

    text.setText("Text");



    AnchorPane anchorPane = new AnchorPane();

    AnchorPane.setTopAnchor(chart, 0.0);

    AnchorPane.setRightAnchor(chart, 0.0);

    AnchorPane.setBottomAnchor(chart, 0.0);

    AnchorPane.setLeftAnchor(chart, 0.0);



    anchorPane.getChildren().addAll(chart, text);


    Scene scene = new Scene(anchorPane);

    primaryStage.setScene(scene);

    primaryStage.setMaximized(true);



    primaryStage.show();


        Bounds bounds =

            anchorPane.sceneToLocal(chartPlotArea.localToScene(chartPlotArea.getBoundsInLocal()));


    double textRelativeX = (bounds.getMinX() + bounds.getMaxX()) / 2 - text.getLayoutBounds().getWidth() / 2;

    double textRelativeY = bounds.getMinY() - text.getLayoutBounds().getHeight() / 2;


    AnchorPane.setLeftAnchor(text, textRelativeX);

    AnchorPane.setTopAnchor(text, textRelativeY);

請記住,如果您想在調整大小時更改坐標,您可以將其綁定到圖表或 chartPlotArea 邊界/寬度更改,類似這樣


  chart.layoutBoundsProperty().addListener((observable, oldValue, newValue) -> {

        double textRelativeXz = (newValue.getMinX() + newValue.getMaxX()) / 2 - text.getLayoutBounds().getWidth() / 2;

        double textRelativeYz = newValue.getMinY() - text.getLayoutBounds().getHeight() / 3;


        AnchorPane.setLeftAnchor(text, textRelativeXz);

        AnchorPane.setTopAnchor(text, textRelativeYz);

    });

編輯:如果您有多個祖先,您可以這樣做以在 anchorPane 坐標系中接收字符圖邊界


     Bounds bounds =

            anchorPane.sceneToLocal(chartPlotArea.localToScene(chartPlotArea.getBoundsInLocal()));   

即使他們之間有不止一個祖先,這也會起作用


查看完整回答
反對 回復 2021-11-24
  • 1 回答
  • 0 關注
  • 154 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號