2015-04-02 86 views
0

的孩子,我創建了這個簡單的例子來告訴你,我有問題:添加保證金的ScrollPane

import javafx.application.Application; 
import static javafx.application.Application.launch; 
import javafx.collections.FXCollections; 
import javafx.collections.ObservableList; 
import javafx.geometry.Insets; 
import javafx.geometry.Orientation; 
import javafx.geometry.Pos; 
import javafx.scene.Scene; 
import javafx.scene.control.ComboBox; 
import javafx.scene.control.ScrollPane; 
import javafx.scene.layout.FlowPane; 
import javafx.scene.layout.StackPane; 
import javafx.scene.paint.Color; 
import javafx.scene.shape.Rectangle; 
import javafx.stage.Stage; 

public class MainApp extends Application 
{ 

    private final StackPane stackPane = new StackPane(); 

    @Override 
    public void start(Stage stage) throws Exception 
    { 

     ScrollPane scroll = new ScrollPane(); 

     stackPane.getChildren().addAll(scroll, comboPane()); 
     stackPane.setStyle("-fx-background-color: white;"); 

     stackPane.setAlignment(Pos.TOP_RIGHT); 
     stackPane.setPrefSize(900, 900); 

     FlowPane flowPane; 

     flowPane = new FlowPane(Orientation.HORIZONTAL); 
     flowPane.setVgap(10); 
     flowPane.setHgap(10); 

     flowPane.setPadding(new Insets(15, 15, 15, 15)); 
     flowPane.setStyle("-fx-background-color: white;"); 
     flowPane.setAlignment(Pos.TOP_LEFT); 

     scroll.setStyle("-fx-background-color: white;"); 
     scroll.setHbarPolicy(ScrollPane.ScrollBarPolicy.AS_NEEDED); // Horizontal scroll bar 
     scroll.setVbarPolicy(ScrollPane.ScrollBarPolicy.AS_NEEDED); // Vertical scroll bar 
     scroll.setFitToHeight(true); 
     scroll.setFitToWidth(true); 
     scroll.setContent(flowPane); 

     for (int i = 0; i < 20; i++) 
     { 

      Rectangle rect = new Rectangle(20, 20, 200, 200); 

      rect.setArcHeight(15); 
      rect.setArcWidth(15); 

      rect.setStroke(Color.GREEN); 

      flowPane.getChildren().add(rect); 

     } 

     Scene scene = new Scene(stackPane); 

     stage.setTitle("JavaFX and Maven"); 
     stage.setScene(scene); 
     stage.show(); 
    } 

    private ComboBox<String> comboPane() 
    { 
     ObservableList<String> options = FXCollections.observableArrayList(
      "One", "Two", "Three", "Four"); 
     ComboBox<String> combo = new ComboBox<>(options); 

     return combo; 
    } 

    public static void main(String[] args) 
    { 
     launch(args); 
    } 
} 

enter image description here

正如你可以看到組合框放置在StackPane的角落。我試圖添加此代碼combo.setPadding(new Insets(20,20,20,20));

什麼是在StackPane的Corned和Combobox之間添加一些空間的正確方法?

回答

4

使用StackPane

ComboBox<String> cb = comboPane(); 
stackPane.getChildren().addAll(scroll, cb); 
StackPane.setMargin(cb, new Insets(30)); 
靜態方法