面板是容纳结点的容器,面板主要有以下几类:
1. StackPane前面已经用了,其作用就是将节点置于中央。
2.FlowPane的应用
例子:
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.PasswordField;
import javafx.scene.control.TextField;
import javafx.scene.layout.FlowPane;
public class FlowPaneDemo extends Application {
public static void main(String[] args) {
// TODO Auto-generated method stub
launch(args);
}
public void start(Stage primaryStage) throws Exception{
FlowPane flowPane=new FlowPane();
Label l1=new Label("用户");
TextField text=new TextField("请输入你的名字");
Label l2=new Label("用户密码");
PasswordField ped=new PasswordField();
Button login=new Button("登录");
flowPane.getChildren().addAll(l1,text,l2,ped,login);
Scene scene=new Scene(flowPane,280,200);
primaryStage.setTitle("登录");
primaryStage.setScene(scene);
primaryStage.show();
}
}
3.GridPane的应用
例子:
import javafx.application.Application;
import javafx.geometry.HPos;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.layout.GridPane;
import javafx.stage.Stage;
public class GridPaneDemo extends Application {
public static void main(String[] args) {
// TODO Auto-generated method stub
launch(args);
}
@Override
public void start(Stage stage) throws Exception {
GridPane pane=new GridPane();//网格矩阵结构
pane.setAlignment(Pos.CENTER);
pane.setPadding(new Insets(11.5,12.5,13.5,14.5));
pane.setHgap(5.5);//设置两个结点之间的距离
pane.setVgap(5.5);//设置两行之间的行高
pane.add(new Label("First Name"), 0, 0);//表示第一列,第一行;
pane.add(new TextField(), 1, 0);
pane.add(new Label("MI"), 0, 1);
pane.add(new TextField(), 1, 1);
pane.add(new Label("Last Name"), 0, 2);
pane.add(new TextField(), 1, 2);
Button bt=new Button("Add Name");
pane.add(bt, 1, 3);
GridPane.setHalignment(bt, HPos.RIGHT);//水平向右对齐
Scene scene=new Scene(pane,400,400);
stage.setTitle("show a circle");
stage.setScene(scene);
stage.show();
}
}
5.BorderPane的应用
6.HBox与VBox
例子:
import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.scene.paint.Color;
import javafx.scene.text.Text;
import javafx.stage.Stage;
public class MulipleStage extends Application {
public static void main(String[] args) {
// TODO Auto-generated method stub
launch(args);
}
@Override
public void start(Stage stage) throws Exception {
// TODO Auto-generated method stub
BorderPane pane=new BorderPane();
pane.setTop(getHBox());
pane.setLeft(getVBox());
Scene scene=new Scene(pane,500,300);
stage.setTitle("JavaFx");
stage.setScene(scene);
stage.show();
}
public static HBox getHBox() {
HBox hBox=new HBox(15);
hBox.setPadding(new Insets(15,15,15,15));
hBox.setStyle("-fx-background-color:gray");
hBox.getChildren().add(new Button("Computer Science"));
hBox.getChildren().add(new Button("Chemitry"));
hBox.getChildren().add(new Button("Math"));
return hBox;
}
public static VBox getVBox() {
VBox vBox=new VBox(15);
vBox.setPadding(new Insets(15,5,5,5));
vBox.getChildren().add(new Label("Courses"));
Label[]courses= {new Label("CSCI 1301"),new Label("CSCI 2410"),new Label("CSCI 3720")};
for(Label course:courses) {
VBox.setMargin(course, new Insets(0,0,0,15));
vBox.getChildren().add(course);
}
return vBox;
}
}