gpt4 book ai didi

javafx-2 - 如何拖动JavaFX的未修饰窗口(阶段)

转载 作者:行者123 更新时间:2023-12-04 02:40:49 28 4
gpt4 key购买 nike

我有这个未修饰的窗口:

public static void initStartPage(final Stage primaryStage) {

final Stage startPage = new Stage();
startPage.initStyle(StageStyle.UNDECORATED);
//startPage.initOwner(primaryStage);
//startPage.toFront();
Scene scene = new Scene(agentsPanel(), 900, 500);
startPage.setScene(scene);
startPage.show();

}


我想知道如何使其成为可拖动的未修饰窗口?
当用户使用鼠标右键选择窗口,然后在按住鼠标按钮的同时移动鼠标时,我想更改其位置。

附言我测试了此解决方案,但无法正常工作:

private static FlowPane flow;
private static BorderPane bpi;

public static void initStartPage(final Stage primaryStage) {

final Stage startPage = new Stage();
startPage.initStyle(StageStyle.UNDECORATED);
startPage.initOwner(primaryStage);
//startPage.toFront();
Scene scene = new Scene(agentsPanel(primaryStage), 900, 500);
startPage.setScene(scene);
startPage.show();

}

private static double xOffset = 0;
private static double yOffset = 0;

public static BorderPane agentsPanel(final Stage primaryStage) {

BorderPane bp = new BorderPane();
bp.setPrefSize(900, 500);
bp.setMaxSize(900, 500);

HBox thb = new HBox(10); // Set spacing between each child into the HBox
thb.setPadding(new Insets(15, 15, 15, 15));


HBox bhb = new HBox(10); // Set spacing between each child into the HBox
bhb.setPadding(new Insets(15, 15, 15, 15));

bp.setTop(thb);
bp.setBottom(bhb);
bp.setCenter(navigationPanel());


bp.setOnMousePressed(new EventHandler<MouseEvent>() {
@Override
public void handle(MouseEvent event) {
xOffset = event.getSceneX();
yOffset = event.getSceneY();
}
});
bp.setOnMouseDragged(new EventHandler<MouseEvent>() {
@Override
public void handle(MouseEvent event) {
primaryStage.setX(event.getScreenX() - xOffset);
primaryStage.setY(event.getScreenY() - yOffset);
}
});

return bp;

}

最佳答案

只需将setOnMousePressed方法更改为此:

bp.setOnMousePressed(new EventHandler<MouseEvent>() {
@Override
public void handle(MouseEvent event) {
xOffset = primaryStage.getX() - event.getScreenX();
yOffset = primaryStage.getY() - event.getScreenY();
}
});


和您的setOnMouseDragged到此:

bp.setOnMouseDragged(new EventHandler<MouseEvent>() {
@Override
public void handle(MouseEvent event) {
primaryStage.setX(event.getScreenX() + xOffset);
primaryStage.setY(event.getScreenY() + yOffset);
}
});

关于javafx-2 - 如何拖动JavaFX的未修饰窗口(阶段),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18173956/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com