gpt4 book ai didi

javafx - 如何在 Javafx 2.0 中制作可拖动节点。

转载 作者:行者123 更新时间:2023-12-01 07:10:47 27 4
gpt4 key购买 nike

如何在 Javafx 2.0 中制作可拖动节点。
JavaFX 专门用于 GUI 目的
我需要一些 sample 谢谢

最佳答案

Oracle 提供了 tutorial on draggable nodes .

这是makeDraggable教程中的方法:

private Node makeDraggable(final Node node) {
final DragContext dragContext = new DragContext();
final Group wrapGroup = new Group(node);

wrapGroup.addEventFilter(
MouseEvent.ANY,
new EventHandler<MouseEvent>() {
public void handle(final MouseEvent mouseEvent) {
if (dragModeActiveProperty.get()) {
// disable mouse events for all children
mouseEvent.consume();
}
}
});

wrapGroup.addEventFilter(
MouseEvent.MOUSE_PRESSED,
new EventHandler<MouseEvent>() {
public void handle(final MouseEvent mouseEvent) {
if (dragModeActiveProperty.get()) {
// remember initial mouse cursor coordinates
// and node position
dragContext.mouseAnchorX = mouseEvent.getX();
dragContext.mouseAnchorY = mouseEvent.getY();
dragContext.initialTranslateX =
node.getTranslateX();
dragContext.initialTranslateY =
node.getTranslateY();
}
}
});

wrapGroup.addEventFilter(
MouseEvent.MOUSE_DRAGGED,
new EventHandler<MouseEvent>() {
public void handle(final MouseEvent mouseEvent) {
if (dragModeActiveProperty.get()) {
// shift node from its initial position by delta
// calculated from mouse cursor movement
node.setTranslateX(
dragContext.initialTranslateX
+ mouseEvent.getX()
- dragContext.mouseAnchorX);
node.setTranslateY(
dragContext.initialTranslateY
+ mouseEvent.getY()
- dragContext.mouseAnchorY);
}
}
});

return wrapGroup;

}

有时您不需要过滤器和拖动上下文,可以通过对各种鼠标事件进行操作来做一些更简单的事情,例如 example :
static class Delta { double x, y; }
// make a node movable by dragging it around with the mouse.
private void enableDrag(final Circle circle) {
final Delta dragDelta = new Delta();
circle.setOnMousePressed(new EventHandler<MouseEvent>() {
@Override public void handle(MouseEvent mouseEvent) {
// record a delta distance for the drag and drop operation.
dragDelta.x = circle.getCenterX() - mouseEvent.getX();
dragDelta.y = circle.getCenterY() - mouseEvent.getY();
circle.getScene().setCursor(Cursor.MOVE);
}
});
circle.setOnMouseReleased(new EventHandler<MouseEvent>() {
@Override public void handle(MouseEvent mouseEvent) {
circle.getScene().setCursor(Cursor.HAND);
}
});
circle.setOnMouseDragged(new EventHandler<MouseEvent>() {
@Override public void handle(MouseEvent mouseEvent) {
circle.setCenterX(mouseEvent.getX() + dragDelta.x);
circle.setCenterY(mouseEvent.getY() + dragDelta.y);
}
});
circle.setOnMouseEntered(new EventHandler<MouseEvent>() {
@Override public void handle(MouseEvent mouseEvent) {
if (!mouseEvent.isPrimaryButtonDown()) {
circle.getScene().setCursor(Cursor.HAND);
}
}
});
circle.setOnMouseExited(new EventHandler<MouseEvent>() {
@Override public void handle(MouseEvent mouseEvent) {
if (!mouseEvent.isPrimaryButtonDown()) {
circle.getScene().setCursor(Cursor.DEFAULT);
}
}
});
}

