2016-07-23 121 views
1

我有一个窗格对象,用户可以在其中拖放各种ImageView。为此,我使用了 pane.getChildren(imageViewObject) 方法JavaFX:将孩子添加到ScrollPane中

现在,在用ScrollPane替换Pane后,它没有此方法。所以我不知道如何得到这个问题。

预先感谢您

回答

6

您可以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); 
    } 

} 
+0

这是不太我一直在寻找的解决方案。现在的问题是,ScrollPane只能从侧栏滚动。 – Alex

+0

call'scrollPane.setPannable(true);' – Kachna