我剛剛制作了一個簡單的應用程序,其中包括 FlowPane 和一些帶有按鈕的 VBox。Main類是這樣的。public class Main extends Application { @Override public void start(Stage stage) { Gallery a = new Gallery(); a.setMaxWidth(200); a.setPrefWidth(200); Scene scene = new Scene(a); stage.setTitle("Welcome to JavaFX!"); stage.setScene(scene); stage.sizeToScene(); stage.show(); } public static void main(String[] args) { launch(args); }}這個 Gallery.class 是擴展 FlowPane 的主要后臺類。public class Gallery extends FlowPane{ public Gallery() { super(); PlotterPanel p1 = new PlotterPanel(this, "B1" ); getChildren().add(p1); PlotterPanel p2 = new PlotterPanel(this, "B2"); getChildren().add(p2); PlotterPanel p3 = new PlotterPanel(this, "B3" ); getChildren().add(p3); PlotterPanel p4 = new PlotterPanel(this, "B4" ); getChildren().add(p4); PlotterPanel p5 = new PlotterPanel(this, "B5" ); getChildren().add(p5); PlotterPanel p6 = new PlotterPanel(this, "B6" ); getChildren().add(p6); }}PlotterPanel是VBox,有Button并且可以在Gallery中拖放。public class PlotterPanel extends VBox{ private static final String TAB_DRAG_KEY = "titledpane"; private ObjectProperty<VBox> draggingTab; private Gallery mgallery; private PlotterPanel self; public PlotterPanel(Gallery gallery, String name) { super(); mgallery = gallery; setPrefWidth(100); setPrefHeight(100); self = this; Button btn = new Button(name); btn.setEffect(new DropShadow()); getChildren().add(btn); draggingTab = new SimpleObjectProperty<VBox>();問題是,當我在圖庫中拖動 PlotterPanel 時,第一次無法拖動它。我在第二次嘗試后工作了。當我開始拖動時,它顯示拖動框,但是當我嘗試放在另一個節點上時,鼠標點顯示 x 符號。但是當目標已經嘗試節點被拖動時,我可以放在該節點上。我怎樣才能使拖放在 JavaFX 中正常工作?
Javafx拖放第一次不起作用
慕婉清6462132
2023-06-21 16:05:59