java JavaFX:将子项添加到 ScrollPane

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/38546860/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 03:30:59  来源:igfitidea点击:

JavaFX: Add children to ScrollPane

javajavafxscrollpane

提问by Alex

I have a Pane Object where users can drag and drop various ImageViews. For this, I used pane.getChildren(imageViewObject)method

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

Now, after replacing Pane with ScrollPane, it does not have this method. So I don't know how to get arrount this issue.

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

Thank you in advance

先感谢您

回答by Kachna

you can specify only one node with ScrollPane. To create a scroll view with more than one component, use layout containers or the Group class.

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

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

Example:

示例

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);
    }

}