Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/357.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 在其父窗格内为可拖动窗格设置边界限制_Java_Javafx_Pane - Fatal编程技术网

Java 在其父窗格内为可拖动窗格设置边界限制

Java 在其父窗格内为可拖动窗格设置边界限制,java,javafx,pane,Java,Javafx,Pane,我有一个可拖动的窗格,它位于另一个窗格中。我想使子窗格只能在父窗格的边界内拖动,但默认情况下,子窗格可以拖动到任何地方。如何解决这个问题。看看这个。应用程序生成可拖动的标签,这些标签可以在场景中移动。要设置边界限制,需要使用以下技术:在onmousedradded处理程序中,我们计算节点的当前位置,如果它不满足某些条件,我们不会修改它。特别是: label.setOnMouseDragged(new EventHandler<MouseEvent>() { @Override

我有一个可拖动的窗格,它位于另一个窗格中。我想使子窗格只能在父窗格的边界内拖动,但默认情况下,子窗格可以拖动到任何地方。如何解决这个问题。

看看这个。应用程序生成可拖动的标签,这些标签可以在场景中移动。要设置边界限制,需要使用以下技术:在onmousedradded处理程序中,我们计算节点的当前位置,如果它不满足某些条件,我们不会修改它。特别是:

label.setOnMouseDragged(new EventHandler<MouseEvent>() {
    @Override public void handle(MouseEvent mouseEvent) {

      //Sets the drag boundaries limit
      double newX = mouseEvent.getSceneX() + dragDelta.x;
      if (newX > 200 || newX < 10) {
        return;
      }

      label.setLayoutX(mouseEvent.getSceneX() + dragDelta.x);
      label.setLayoutY(mouseEvent.getSceneY() + dragDelta.y);
    }
  });
label.setonMouseDrawed(新的EventHandler(){
@重写公共无效句柄(MouseEvent MouseEvent){
//设置拖动边界限制
double newX=mouseEvent.getSceneX()+dragDelta.x;
如果(newX>200 | | newX<10){
返回;
}
label.setLayoutX(mouseEvent.getSceneX()+dragDelta.x);
label.setLayoutY(mouseEvent.getSceneY()+dragDelta.y);
}
});
添加到,而不是

 if (newX > 200 || newX < 10) { return; }
其中,
outSideParentBounds
由以下内容定义:

private boolean outSideParentBounds( Bounds childBounds, double newX, double newY) {

        Bounds parentBounds = getLayoutBounds();

        //check if too left
        if( parentBounds.getMaxX() <= (newX + childBounds.getMaxX()) ) {
            return true ;
        }

        //check if too right
        if( parentBounds.getMinX() >= (newX + childBounds.getMinX()) ) {
            return true ;
        }

        //check if too down
        if( parentBounds.getMaxY() <= (newY + childBounds.getMaxY()) ) {
            return true ;
        }

        //check if too up
        if( parentBounds.getMinY() >= (newY + childBounds.getMinY()) ) {
            return true ;
        }

        return false;

        /* Alternative implementation 
        Point2D topLeft = new Point2D(newX + childBounds.getMinX(), newY + childBounds.getMinY());
        Point2D topRight = new Point2D(newX + childBounds.getMaxX(), newY + childBounds.getMinY());
        Point2D bottomLeft = new Point2D(newX + childBounds.getMinX(), newY + childBounds.getMaxY());
        Point2D bottomRight = new Point2D(newX + childBounds.getMaxX(), newY + childBounds.getMaxY());
        Bounds newBounds = BoundsUtils.createBoundingBox(topLeft, topRight, bottomLeft, bottomRight);

        return ! parentBounds.contains(newBounds);
         */
    }
private boolean outSideParentBounds(Bounds-childBounds、double-newX、double-newY){
Bounds parentBounds=getLayoutBounds();
//检查是否太左
if(parentBounds.getMaxX()=(newX+childBounds.getMinX()){
返回true;
}
//检查是否太低
if(parentBounds.getMaxY()=(newY+childBounds.getMinY()){
返回true;
}
返回false;
/*替代实施
Point2D topLeft=新的Point2D(newX+childBounds.getMinX(),newY+childBounds.getMinY());
Point2D topRight=newPoint2D(newX+childBounds.getMaxX(),newY+childBounds.getMinY());
Point2D bottomLeft=newPoint2D(newX+childBounds.getMinX(),newY+childBounds.getMaxY());
Point2D bottomRight=newPoint2D(newX+childBounds.getMaxX(),newY+childBounds.getMaxY());
Bounds newBounds=BoundsUtils.createBoundingBox(左上、右上、左下、右下);
return!parentBounds.contains(新边界);
*/
}

真的非常感谢您这对我来说很有用。但这段代码中的一个问题是,如果我们保留“if”语句,它将使窗格比删除它慢得多。如果能解决这个问题,我会很高兴的。谢谢,我认为这是一个局部的回答。它回答“如何限制”,但忽略“父窗格的边界内”。您可以使用依赖于边界窗格边界的属性,而不是硬编码的值。
private boolean outSideParentBounds( Bounds childBounds, double newX, double newY) {

        Bounds parentBounds = getLayoutBounds();

        //check if too left
        if( parentBounds.getMaxX() <= (newX + childBounds.getMaxX()) ) {
            return true ;
        }

        //check if too right
        if( parentBounds.getMinX() >= (newX + childBounds.getMinX()) ) {
            return true ;
        }

        //check if too down
        if( parentBounds.getMaxY() <= (newY + childBounds.getMaxY()) ) {
            return true ;
        }

        //check if too up
        if( parentBounds.getMinY() >= (newY + childBounds.getMinY()) ) {
            return true ;
        }

        return false;

        /* Alternative implementation 
        Point2D topLeft = new Point2D(newX + childBounds.getMinX(), newY + childBounds.getMinY());
        Point2D topRight = new Point2D(newX + childBounds.getMaxX(), newY + childBounds.getMinY());
        Point2D bottomLeft = new Point2D(newX + childBounds.getMinX(), newY + childBounds.getMaxY());
        Point2D bottomRight = new Point2D(newX + childBounds.getMaxX(), newY + childBounds.getMaxY());
        Bounds newBounds = BoundsUtils.createBoundingBox(topLeft, topRight, bottomLeft, bottomRight);

        return ! parentBounds.contains(newBounds);
         */
    }