当我的框架设置为UNdecorated时,如何在JAVAFX中使我的Anchorpane拖动?

时间:2017-11-24 15:45:38

标签: java javafx

这是我在视图类中的代码 这只是import

package View;

import javafx.application.Application;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;

import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.Pane;
import javafx.stage.Stage;
import javafx.stage.StageStyle;

import java.awt.*;

别介意我的sql方法

import static SQL.MySQL.connectDB;

public class Client extends Application {

    private static Dimension  screenSize = Toolkit.getDefaultToolkit().getScreenSize();

这是我的JavaFX框架。请记住,我使用FXML场景构建器

@Override

public void start(Stage primaryStage) throws Exception{
    Parent root = FXMLLoader.load(getClass().getResource("design.fxml"));
    primaryStage.initStyle(StageStyle.UNDECORATED);
    primaryStage.setScene(new Scene(root, (screenSize.getWidth()/2), screenSize.getHeight()/2));

    primaryStage.show();

}
public static void main(String[] args)
{
    connectDB();
    launch(args);
}

1 个答案:

答案 0 :(得分:1)

public class Client extends Application {

    private double offset_x;
    private double offset_y;

    private static Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();

    @Override
    public void start(Stage primaryStage) throws Exception {
        Parent root = FXMLLoader.load(getClass().getResource("design.fxml"));

        Scene scene = new Scene(root, (screenSize.getWidth()/2), screenSize.getHeight()/2);
        scene.setOnMousePressed(event -> {
            offset_x = event.getSceneX();
            offset_y = event.getSceneY();
        });

        scene.setOnMouseDragged(event -> {
            primaryStage.setX(event.getScreenX() - offset_x);
            primaryStage.setY(event.getScreenY() - offset_y);
        });

        primaryStage.initStyle(StageStyle.UNDECORATED);
        primaryStage.setScene(scene);
        primaryStage.show();
    }

    public static void main(String[] args) {
        launch(args);
    }
}