繁体   English   中英

使用JXML和Scene Builder移动未修饰的舞台Javafx

[英]Moving undecorated stage Javafx with JXML and Scene Builder

我正在尝试捕获Javafx中未修饰窗格的X和Y坐标。 该项目是使用javafx,JXML和Scene Builder创建的。 现在,SO上还有其他链接可以提示答案,但我完全无法实现。 我得到构建错误异常或得到NaN作为坐标。 用以下代码实现可移动窗格功能所需的最少修改是什么?

这是MainApp.java的代码(跳过导入过程,因为NetBeans很好地处理了它们)

public class MainApp extends Application {

@Override
public void start(Stage stage) throws Exception {
    GridPane MainPane = FXMLLoader.load(getClass().getResource("/fxml/Scene.fxml"));

    Scene scene = new Scene(MainPane, Color.TRANSPARENT);
    scene.getStylesheets().add("/styles/Styles.css");
    stage.initStyle(StageStyle.TRANSPARENT);

    stage.setScene(scene);
    stage.show();

}

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

}

FXMLController.java

public class FXMLController extends GridPane {

public void rootNodeClick(MouseEvent mouseEvent) {
System.out.println("click");
}

public void rootNodeDrag(MouseEvent mouseEvent) {
System.out.println("drag");
}
}

XML文件

 <?xml version="1.0" encoding="UTF-8"?>


 <GridPane fx:id="MainPane" alignment="CENTER" focusTraversable="true" maxHeight="550.0" maxWidth="600.0" minHeight="550.0" minWidth="600.0" nodeOrientation="LEFT_TO_RIGHT" onMouseClicked="#rootNodeClick" onMouseDragged="#rootNodeDrag" prefHeight="550.0" prefWidth="600.0" style="-fx-background-color: #ecf0f1; -fx-background-radius: 3;" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="FXMLController">
 <columnConstraints>
 <ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
 <ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
 </columnConstraints>
 <rowConstraints>
 <RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
 <RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
 <RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
 </rowConstraints>
 <effect>
 <InnerShadow blurType="GAUSSIAN" color="#ffffff44" height="10.0" radius="4.5" width="10.0" />
 </effect>
 </GridPane>

这样的事情应该可以解决问题。 按下鼠标时,我们捕获X和Y并设置拖动结束时的差值。

    final Delta dragDelta = new Delta();
    MainPane.setOnMousePressed(new EventHandler<MouseEvent>() {
        @Override
        public void handle(MouseEvent me) {
            if (me.getButton() == MouseButton.PRIMARY) {
                dragDelta.x = me.getSceneX();
                dragDelta.y = me.getSceneY();
            }
        }
    });

    MainPane.setOnMouseDragged(new EventHandler<MouseEvent>() {
        @Override
        public void handle(MouseEvent me) {
            if (me.getButton() == MouseButton.PRIMARY) {
                scene.getWindow().setX(me.getScreenX() - dragDelta.x);
                scene.setY(me.getScreenY() - dragDelta.y);
            }
        }
    });

这是三角洲

public class Delta {
    public double x;
    public double y;
}

最初的答案是在Stackoverflow上找到的,我很久以前就使用过,但是我再也找不到了,所以很抱歉原来的答案

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM