har*_*ger 3 java javafx object line scene
对不起,我找不到解决方法了.我必须得到一个"节点"对象作为"线"对象.我的意思是这样的:
我有一个充满了很多节点的AnchorPane,其中一些是标签,其中大多数是行.设置它很好,但是在我的代码中我需要这些行的坐标.我试过的是这个(下面的解释):
List<Line> lineList = new ArrayList<>();
for (Node currentNode : anchorPaneGame.getChildren()){
if (currentNode.getTypeSelector().equals("Line")){
lineList.add(currentNode);
}
Run Code Online (Sandbox Code Playgroud)
我创建了一个列表,我想收集所有的行,但这不起作用,因为(我只引用我的IDE):"List中的add(javafx.scene.shape.Line)无法应用于(java. fx.scene.Node)".
因为我试图这样做
Line tempLine = apGame.getChildren().get(apGame.getChildren().size()-1);
Run Code Online (Sandbox Code Playgroud)
并且当然得到了相同的错误(我知道最后一个元素是一个Line的事实,添加它作为最后一个是硬编码的).我做这一切的原因是最后做.getEndX()的 - 我尝试的第一件事就是
AnchorPane.setLeftAnchor(borderPane, apGame.getChildren().get(apGame.getChildren().size()-1).getEndX());
Run Code Online (Sandbox Code Playgroud)
这应该将Borderpane设置在AnchorPane中可以找到的最后一行的末尾apGame.但因为.getChildren只返回Nodes,所以它不知道它正在处理一个Line,因此无法解决.getEndX().
您是否有任何想法如何让程序意识到给定的节点实际上是一个线?
使用instanceof与沮丧的结合:
List<Line> lineList = new ArrayList<>();
for (Node currentNode : anchorPaneGame.getChildren()){
if (currentNode instanceof Line){
lineList.add((Line)currentNode);
}
}
Run Code Online (Sandbox Code Playgroud)