Javafx中的拖动按钮
我有一个按钮,我想拖一段时间后把它放下。 我在网上搜索到的最有用的东西是链接。 与提供的链接类似,我希望无论光标移动到哪里,都能拖动按钮,只有当鼠标按钮向上移动时,按钮才会下降。Javafx中的拖动按钮,java,drag-and-drop,javafx,draggable,drag,Java,Drag And Drop,Javafx,Draggable,Drag,我有一个按钮,我想拖一段时间后把它放下。 我在网上搜索到的最有用的东西是链接。 与提供的链接类似,我希望无论光标移动到哪里,都能拖动按钮,只有当鼠标按钮向上移动时,按钮才会下降。 如何解决这个问题呢?提前谢谢我制作了一个边框窗格,并在其上放置了一个按钮。释放鼠标时,按钮会被拖动和释放 import javafx.application.Application; import javafx.event.EventHandler; import javafx.scene.Cursor; import
如何解决这个问题呢?提前谢谢我制作了一个边框窗格,并在其上放置了一个按钮。释放鼠标时,按钮会被拖动和释放
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);
}
}
导入javafx.application.application;
导入javafx.event.EventHandler;
导入javafx.scene.Cursor;
导入javafx.scene.scene;
导入javafx.scene.control.Button;
导入javafx.scene.input.MouseEvent;
导入javafx.scene.layout.BorderPane;
导入javafx.stage.stage;
公共类ButtonDragable扩展应用程序{
@凌驾
public void start(Stage)引发异常{
BorderPane BorderPane=新的BorderPane();
borderPane.setPrefSize(800600);
最终按钮=新按钮(“拖动我!”);
最终增量dragDelta=新增量();
setOnMousePressed(新的EventHandler(){
@凌驾
公共无效句柄(MouseEvent MouseEvent){
//记录拖放操作的增量距离。
dragDelta.x=button.getLayoutX()-mouseEvent.getSceneX();
dragDelta.y=button.getLayoutY()-mouseEvent.getSceneY();
按钮。设置光标(光标。移动);
}
});
setOnMouseReleased(新的EventHandler(){
@凌驾
公共无效句柄(MouseEvent MouseEvent){
按钮。设置光标(光标。指针);
}
});
setOnMouseDrawed(新的EventHandler(){
@凌驾
公共无效句柄(MouseEvent MouseEvent){
setLayoutX(mouseEvent.getSceneX()+dragDelta.x);
设置布局(mouseEvent.getSceneY()+dragDelta.y);
}
});
setOnMouseCentered(新的EventHandler(){
@凌驾
公共无效句柄(MouseEvent MouseEvent){
按钮。设置光标(光标。指针);
}
});
边框窗格。设置中心(按钮);
场景=新场景(边框窗格);
舞台场景;
stage.show();
}
//记录相对x和y坐标。
类三角洲{
双x,y;
}
公共静态void main(字符串参数[]){
发射(args);
}
}
如果有任何疑问,请随时发表评论
-AA嘿,上面的代码对我不起作用,但这确实起作用
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);
}
}
谢谢你的回答,我的问题已经解决了,它足够清晰和详细,不会留下任何疑问。@AdilliAdil,我的荣幸!很乐意帮忙!我尝试了完全相同的代码。它使用BorderPane失败。但它对AnchorPane有效。对我和adilif都有效。如果您提供了一个方法参考的示例,那么它可能会有所帮助。