简体   繁体   English

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

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

I'm trying to capture X and Y coordinates of a undecorated pane in Javafx. 我正在尝试捕获Javafx中未修饰窗格的X和Y坐标。 The project is created using javafx, JXML and Scene Builder. 该项目是使用javafx,JXML和Scene Builder创建的。 Now there are other links on SO which hints to the answer, but I'm quite not able to implement it. 现在,SO上还有其他链接可以提示答案,但我完全无法实现。 Either I get build error exception or get NaN as coordinates. 我得到构建错误异常或得到NaN作为坐标。 What are minimum modifications required to implement movable panes functionality with following code? 用以下代码实现可移动窗格功能所需的最少修改是什么?

Here is the code for MainApp.java (skipping imports as they are handled by NetBeans very well) 这是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 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");
}
}

FXML 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>

Something like this should do the trick. 这样的事情应该可以解决问题。 When the mouse is pressed, we capture X and Y and set the difference when the drag is over. 按下鼠标时,我们捕获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);
            }
        }
    });

Here is Delta 这是三角洲

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

The original answer was found on Stackoverflow, I used it long time ago, but I can't find it again, so sorry for the original answer 最初的答案是在Stackoverflow上找到的,我很久以前就使用过,但是我再也找不到了,所以很抱歉原来的答案

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

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