在舞台上拖动节点

时间:2014-03-14 21:04:22

标签: javafx javafx-2 javafx-8

我有一张可以缩放的图表

final BarChart<String, Number> bc
            = new BarChart<String, Number>(xAxis, yAxis);

final double SCALE_DELTA = 1.1;
        bc.setOnScroll(new EventHandler<ScrollEvent>()
        {
            @Override
            public void handle(ScrollEvent event)
            {
                event.consume();

                if (event.getDeltaY() == 0)
                {
                    return;
                }

                double scaleFactor = (event.getDeltaY() > 0) ? SCALE_DELTA : 1 / SCALE_DELTA;

                bc.setScaleX(bc.getScaleX() * scaleFactor);
                bc.setScaleY(bc.getScaleY() * scaleFactor);
            }
        });

我可以放大和缩小但我无法拖动图表以导航到特定的缩放区域。如何通过单击并按住图表来拖动图表?

P.S我测试了这个代码位我没有得到任何结果

label.setOnMousePressed(new EventHandler<MouseEvent>()
            {
                @Override
                public void handle(MouseEvent mouseEvent)
                {
                // record a delta distance for the drag and drop operation.
                    // treePane.x = label.getLayoutX() - mouseEvent.getSceneX();
                    treePane.setLayoutX(label.getLayoutX() - mouseEvent.getSceneX()); //= label.getLayoutX() - mouseEvent.getSceneX();

                    //treePane.y = label.getLayoutY() - mouseEvent.getSceneY();
                    treePane.setLayoutY(label.getLayoutY() - mouseEvent.getSceneY());

                    label.setCursor(Cursor.MOVE);
                }
            });
            label.setOnMouseReleased(new EventHandler<MouseEvent>()
            {
                @Override
                public void handle(MouseEvent mouseEvent)
                {
                    label.setCursor(Cursor.HAND);
                }
            });
            label.setOnMouseDragged(new EventHandler<MouseEvent>()
            {
                @Override
                public void handle(MouseEvent mouseEvent)
                {
                    label.setLayoutX(mouseEvent.getSceneX() + treePane.getLayoutX());
                    label.setLayoutY(mouseEvent.getSceneY() + treePane.getLayoutY());
                }
            });
            label.setOnMouseEntered(new EventHandler<MouseEvent>()
            {
                @Override
                public void handle(MouseEvent mouseEvent)
                {
                    label.setCursor(Cursor.HAND);
                }
            });

知道我哪里错了吗?

1 个答案:

答案 0 :(得分:0)

您可以将此代码用于拖放标签这是javafx的解决方案

@FXML
public void lblDragMouseDragged(MouseEvent m)
{
    System.out.println("Mouse Dragged");
    System.out.println(lblDragTest.getLayoutX());
    System.out.println(lblDragTest.getLayoutY());

    int diffX;
    int diffY;
    int x = (int) (m.getX()+lblDragTest.getLayoutX()-rootAnchorPane.getLayoutX());
    int y = (int) (m.getY()+lblDragTest.getLayoutY()-rootAnchorPane.getLayoutY()); 



    if (y > 0 && x > 0 && y < rootAnchorPane.getHeight() && x < rootAnchorPane.getWidth()) 
    { 
     lblDragTest.setLayoutX(x);
     lblDragTest.setLayoutY(y);
    }
}