英文:
JavaFX Line not moving as it should
问题
我最近开始使用JavaFX,但在使线对象按预期移动方面遇到了一些错误。我对我的线条有以下代码。
Line line1 = new Line();
line1.setStartX(100.0);
line1.setStartY(150.0);
line1.setEndX(500.0);
line1.setEndY(150.0);
然后,我通过使用 gridPane.getChildren().addAll(line1);
将上述线条添加到我的GridPane中,与我之前在程序中使用的标签一起。
如您所见,无论我输入什么参数,该线条始终位于程序的顶部。我相信我已经正确设置了该线条,所以可能是GridPane的问题?我希望能得到指引方向的帮助。谢谢。
英文:
I've recently started using JavaFX and i've been having some errors getting a line object to move as it should. I have the following code for my line.
Line line1 = new Line();
line1.setStartX(100.0);
line1.setStartY(150.0);
line1.setEndX(500.0);
line1.setEndY(150.0);
I then add the said line to my GridPane by using gridPane.getChildren().addAll(line1); along with labels I used earlier in the program.
As you can see, the line is always at the top of the program, regardless of what parameters I enter. I believe I am properly setting up the line, so it may be a gridpane issue? I was hoping for help in the right direction. Thanks.
答案1
得分: 1
问题出在你将组件错误地放入了GridPane中。你忘记指定行索引,所以它被放置在第一个单元格中(行索引=0,列索引=0)。
顺便说一下,代替这样写:
Label anyLabel = new Label("Something");
GridPane.setRowIndex(anyLabel, y);
GridPane.setColumnIndex(anyLabel, x);
gridPane.getChildren().add(anyLabel, ... /* 其他JavaFX组件 */);
我建议你总是这样写:
Label anyLabel = new Label("Something");
gridPane.add(anyLabel, y, x);
使用这种语法,你就不会忘记将任何组件放入gridPane
中了。
英文:
The problem comes from a misplacement of your component into the GridPane. You forgot indicating the row index of your line, so it places it on the first cell (rowIndex=0, columnIndex=0).
By the way, instead of writing:
Label anyLabel = new Label("Something");
GridPane.setRowIndex(anyLabel, y);
GridPane.setColumnIndex(anyLabel, x);
gridPane.getChrildren().add(anyLabel, ... /* other JavaFX components */);
I suggest you to always write:
Label anyLabel = new Label("Something");
gridPane.add(anyLabel, y, x);
With that syntax you cannot forget any component's placement into gridPane
.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论