【问题标题】:JavaFX: Add children to ScrollPaneJavaFX:将子级添加到 ScrollPane
【发布时间】:2016-07-23 22:11:21
【问题描述】:

我有一个窗格对象,用户可以在其中拖放各种 ImageView。为此,我使用了 pane.getChildren(imageViewObject) 方法

现在,将 Pane 替换为 ScrollPane 后,就没有这个方法了。所以我不知道如何解决这个问题。

提前谢谢你

【问题讨论】:

    标签: java javafx scrollpane


    【解决方案1】:

    您只能使用ScrollPane 指定一个节点。要创建包含多个组件的滚动视图,请使用布局容器或 Group 类。

    Pane pane = ...;
    ScrollPane sp = new ScrollPane();
    sp.setContent(pane);
    

    示例

    import javafx.application.Application;
    import static javafx.application.Application.launch;
    import javafx.geometry.Insets;
    import javafx.scene.Scene;
    import javafx.scene.control.Button;
    import javafx.scene.control.ScrollPane;
    import javafx.scene.layout.VBox;
    import javafx.stage.Stage;
    
    /**
     *
     * @author kachna
     */
    public class Test extends Application {
    
        @Override
        public void start(Stage stage) throws Exception {
            VBox root = new VBox();
            root.getChildren().addAll(new Button("button1"), new Button("button2"), new Button("button3"));
            root.setSpacing(10);
            root.setPadding(new Insets(10));
            ScrollPane sp = new ScrollPane();
            sp.setContent(root);
            sp.setPannable(true); // it means that the user should be able to pan the viewport by using the mouse.
            Scene scene = new Scene(sp, 100, 100);
            stage.setScene(scene);
            stage.show();
        }
    
        public static void main(String[] args) {
            launch(args);
        }
    
    }
    

    【讨论】:

    • 这不是我正在寻找的解决方案。问题是现在,ScrollPane 只能从侧边栏滚动。
    • 致电scrollPane.setPannable(true);
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多