correct way to move a node by dragging in javafx 2?

前端 未结 8 810
感动是毒
感动是毒 2020-12-01 05:50

I\'m converting a Swing/Graphics2D app with a lot of custom painting to a JavaFX2 app. Although I absolutely love the new API, I seem to have a performance problem when pain

8条回答
  •  Happy的楠姐
    2020-12-01 06:09

    here is the code to drag and drop label using mouse in javafx

    @FXML
    public void lblDragMousePressed(MouseEvent m)
    {
        System.out.println("Mouse is pressed");
    
        prevLblCordX= (int) lblDragTest.getLayoutX();
        prevLblCordY= (int) lblDragTest.getLayoutY();
        prevMouseCordX= (int) m.getX();
        prevMouseCordY= (int) m.getY();     
    }
    //set this method on mouse released event for lblDrag
    @FXML
    public void lblDragMouseReleased(MouseEvent m)
    {       
        System.out.println("Label Dragged");    
    }
    // set this method on Mouse Drag event for lblDrag
    @FXML
    public void lblDragMouseDragged(MouseEvent m)
    {   
        diffX= (int) (m.getX()- prevMouseCordX);
        diffY= (int) (m.getY()-prevMouseCordY );        
        int x = (int) (diffX+lblDragTest.getLayoutX()-rootAnchorPane.getLayoutX());
        int y = (int) (diffY+lblDragTest.getLayoutY()-rootAnchorPane.getLayoutY());     
        if (y > 0 && x > 0 && y < rootAnchorPane.getHeight() && x < rootAnchorPane.getWidth()) 
        { 
         lblDragTest.setLayoutX(x);
         lblDragTest.setLayoutY(y);
        }
    }
    

提交回复
热议问题