javafx慢慢学习吧建议使用idea敲代码,可以选择颜色方便点
下面代码注释时候看看改变来学习
package sample;
import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
public class Main extends Application {
public static void main(String[] args) {
launch(args);
}
@Override
public void start(Stage primaryStage) throws Exception {
Button b1 = new Button("button1");
Button b2 = new Button("button2");
Button b3 = new Button("button3");
Button b4 = new Button("button4");
AnchorPane anchorPane = new AnchorPane();
anchorPane.setStyle("-fx-background-color: #783e78");
HBox box = new HBox();水平排列,子组件多了的话会都显示,但是缩小了。字都会看不见。
// VBox box = new VBox();垂直排列,子组件多了的话会都显示,并且显示正常,因为会把anp撑大!使用方法与Hbox一致。
box.setStyle("-fx-background-color: #03e8a0");
box.getChildren().addAll(b1,b2,b3,b4);
box.setPrefHeight(500);
box.setPrefWidth(300);
box.setPadding(new Insets(20));//会使得按钮距离父控件的边缘为20,上下左右都会,再小的父控件都会撑大
box.setSpacing(10);///设置子组件的相互之间距离
box.setMargin(b1,new Insets(10));//b1外边距加10
box.setAlignment(Pos.BOTTOM_CENTER);按钮下居中,之前设置的B1外边距还保留
anchorPane.getChildren().add(box);
Scene scene = new Scene(anchorPane);
primaryStage.setScene(scene);
primaryStage.setTitle("Java FX Lesson.HBOX.VBOX");
primaryStage.setWidth(800);
primaryStage.setHeight(800);
primaryStage.show();
}
}