第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

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

如何根據(jù)JavaFX 8中的特定祖先獲取節(jié)點邊界?

如何根據(jù)JavaFX 8中的特定祖先獲取節(jié)點邊界?

肥皂起泡泡 2021-11-24 18:32:37
我在 AnchorPane 中添加了一個圖表,我想得到它的圖的邊界(圖表圖,我用青色標(biāo)記了它),這樣我就可以在它上面添加一些文本,但我應(yīng)該知道它的確切信息根據(jù)其祖先的界限。如果我手動執(zhí)行此操作,則在調(diào)整大小等時會更改節(jié)點的填充大小時,我可能會失敗。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();    }}所以問題是如何在我的情況下根據(jù)其祖先獲得節(jié)點或圖表圖的邊界,無論有多少?
查看完整描述

1 回答

?
ibeautiful

TA貢獻(xiàn)1993條經(jīng)驗 獲得超6個贊

您已經(jīng)找到了 Chart-Plot 背景節(jié)點,要根據(jù)其祖先獲取坐標(biāo),您只需調(diào)用


chartPlotArea.getBoundsInParent();

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


Bounds bounds =

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

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


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

請記住,如果您想在調(diào)整大小時更改坐標(biāo),您可以將其綁定到圖表或 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 坐標(biāo)系中接收字符圖邊界


     Bounds bounds =

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

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


查看完整回答
反對 回復(fù) 2021-11-24
  • 1 回答
  • 0 關(guān)注
  • 159 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號