JavaFX的布局方式:
HBox水平布局 VBox竖直布局 BorderPane边框布局 FlowPane流式布局 GridPane网格布局
本文主要介绍HBox VBox两种布局方式:
HBox
水平布局
代码:
package sample; import javafx.application.Application; import javafx.fxml.FXMLLoader; import javafx.geometry.Insets; import javafx.geometry.Pos; import javafx.scene.Parent; 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.HBox; import javafx.scene.layout.VBox; import javafx.stage.Stage; import javax.swing.*; public class Main extends Application { @Override public void start(Stage primaryStage) throws Exception{ Button butOk = new Button("确定"); //设置按钮大小 butOk.setPrefSize(80,25); Button butCancel = new Button("取消"); butCancel.setPrefSize(80,25); HBox box = new HBox(butOk,butCancel); box.setSpacing(20); //box中间的控件之间的距离 box.setPadding(new Insets(10,20,30,40)); //box中间的控件与边框之间的距离 上 右 下 左 primaryStage.setTitle("Hello World"); primaryStage.setScene(new Scene(box, 300, 275)); primaryStage.show(); } public static void main(String[] args) { launch(args); } }
VBox
竖直布局
代码
package sample; import javafx.application.Application; import javafx.fxml.FXMLLoader; import javafx.geometry.Insets; import javafx.geometry.Pos; import javafx.scene.Parent; 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.HBox; import javafx.scene.layout.VBox; import javafx.stage.Stage; import javax.swing.*; public class Main extends Application { @Override public void start(Stage primaryStage) throws Exception{ Button butOk = new Button("确定"); //设置按钮大小 butOk.setPrefSize(80,25); Button butCancel = new Button("取消"); butCancel.setPrefSize(80,25); VBox box = new VBox(butOk,butCancel); box.setAlignment(Pos.CENTER); //居中 box.setSpacing(20); //box上控件之间的距离 primaryStage.setTitle("Hello World"); primaryStage.setScene(new Scene(box, 300, 275)); primaryStage.show(); } public static void main(String[] args) { launch(args); } }
综合示例
利用HBox和VBox设计登录窗口
代码
package sample; import javafx.application.Application; import javafx.fxml.FXMLLoader; import javafx.geometry.Insets; import javafx.geometry.Pos; import javafx.scene.Parent; 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.HBox; import javafx.scene.layout.VBox; import javafx.stage.Stage; import javax.swing.*; public class Main extends Application { @Override public void start(Stage primaryStage) throws Exception{ Label labUsername = new Label("用户名:"); labUsername.setPrefSize(100,20); TextField textUsername = new TextField(); textUsername.setPrefSize(150,20); HBox one = new HBox(labUsername,textUsername); Label labPassword = new Label("密码:"); labPassword.setPrefSize(100,20); PasswordField textPassword = new PasswordField(); textPassword.setPrefSize(150,20); HBox two = new HBox(labPassword,textPassword); Button butOk = new Button("确定"); butOk.setPrefSize(100,20); Button butCancel = new Button("取消:"); butCancel.setPrefSize(100,20); HBox three = new HBox(butOk,butCancel); three.setPadding(new Insets(10,10,10,40)); VBox box = new VBox(one,two,three); box.setAlignment(Pos.CENTER); box.setSpacing(50); primaryStage.setTitle("Hello World"); primaryStage.setScene(new Scene(box, 300, 275)); primaryStage.show(); } public static void main(String[] args) { launch(args); } }
0条评论
点击登录参与评论