2014-03-03 71 views
1

我有一个按钮,我想拖动它并在一段时间后将其放下。 我在网上搜索了我遇到的最有用的东西是this链接。 从提供的链接中可以看出,只要鼠标按钮上移,按钮应该放下,无论光标在哪里移动,都会将该按钮拖动。 如何解决这个问题?提前致谢在Javafx中拖动按钮

回答

2

我设法做了一个borderPane并在其上放置了一个按钮。当您释放鼠标时,该按钮会被拖动并释放!

import javafx.application.Application; 
import javafx.event.EventHandler; 
import javafx.scene.Cursor; 
import javafx.scene.Scene; 
import javafx.scene.control.Button; 
import javafx.scene.input.MouseEvent; 
import javafx.scene.layout.BorderPane; 
import javafx.stage.Stage; 

public class ButtonDraggable extends Application { 

    @Override 
    public void start(Stage stage) throws Exception { 
     BorderPane borderPane = new BorderPane(); 
     borderPane.setPrefSize(800, 600); 
     final Button button = new Button("Drag ME !"); 

     final Delta dragDelta = new Delta(); 
     button.setOnMousePressed(new EventHandler<MouseEvent>() { 
      @Override 
      public void handle(MouseEvent mouseEvent) { 
       // record a delta distance for the drag and drop operation. 
       dragDelta.x = button.getLayoutX() - mouseEvent.getSceneX(); 
       dragDelta.y = button.getLayoutY() - mouseEvent.getSceneY(); 
       button.setCursor(Cursor.MOVE); 
      } 
     }); 
     button.setOnMouseReleased(new EventHandler<MouseEvent>() { 
      @Override 
      public void handle(MouseEvent mouseEvent) { 
       button.setCursor(Cursor.HAND); 
      } 
     }); 
     button.setOnMouseDragged(new EventHandler<MouseEvent>() { 
      @Override 
      public void handle(MouseEvent mouseEvent) { 
       button.setLayoutX(mouseEvent.getSceneX() + dragDelta.x); 
       button.setLayoutY(mouseEvent.getSceneY() + dragDelta.y); 
      } 
     }); 
     button.setOnMouseEntered(new EventHandler<MouseEvent>() { 
      @Override 
      public void handle(MouseEvent mouseEvent) { 
       button.setCursor(Cursor.HAND); 
      } 
     }); 

     borderPane.setCenter(button); 

     Scene scene = new Scene(borderPane); 

     stage.setScene(scene); 
     stage.show(); 

    } 

    // records relative x and y co-ordinates. 
    class Delta { 
     double x, y; 
    } 

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

} 

随时发表评论,如有任何疑问!

-AA

+0

我感谢你的回答,我的问题解决了,它足够清晰,足够详细,不会留下任何疑虑。 –

+1

@AdilliAdil我的快乐!乐意效劳 ! – ItachiUchiha

+0

我尝试了完全相同的代码。它与BorderPane失败。但它适用于AnchorPane。 –

1

嘿上面的代码没有工作对我来说这样做。

import javafx.application.Application; 
import javafx.scene.Scene; 
import javafx.scene.control.Button; 
import javafx.scene.layout.Pane; 
import javafx.stage.Stage; 

public class ButtonDraggable extends Application{ 
    @Override 
    public void start(Stage primaryStage){ 
     // button and pane are created 
     Button btOK = new Button("OK"); 
     Pane pane = new Pane(); 
     // this code drags the button 
     btOK.setOnMouseDragged(e -> { 
     btOK.setLayoutX(e.getSceneX()); 
     btOK.setLayoutY(e.getSceneY()); 
     }); 
     // button added to pane and pane added to scene 
     pane.getChildren().add(btOK); 
     Scene scene = new Scene(pane,200, 250); 
     primaryStage.setTitle("A Draggable button"); 
     primaryStage.setScene(scene); 
     primaryStage.show(); 
    } 

    public static void main(String[] args){ 
     Application.launch(args); 
    } 
} 
+0

适用于我。 – alhelal

+0

如果您也提供了方法参考的示例,那么它可能会有所帮助。 – alhelal