是否可以深度复制Label (或任何其他节点),以便创建具有相同属性值的新Label对象,然后将其放入Dragboard ? 我要实现此功能:将标签拖放到Pane中的某个位置。 在放置位置的“窗格”中创建一个具有相同属性值的新Label对象。 ...
提示:本站收集StackOverFlow近2千万问答,支持中英文搜索,鼠标放在语句上弹窗显示对应的参考中文或英文, 本站还提供 中文繁体 英文版本 中英对照 版本,有任何建议请联系yoyou2525@163.com。
我有一个StackPane,可以在窗格中拖动它(窗格是堆栈窗格的父布局),但是现在我可以将其拖出窗格。 当前,我无法找到一种方法来阻止将StackPane从窗格中拖出窗格。
当您按下StackPane时,我的事件处理程序是:
EventHandler<MouseEvent> circleOnMousePressedEventHandler =
new EventHandler<MouseEvent>() {
@Override
public void handle(MouseEvent t) {
currentStackPane = ((StackPane)(t.getSource()));
orgSceneX = t.getSceneX();
orgSceneY = t.getSceneY();
layoutX = currentStackPane.getLayoutX();
layoutY = currentStackPane.getLayoutY();
}
};
我拖动StackPane时的事件处理程序:
EventHandler<MouseEvent> circleOnMouseDraggedEventHandler =
new EventHandler<MouseEvent>() {
@Override
public void handle(MouseEvent t) {
double offsetX = t.getSceneX() - orgSceneX;
double offsetY = t.getSceneY() - orgSceneY;
currentStackPane.setTranslateX(offsetX);
currentStackPane.setTranslateY(offsetY);
}
};
我放开鼠标时的事件处理程序:
new EventHandler<MouseEvent>() {
@Override
public void handle(MouseEvent t) {
currentStackPane.setLayoutX(layoutX + ((StackPane)(t.getSource())).getTranslateX());
currentStackPane.setLayoutY(layoutY + ((StackPane)(t.getSource())).getTranslateY());
currentStackPane.setTranslateX(0);
currentStackPane.setTranslateY(0);
}
};
return circleOnMouseReleaseEventHandler;
}
有没有一种方法可以使堆栈窗格只能在窗格中拖动。
谢谢。
编辑:
我试过的
我创建了这种方法:
private boolean outSideParentBounds( Bounds childBounds, double newX, double newY) {
Bounds parentBounds = centerPane.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;
}
现在,我的拖动事件处理程序如下所示:
EventHandler<MouseEvent> circleOnMouseDraggedEventHandler =
new EventHandler<MouseEvent>() {
@Override
public void handle(MouseEvent t) {
double offsetX = t.getSceneX() - orgSceneX;
double offsetY = t.getSceneY() - orgSceneY;
if( outSideParentBounds(currentStackPane.getLayoutBounds(), layoutX + ((StackPane)(t.getSource())).getTranslateX(),
layoutY + ((StackPane)(t.getSource())).getTranslateY()) ) {
return;
}
currentStackPane.setTranslateX(offsetX);
currentStackPane.setTranslateY(offsetY);
}
};
但是,一旦我将其拖出窗格,它似乎就卡住了窗格的边缘,并且您再也无法将其拖拽了,这似乎不起作用。
我注意到的一个问题是您在计算outSideParentBounds时没有考虑offSet值。 而且,只有在满足条件时,您才能更新转换值。 相反,您必须始终在拖动处理程序中将转换值更新为所需值。
您的拖动处理程序应如下更改:
EventHandler<MouseEvent> circleOnMouseDraggedEventHandler = e -> {
// Offset of drag
double offsetX = e.getSceneX() - sceneX;
double offsetY = e.getSceneY() - sceneY;
// Taking parent bounds
Bounds parentBounds = currentStackPane.getParent().getLayoutBounds();
// Drag node bounds
double currPaneLayoutX = currentStackPane.getLayoutX();
double currPaneWidth = currentStackPane.getWidth();
double currPaneLayoutY = currentStackPane.getLayoutY();
double currPaneHeight = currentStackPane.getHeight();
if ((currPaneLayoutX + offsetX > -1) && (currPaneLayoutX + offsetX < parentBounds.getWidth() - currPaneWidth)) {
// If the dragNode bounds is within the parent bounds, then you can set the offset value.
currentStackPane.setTranslateX(offsetX);
} else if (currPaneLayoutX + offsetX < 0) {
// If the sum of your offset and current layout position is negative, then you ALWAYS update your translate value to negative layout value
// which makes the final layout position to 0 in mouse released event.
currentStackPane.setTranslateX(-currPaneLayoutX);
} else {
// If your dragNode bounds are outside parent bounds,ALWAYS setting the translate value that fits your node at end.
currentStackPane.setTranslateX(parentBounds.getWidth() - currPaneLayoutX - currPaneWidth);
}
if ((currPaneLayoutY + offsetY < parentBounds.getHeight() - currPaneHeight) && (currPaneLayoutY + offsetY > -1)) {
currentStackPane.setTranslateY(offsetY);
} else if (currPaneLayoutY + offsetY < 0) {
currentStackPane.setTranslateY(-currPaneLayoutY);
} else {
currentStackPane.setTranslateY(parentBounds.getHeight() - currPaneLayoutY - currPaneHeight);
}
};
始终更新的原因是::拖动事件处理程序不会为您拖动的每个像素调用。 假设您要将一个节点拖动500px。 如果您打印offsetX / Y值,您会注意到offsetX / Y值并非始终是顺序的,或者不会为每个像素打印。 它可能会跳一些值。 它们可能会根据您拖动的速度而变化。 因此,您可能会跳过所需的终点。
为了验证这一点,一旦您熟悉上面的代码,请尝试注释代码的“其他”部分,并仅保留第一个if条件。 尝试快速拖动节点,您会注意到您的节点将随机终止在所需端点的前面/后面。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.