簡體   English   中英

javafx - 如何將節點轉換為另一個對象

[英]javafx - how to convert a node to another object

對不起,我找不到解決方法了。 我必須得到一個“節點”對象作為“線”對象。 我的意思是:
我有一個充滿了很多節點的AnchorPane,其中一些是標簽,其中大多數是行。 設置它很好,但是在我的代碼中我需要這些行的坐標。 我試過的是這個(下面的解釋):

List<Line> lineList = new ArrayList<>();
    for (Node currentNode : anchorPaneGame.getChildren()){
        if (currentNode.getTypeSelector().equals("Line")){
            lineList.add(currentNode);
        }

我創建了一個列表,我想收集所有的行,但這不起作用,因為(我只引用我的IDE):“List中的add(javafx.scene.shape.Line)無法應用於(java。 fx.scene.Node)”。
因為我試圖這樣做

Line tempLine = apGame.getChildren().get(apGame.getChildren().size()-1);  

並且當然得到了相同的錯誤(我知道最后一個元素是一個Line的事實,添加它作為最后一個是硬編碼的)。 我正在做這一切的原因是最后做了.getEndX() - 我嘗試的第一件事就是

        AnchorPane.setLeftAnchor(borderPane, apGame.getChildren().get(apGame.getChildren().size()-1).getEndX());

這應該是在最后一行的末尾設置Borderpane,可以在AnchorPane apGame找到。 但是因為.getChildren只返回節點,所以它不知道它正在處理一個Line,因此無法解析.getEndX()
您是否有任何想法如何讓程序意識到給定的節點實際上是一個線?

instanceof與downcast結合使用:

List<Line> lineList = new ArrayList<>();
for (Node currentNode : anchorPaneGame.getChildren()){
    if (currentNode instanceof Line){
        lineList.add((Line)currentNode);
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM