HBox布局窗格将节点排列在一行中。它由javafx.scene.layout.HBox类表示。我们只需要实例化HBox类即可创建HBox布局。
物产
下表提供了类的属性及其设置方法。
属性 | 描述 | 设置方法 |
---|---|---|
alignment | 这表示节点的对齐方式。 | setAlignment(Double) |
fillHeight | 这是一个布尔型属性。如果将此属性设置为true, 则节点的高度将等于HBox的高度。 | setFillHeight(Double) |
spacing | 这表示HBox中节点之间的空间。它是双重类型的。 | setSpacing(Double) |
建设者
HBox类包含下面给出的两个构造函数。
- new HBox():创建间距为0的HBox布局
- 新的Hbox(双倍间距):使用间距值创建HBox布局
例
package application;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.HBox;
import javafx.stage.Stage;
public class Label_Test extends Application {
@Override
public void start(Stage primaryStage) throws Exception {
Button btn1 = new Button("Button 1");
Button btn2 = new Button("Button 2");
HBox root = new HBox();
Scene scene = new Scene(root, 200, 200);
root.getChildren().addAll(btn1, btn2);
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
示例:在节点之间设置空间。
package application;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.HBox;
import javafx.stage.Stage;
public class Label_Test extends Application {
@Override
public void start(Stage primaryStage) throws Exception {
Button btn1 = new Button("Button 1");
Button btn2 = new Button("Button 2");
HBox root = new HBox();
Scene scene = new Scene(root, 200, 200);
root.getChildren().addAll(btn1, btn2);
root.setSpacing(40);
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
评论前必须登录!
注册