package application;
import javafx.application.Application;
import javafx.event.EventHandler;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.input.MouseEvent;
import javafx.stage.Stage;
public class test extends Application{
double x1;
double y1;
public void start(Stage stage){
Group root = new Group();
Scene scene = new Scene(root, 100, 100);
stage.setScene(scene);
stage.show();
scene.setOnMouseDragged(new EventHandler<MouseEvent>() {
@Override public void handle(MouseEvent m)
//获取当前窗口的坐标
double x_stage = stage.getX();
double y_stage = stage.getY();
//计算
stage.setX(x_stage + m.getX() - x1);
stage.setY(y_stage + m.getY() - y1);
}
});
scene.setOnMousePressed(new EventHandler<MouseEvent>() {
@Override public void handle(MouseEvent m) {
//按下鼠标后,记录当前鼠标的坐标
x1 =m.getX();
y1 =m.getY();
}
});
}
public static void main(String[] args) {
launch();
}
}
2 个解决方案
#1
#2
此贴终结!
package application;
import javafx.application.Application;
import javafx.event.EventHandler;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.input.MouseEvent;
import javafx.stage.Stage;
public class test extends Application{
double x1;
double y1;
double x_stage;
double y_stage;
public void start(Stage stage){
Group root = new Group();
Scene scene = new Scene(root, 100, 100);
stage.setScene(scene);
stage.show();
scene.setOnMouseDragged(new EventHandler<MouseEvent>() {
@Override public void handle(MouseEvent m) {
//计算
stage.setX(x_stage + m.getScreenX() - x1);
stage.setY(y_stage + m.getScreenY() - y1);
}
});
scene.setOnDragEntered(null);
scene.setOnMousePressed(new EventHandler<MouseEvent>() {
@Override public void handle(MouseEvent m) {
//按下鼠标后,记录当前鼠标的坐标
x1 =m.getScreenX();
y1 =m.getScreenY();
x_stage = stage.getX();
y_stage = stage.getY();
}
});
}
public static void main(String[] args) {
launch();
}
}
package application;
import javafx.application.Application;
import javafx.event.EventHandler;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.input.MouseEvent;
import javafx.stage.Stage;
public class test extends Application{
double x1;
double y1;
double x_stage;
double y_stage;
public void start(Stage stage){
Group root = new Group();
Scene scene = new Scene(root, 100, 100);
stage.setScene(scene);
stage.show();
scene.setOnMouseDragged(new EventHandler<MouseEvent>() {
@Override public void handle(MouseEvent m) {
//计算
stage.setX(x_stage + m.getScreenX() - x1);
stage.setY(y_stage + m.getScreenY() - y1);
}
});
scene.setOnDragEntered(null);
scene.setOnMousePressed(new EventHandler<MouseEvent>() {
@Override public void handle(MouseEvent m) {
//按下鼠标后,记录当前鼠标的坐标
x1 =m.getScreenX();
y1 =m.getScreenY();
x_stage = stage.getX();
y_stage = stage.getY();
}
});
}
public static void main(String[] args) {
launch();
}
}
#1
#2
此贴终结!
package application;
import javafx.application.Application;
import javafx.event.EventHandler;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.input.MouseEvent;
import javafx.stage.Stage;
public class test extends Application{
double x1;
double y1;
double x_stage;
double y_stage;
public void start(Stage stage){
Group root = new Group();
Scene scene = new Scene(root, 100, 100);
stage.setScene(scene);
stage.show();
scene.setOnMouseDragged(new EventHandler<MouseEvent>() {
@Override public void handle(MouseEvent m) {
//计算
stage.setX(x_stage + m.getScreenX() - x1);
stage.setY(y_stage + m.getScreenY() - y1);
}
});
scene.setOnDragEntered(null);
scene.setOnMousePressed(new EventHandler<MouseEvent>() {
@Override public void handle(MouseEvent m) {
//按下鼠标后,记录当前鼠标的坐标
x1 =m.getScreenX();
y1 =m.getScreenY();
x_stage = stage.getX();
y_stage = stage.getY();
}
});
}
public static void main(String[] args) {
launch();
}
}
package application;
import javafx.application.Application;
import javafx.event.EventHandler;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.input.MouseEvent;
import javafx.stage.Stage;
public class test extends Application{
double x1;
double y1;
double x_stage;
double y_stage;
public void start(Stage stage){
Group root = new Group();
Scene scene = new Scene(root, 100, 100);
stage.setScene(scene);
stage.show();
scene.setOnMouseDragged(new EventHandler<MouseEvent>() {
@Override public void handle(MouseEvent m) {
//计算
stage.setX(x_stage + m.getScreenX() - x1);
stage.setY(y_stage + m.getScreenY() - y1);
}
});
scene.setOnDragEntered(null);
scene.setOnMousePressed(new EventHandler<MouseEvent>() {
@Override public void handle(MouseEvent m) {
//按下鼠标后,记录当前鼠标的坐标
x1 =m.getScreenX();
y1 =m.getScreenY();
x_stage = stage.getX();
y_stage = stage.getY();
}
});
}
public static void main(String[] args) {
launch();
}
}