Java/javaFX & Scene Builder

5주차 - javaFX VBox

우젼 2022. 9. 5. 20:17

Vbox는 내부의 요소들을 세로로 정렬한다.

public class VboxEx01 extends Application {
	public static void main(String[] args) {
		launch(args);
	}

	@Override
	public void start(Stage primaryStage) throws Exception {
		Pane p1 = new Pane();
		Pane p2 = new Pane();
		Pane p3 = new Pane();
		
		// VBox는 HBox와 반대로 길이값은 채워주지 않아도 최대값만 지정해주면 자동으로 채워짐. 
        // 단, 높이는 채워줘야함
		p1.setPrefHeight(100);
		p2.setPrefHeight(100);
		p3.setPrefHeight(100);
		
		p1.setMaxWidth(100);
		p2.setMaxWidth(100);
		p3.setMaxWidth(100);
		
		
		p1.setStyle("-fx-background-color:red");
		p2.setStyle("-fx-background-color:blue");
		p3.setStyle("-fx-background-color:green");
		
		VBox box = new VBox();
		box.setAlignment(Pos.CENTER);
		box.setPadding(new Insets(50));
		box.setSpacing(10); // 내부 요소 사이에 간격 부여
		box.getChildren().addAll(p1,p2,p3);
		
		primaryStage.setScene(new Scene(box, 400, 300));
		primaryStage.setTitle("VBoxEx");
		primaryStage.show();
	}
}

위 코드의 실행화면