我创建了这个简单的例子来向您展示我遇到的问题:
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);
}
}
正如您所见,ComboBox位于StackPane的角落。我要添加这个代码combo.setPadding(new Insets(20,20,20,20));
在StackPane和Combobox的corned之间添加一些空间的正确方法是什么?
答案 0 :(得分:6)
使用StackPane
ComboBox<String> cb = comboPane();
stackPane.getChildren().addAll( scroll, cb );
StackPane.setMargin( cb, new Insets( 30 ) );