用于拖动节点的相同技术也可用于 drag around stages :
static class Delta { double x, y; }
/** makes a stage draggable using a given node */
public static void makeDraggable(final Stage stage, final Node byNode) {
final Delta dragDelta = new Delta();
byNode.setOnMousePressed(new EventHandler<MouseEvent>() {
@Override public void handle(MouseEvent mouseEvent) {
// record a delta distance for the drag and drop operation.
dragDelta.x = stage.getX() - mouseEvent.getScreenX();
dragDelta.y = stage.getY() - mouseEvent.getScreenY();
byNode.setCursor(Cursor.MOVE);
}
});
byNode.setOnMouseReleased(new EventHandler<MouseEvent>() {
@Override public void handle(MouseEvent mouseEvent) {
byNode.setCursor(Cursor.HAND);
}
});
byNode.setOnMouseDragged(new EventHandler<MouseEvent>() {
@Override public void handle(MouseEvent mouseEvent) {
stage.setX(mouseEvent.getScreenX() + dragDelta.x);
stage.setY(mouseEvent.getScreenY() + dragDelta.y);
}
});
byNode.setOnMouseEntered(new EventHandler<MouseEvent>() {
@Override public void handle(MouseEvent mouseEvent) {
if (!mouseEvent.isPrimaryButtonDown()) {
byNode.setCursor(Cursor.HAND);
}
}
});
byNode.setOnMouseExited(new EventHandler<MouseEvent>() {
@Override public void handle(MouseEvent mouseEvent) {
if (!mouseEvent.isPrimaryButtonDown()) {
byNode.setCursor(Cursor.DEFAULT);
}
}
});
}

用于在父节点(包含多个子节点)周围拖动的示例。这个例子比上面基于圆的例子更通用,因为它不依赖于大多数节点没有的 centerX/Y 属性,而是在 layoutX/Y 上工作,这些属性可用于放置在父组或 Pane 中的所有节点.
import javafx.application.Application;
import javafx.scene.*;
import javafx.scene.layout.*;
import javafx.scene.paint.Color;
import javafx.scene.shape.Circle;
import javafx.scene.text.Text;
import javafx.scene.text.TextBoundsType;
import javafx.stage.Stage;

public class TextOnCircleWithDragging extends Application {
private static final int W = 400;
private static final int H = 400;
private static final int R = 15;

@Override
public void start(Stage stage) {
final StackPane circleWithText = new StackPane(
createCircle(),
createText()
);
circleWithText.relocate(
W/2 - R/2,
H/2 - R/2
);

makeDraggable(circleWithText);

stage.setScene(
new Scene(
new Pane(circleWithText),
W, H
)
);
stage.show();
}

private Circle createCircle() {
final Circle circle = new Circle(R);
circle.setFill(Color.PALEGREEN);
circle.relocate(0, 0);

return circle;
}

private Text createText() {
final Text text = new Text("A");
text.setBoundsType(TextBoundsType.VISUAL);

return text;
}

private void makeDraggable(Node node) {
final Delta dragDelta = new Delta();

node.setOnMouseEntered(me -> {
if (!me.isPrimaryButtonDown()) {
node.getScene().setCursor(Cursor.HAND);
}
});
node.setOnMouseExited(me -> {
if (!me.isPrimaryButtonDown()) {
node.getScene().setCursor(Cursor.DEFAULT);
}
});
node.setOnMousePressed(me -> {
if (me.isPrimaryButtonDown()) {
node.getScene().setCursor(Cursor.DEFAULT);
}
dragDelta.x = me.getX();
dragDelta.y = me.getY();
node.getScene().setCursor(Cursor.MOVE);
});
node.setOnMouseReleased(me -> {
if (!me.isPrimaryButtonDown()) {
node.getScene().setCursor(Cursor.DEFAULT);
}
});
node.setOnMouseDragged(me -> {
node.setLayoutX(node.getLayoutX() + me.getX() - dragDelta.x);
node.setLayoutY(node.getLayoutY() + me.getY() - dragDelta.y);
});
}

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

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

滞后调整

如果您看到拖动的节点落后于光标并希望解决该问题,请参阅 Xanatos 对以下问题的回答:
  • https://stackoverflow.com/a/22695105/1155209

  • 他建议您可能希望设置的位置:
    -Dprism.vsync=false

    解决问题 https://bugs.openjdk.java.net/browse/JDK-8087922 .

    关于javafx - 如何在 Javafx 2.0 中制作可拖动节点。,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17312734/

    27 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com