您可以使用JavaFX中的Drag and Drop API来创建在鼠标事件中移动标签的应用程序。
这里是一个简单的示例,演示如何在标签上使用Drag and Drop功能:
import javafx.application.Application; import javafx.geometry.Pos; import javafx.scene.Scene; import javafx.scene.control.Label; import javafx.scene.input.DragEvent; import javafx.scene.input.MouseEvent; import javafx.scene.layout.HBox; import javafx.stage.Stage;
public class LabelDragAndDropExample extends Application {
private Label label;
@Override
public void start(Stage primaryStage) throws Exception {
label = new Label("拖动我");
label.setOnDragDetected((MouseEvent event) -> {
label.startFullDrag();
});
label.setOnMouseDragged((MouseEvent event) -> {
label.setLayoutX(event.getX() + label.getLayoutX());
label.setLayoutY(event.getY() + label.getLayoutY());
});
HBox hbox = new HBox(label);
hbox.setAlignment(Pos.CENTER);
Scene scene = new Scene(hbox, 300, 200);
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
在这个示例中,我们将标签添加到HBox中,并在使用鼠标拖动标签时更改标签的布局X和Y属性。 我们还使用setOnDragDetected()方法启用“整个拖动”来启用Drag and Drop API。
这将使鼠标在整个屏幕上拖动标签,而不仅仅是在应用程序窗口的边界内。
上一篇:标签不会消失
下一篇:标签捕获后视频无法播放。