sách gpt4 ai đã đi

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

In lại 作者:行者123 更新时间:2023-12-04 02:40:49 28 4
mua khóa gpt4 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() {
@Ghi đè
public void handle(MouseEvent event) {
xOffset = event.getSceneX();
yOffset = event.getSceneY();
}
});
bp.setOnMouseDragged(new EventHandler() {
@Ghi đè
public void handle(MouseEvent event) {
primaryStage.setX(event.getScreenX() - xOffset);
primaryStage.setY(event.getScreenY() - yOffset);
}
});

return bp;

}

1 Câu trả lời

只需将setOnMousePressed方法更改为此:

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


和您的setOnMouseDragged到此:

bp.setOnMouseDragged(new EventHandler() {
@Ghi đè
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
行者123
Hồ sơ cá nhân

Tôi là một lập trình viên xuất sắc, rất giỏi!

Nhận phiếu giảm giá Didi Taxi miễn phí
Mã giảm giá Didi Taxi
Giấy chứng nhận ICP Bắc Kinh số 000000
Hợp tác quảng cáo: 1813099741@qq.com 6ren.com