import javafx.application.Application;
import javafx.beans.property.SimpleDoubleProperty;
import javafx.scene.Scene;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.Pane;
import javafx.scene.text.Text;
import javafx.stage.Stage;
public class App15_4 extends Application {
private double tOffX, tOffy;
Text t = new Text(20, 20, "拖动我");
@Override
public void start(Stage primaryStage) {
Pane pane = new Pane();
pane.getChildren().add(t);
t.setOnMousePressed(e -> HandleMousePressed(e));
t.setOnMouseDragged(e -> HandleMouseDragged(e));
Scene scene = new Scene(pane, 800, 600);
primaryStage.setScene(scene);
primaryStage.setTitle("拖动操作");
primaryStage.show();
}
private void HandleMousePressed(MouseEvent e) {
tOffX = e.getSceneX() - t.getX();
tOffy = e.getSceneY() - t.getY();
}
private void HandleMouseDragged(MouseEvent e) {
// t.setX(e.getSceneX() - tOffX);
// t.setY(e.getSceneY() - tOffy);
t.xProperty().bind(new SimpleDoubleProperty(e.getSceneX() - tOffX));
t.yProperty().bind(new SimpleDoubleProperty(e.getSceneY() - tOffy));
}
public static void main(String[] args) {
Application.launch(args);
}
}