JavaFX學習,通過鼠標拖動窗體代碼
package ch04;
/**
@package ch04
@file DraggingStage.java
@date 2020/12/31
@author qiaowei
@version 1.0
@brief
*/
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
import javafx.stage.StageStyle;
public class DraggingStage extends Application {
public static void main(String[] args) {
Application.launch(DraggingStage.class, args);
}
@Override
public void start(Stage primaryStage) {
// Store the stage reference in the instance variable to
// use it in the mouse pressed event handler later.
this.stage = primaryStage;
Label msgLabel = new Label("Press the mouse button and drag.");
Button closeButton = new Button("Close");
// closeButton.setOnAction(e -> stage.close());
closeButton.setOnAction(e -> primaryStage.close());
VBox root = new VBox();
root.getChildren().addAll(msgLabel, closeButton);
Scene scene = new Scene(root, 300, 200);
// Set lambda of mouse pressed and dragged even handlers for the scene
scene.setOnMousePressed((ev) -> handleMousePressed(ev));
// scene.setOnMousePressed(this::handleMousePressed(e));
scene.setOnMouseDragged(e -> handleMouseDragged(e));
stage.setScene(scene);
stage.setTitle("Moving a Stage");
// Setup the style of stage
stage.initStyle(StageStyle.UNDECORATED);
stage.show();
}
/********************************************************************************************************
@class DraggingStage
@date 2021/6/21
@author qiaowei
@version 1.0
@brief The press mouse event
@param e mouse event
********************************************************************************************************/
protected void handleMousePressed(MouseEvent e) {
// Store the mouse x and y coordinates with respect to the
// stage in the reference variables to use them in the drag event
// 點擊鼠標時,獲取鼠標在窗體上點擊時相對應窗體左上角的偏移
this.dragOffsetX = e.getScreenX() - stage.getX();
this.dragOffsetY = e.getScreenY() - stage.getY();
}
protected void handleMouseDragged(MouseEvent e) {
// Move the stage by the drag amount
// 拖動鼠標後,獲取鼠標相對應顯示器座標減去鼠標相對窗體的座標,並將其設置爲窗體在顯示器上的座標
stage.setX(e.getScreenX() - this.dragOffsetX);
stage.setY(e.getScreenY() - this.dragOffsetY);
}
/********************************************************************************************************
@date 2021/6/21
@author qiaowei
@brief Main window
********************************************************************************************************/
private Stage stage;
/********************************************************************************************************
@date 2021/6/21
@author qiaowei
@brief The x coordinate while pressing mouse
********************************************************************************************************/
private double dragOffsetX;
/********************************************************************************************************
@date 2021/6/21
@author qiaowei
@brief The y coordinate while pressing mouse
********************************************************************************************************/
private double dragOffsetY;
}