如何为透明 AnchorPane 启用拖动?
How to enable drag for transparent AnchorPane?
我需要让我的底部 AnchorPane
透明,我使用下面的代码来做到这一点。它有效,但之后我无法通过拖动移动我的应用程序?
Parent root = FXMLLoader.load(getClass().getResource("view/DashBoardForm.fxml"));
primaryStage.initStyle(StageStyle.TRANSPARENT);
Scene scene = new Scene(root);
scene.setFill(Color.TRANSPARENT);
primaryStage.setScene(scene);
primaryStage.show();
我无法在工作区中移动应用程序;我该如何解决?
你可以看到这个例子
public class Demo extends Application {
double x = 0;
double y = 0;
double sceneX = 0;
double sceneY = 0;
@Override
public void start(Stage primaryStage) {
var anchorPane = new AnchorPane();
var button = new Button("button");
var background = new Background(new BackgroundFill(Paint.valueOf("#FFB5C5"), new CornerRadii(15), new Insets(0)));
anchorPane.setBackground(background);
anchorPane.setBorder(new Border(new BorderStroke(Color.valueOf("#912CEE"), BorderStrokeStyle.SOLID, new CornerRadii(15), new BorderWidths(2), new Insets(0))));
button.setOnMousePressed(event -> {
x = event.getX();
y = event.getY();
});
button.setOnMouseDragged(event -> {
var sceneX = event.getSceneX();
var sceneY = event.getSceneY();
AnchorPane.setLeftAnchor(button, sceneX - x);
AnchorPane.setTopAnchor(button, sceneY - y);
});
anchorPane.getChildren().add(button);
AnchorPane.setTopAnchor(button, 100.0);
AnchorPane.setLeftAnchor(button, 100.0);
var scene = new Scene(anchorPane);
scene.setFill(Paint.valueOf("#ffffff00"));
scene.setOnMousePressed(event -> {
sceneX = event.getSceneX() - primaryStage.getX();
sceneY = event.getSceneY() - primaryStage.getY();
});
scene.setOnMouseDragged(event -> {
primaryStage.setX(event.getScreenX() - sceneX);
primaryStage.setY(event.getScreenY() - sceneY);
});
primaryStage.initStyle(StageStyle.TRANSPARENT);
primaryStage.setScene(scene);
primaryStage.setTitle("JavaFx");
primaryStage.setWidth(800);
primaryStage.setHeight(800);
primaryStage.show();
}
}
不过我觉得会有更好的办法
我需要让我的底部 AnchorPane
透明,我使用下面的代码来做到这一点。它有效,但之后我无法通过拖动移动我的应用程序?
Parent root = FXMLLoader.load(getClass().getResource("view/DashBoardForm.fxml"));
primaryStage.initStyle(StageStyle.TRANSPARENT);
Scene scene = new Scene(root);
scene.setFill(Color.TRANSPARENT);
primaryStage.setScene(scene);
primaryStage.show();
我无法在工作区中移动应用程序;我该如何解决?
你可以看到这个例子
public class Demo extends Application {
double x = 0;
double y = 0;
double sceneX = 0;
double sceneY = 0;
@Override
public void start(Stage primaryStage) {
var anchorPane = new AnchorPane();
var button = new Button("button");
var background = new Background(new BackgroundFill(Paint.valueOf("#FFB5C5"), new CornerRadii(15), new Insets(0)));
anchorPane.setBackground(background);
anchorPane.setBorder(new Border(new BorderStroke(Color.valueOf("#912CEE"), BorderStrokeStyle.SOLID, new CornerRadii(15), new BorderWidths(2), new Insets(0))));
button.setOnMousePressed(event -> {
x = event.getX();
y = event.getY();
});
button.setOnMouseDragged(event -> {
var sceneX = event.getSceneX();
var sceneY = event.getSceneY();
AnchorPane.setLeftAnchor(button, sceneX - x);
AnchorPane.setTopAnchor(button, sceneY - y);
});
anchorPane.getChildren().add(button);
AnchorPane.setTopAnchor(button, 100.0);
AnchorPane.setLeftAnchor(button, 100.0);
var scene = new Scene(anchorPane);
scene.setFill(Paint.valueOf("#ffffff00"));
scene.setOnMousePressed(event -> {
sceneX = event.getSceneX() - primaryStage.getX();
sceneY = event.getSceneY() - primaryStage.getY();
});
scene.setOnMouseDragged(event -> {
primaryStage.setX(event.getScreenX() - sceneX);
primaryStage.setY(event.getScreenY() - sceneY);
});
primaryStage.initStyle(StageStyle.TRANSPARENT);
primaryStage.setScene(scene);
primaryStage.setTitle("JavaFx");
primaryStage.setWidth(800);
primaryStage.setHeight(800);
primaryStage.show();
}
}
不过我觉得会有更好的办法