Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
938 views
in Technique[技术] by (71.8m points)

javafx - JavaFX:如何绑定到鼠标位置?(JavaFX : How to bind to mouse position?)

I want a line to bind from the mouse position while mouse is pressed.

(我希望在按下鼠标时从鼠标位置绑定一条线。)

Here is what i try :

(这是我尝试的:)

scene.setOnMousePressed(e ->{
    Line line = new Line();
    root.getChildren().add(line);
    DoubleProperty x = new SimpleDoubleProperty(e.getX());
    DoubleProperty y = new SimpleDoubleProperty(e.getY());
    line.setStartX(0.0f);
    line.setStartY(0.0f);
    line.endXProperty().bind(x);
    line.endYProperty().bind(y);
    scene.setOnMouseReleased(eMoved ->{
        root.getChildren().remove(line);
    });
});

But the line is not moving.

(但是线没有动。)

I know i could do something like that :

(我知道我可以做这样的事情:)

Line line = new Line();
root.getChildren().add(line);
line.setStartX(0.0f);
line.setStartY(0.0f);
scene.setOnMouseMoved(e ->{
    line.setEndX(e.getX());
    line.setEndY(e.getY());
});

But there is a little delay when moving the mouse (maybe this the best i can have, i don't know).

(但是移动鼠标时会有一点延迟(也许这是我所能拥有的最好的,我不知道)。)

Thanks.

(谢谢。)

  ask by perwhite translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
等待大神答复

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...