先来两张效果图::
package sample;
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.*;
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 button = new Button("button");
AnchorPane a1 = new AnchorPane();
AnchorPane a2 = new AnchorPane();
AnchorPane a3 = new AnchorPane();
AnchorPane a4 = new AnchorPane();
AnchorPane a5 = new AnchorPane();
a1.setStyle("-fx-background-color: darkturquoise");
a1.setPrefHeight(50);
a1.setPrefWidth(50);
a2.setStyle("-fx-background-color: #a15252");
a2.setPrefHeight(100);
a2.setPrefWidth(100);
a3.setStyle("-fx-background-color: #c78441");
a3.setPrefHeight(300);
a3.setPrefWidth(300);
a4.setStyle("-fx-background-color: #5d7433");
a4.setPrefHeight(100);
a4.setPrefWidth(100);
a5.setStyle("-fx-background-color: #088de5");
a5.setPrefHeight(100);
a5.setPrefWidth(100);
BorderPane bp = new BorderPane();
bp.setStyle("-fx-background-color: #6b11ac");
/* //给各个方位安插anchor
bp.setTop(button);//在TOP放一个按钮
//获得组件,在top的button叫啥
Button bu = (Button) bp.getTop();
System.out.println(bu.getText());
*/
bp.setTop(a1);
bp.setBottom(a2);//注释后会将下面的面板去掉,从而使得左右面板直接垂直下去。
bp.setCenter(a3);当不指定宽高时,center的权限最高,会铺满整个屏幕。
bp.setLeft(a4);
bp.setRight(a5);
//三个重要方法::内边距、外边距、对齐方式
bp.setPadding(new Insets(5));//内边距
bp.setMargin(a1, new Insets(5));//设置a1的外边距
bp.setAlignment(button, Pos.BOTTOM_CENTER);
Scene scene = new Scene(bp);
primaryStage.setScene(scene);
primaryStage.setTitle("Java FX Lesson16.BorderPane ");
primaryStage.setWidth(800);
primaryStage.setHeight(800);
primaryStage.show();
}
}