JavaFx HBox VBox 布局利用Priority实现布局自适应

一:相关类和方法

1:   javafx.scene.layout.Priority,一个枚举类,用于确定给定节点的增长(或缩小)优先级。比如:一个HBox布局,里面有三个控件,当屏幕宽度是800时,刚好把屏幕占满,但是当屏幕扩大到1200时,这个Priority规定了这三个控件如何处理增加的400宽度。共有三个取值:

ALWAYS:布局区域将始终尝试增长(或缩小),共享那些空间;

SOMETIMES:如果没有控件设置为ALWAYS,或者其它控件没有处理完变化的控件,设置为SOMETIMES的控件将和其它控件分享这些区域。

NEVER:控件不会参与处理变化的空间。

2.  HBox.setHgrow(Node child, Priority value),HBox.getHgrow(Node child);

    VBox.setVgrow(Node child, Priority value),VBox.getVgrow(Node child);

3.  注意事项

     如果HBox里面所有的控件都设置成ALWAYS,那么这些控件需要设置maxWidth="Infinity",否则会不起作用。


二:实例

1. main.xml

<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.text.*?>
<?import javafx.geometry.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>

<VBox maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="800.0" prefWidth="1200.0" spacing="10.0" stylesheets="@main.css" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1">
   <children>
      <HBox maxHeight="60.0" prefHeight="60.0" prefWidth="1200.0" spacing="10.0" styleClass="bg" VBox.vgrow="NEVER">
         <children>
            <Button maxWidth="Infinity" mnemonicParsing="false" prefHeight="40.0" prefWidth="160.0" text="Button" HBox.hgrow="ALWAYS">

            </Button>
             <Button maxWidth="Infinity" mnemonicParsing="false" prefHeight="40.0" prefWidth="160.0" text="Button" HBox.hgrow="ALWAYS">

             </Button>
             <Button maxWidth="Infinity" mnemonicParsing="false" prefHeight="40.0" prefWidth="160.0" text="Button" HBox.hgrow="ALWAYS">

             </Button>
             <Button maxWidth="Infinity" mnemonicParsing="false" prefHeight="40.0" prefWidth="160.0" text="Button" HBox.hgrow="ALWAYS">

             </Button>
             <Button maxWidth="Infinity" mnemonicParsing="false" prefHeight="40.0" prefWidth="160.0" text="Button" HBox.hgrow="ALWAYS">

             </Button>
             <Button maxWidth="Infinity" mnemonicParsing="false" prefHeight="40.0" prefWidth="160.0" text="Button" HBox.hgrow="ALWAYS">

             </Button>
             <Button maxWidth="Infinity" mnemonicParsing="false" prefHeight="40.0" prefWidth="160.0" text="Button" HBox.hgrow="ALWAYS">
             </Button>
         </children>
         <VBox.margin>
            <Insets />
         </VBox.margin>
         <padding>
            <Insets bottom="10.0" left="10.0" right="10.0" top="10.0" />
         </padding></HBox>
       <HBox maxHeight="40.0" prefHeight="60.0" prefWidth="1200.0" spacing="10.0" styleClass="bg" VBox.vgrow="NEVER">
         <children>
            <Label alignment="CENTER" graphicTextGap="8.0" prefHeight="40.0" prefWidth="160.0" text="Label" textAlignment="JUSTIFY">
               <font>
                  <Font size="16.0" />
               </font></Label>
            <TextField prefHeight="40.0" prefWidth="1010.0" HBox.hgrow="ALWAYS" />
         </children>
         <padding>
            <Insets bottom="10.0" left="10.0" right="10.0" top="10.0" />
         </padding>
      </HBox>
   </children>
   <padding>
      <Insets top="10.0" />
   </padding>
</VBox>

2.  Main.java

import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;

public class Main extends Application {

    @Override
    public void start(Stage primaryStage) throws Exception{
        Parent root = FXMLLoader.load(getClass().getResource("main.fxml"));
        primaryStage.setTitle("HBox Button 自动增长");
        primaryStage.setScene(new Scene(root));
        primaryStage.show();
    }


    public static void main(String[] args) {
        launch(args);
    }
}

3. main.css

/**设置背景颜色**/
.root{
-fx-background-color:#ffffff;
}
/**设置Label样式**/
.label {
    -fx-font-size: 16px;
    -fx-font-weight: bold;
    -fx-text-fill: #333333;
    -fx-effect: dropshadow( gaussian , rgba(255,255,255,0.5) , 0,0,0,1 );
}
/**设置Button样式**/
.button{
    /*设置背景颜色渐变*/
    -fx-background-color: linear-gradient(to right,#ABB2B9,#ABB2B9);
    -fx-text-fill:#ffffff;
    -fx-font-size: 16px;
    /*设置圆角*/
    -fx-background-radius: 10;
    -fx-border-radius: 10;
 }
/**设置Button  鼠标悬停样式**/
 .button:hover{
     -fx-border-color: blue;
 }
/**设置Button 点击鼠标样式**/
 .button:pressed{
 /**设置边框背景颜色**/
     -fx-border-color: red;
 }
/**设置HBox背景样式 bg是自定义的class**/
 .bg{
    /*设置圆角*/
        -fx-background-radius: 10;
        -fx-border-radius: 10;
        -fx-background-color:#EAECEE;

 }

4. 效果图





  • 3
    点赞
  • 22
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
JavaFX中,Pane是一个非常通用的容器,可以用来放置各种不同类型的节点。而HBoxVBox是专门用于水平和垂直布局的容器,可以方便地将子节点按照指定的方向排列。 你可以将HBoxVBox放置在Pane中,然后将需要布局的节点添加到HBoxVBox中。这样,你就可以使用HBoxVBox的布局功能来控制子节点的位置和大小,同时可以利用Pane的通用性来添加其他类型的节点,如图像、文本等等。 下面是一个简单的示例代码,展示如何在Pane中使用HBoxVBox来布局节点: ```java import javafx.application.Application; import javafx.scene.Scene; import javafx.scene.control.Button; import javafx.scene.layout.HBox; import javafx.scene.layout.Pane; import javafx.scene.layout.VBox; import javafx.stage.Stage; public class Main extends Application { @Override public void start(Stage primaryStage) { // 创建一个 Pane Pane pane = new Pane(); // 创建一个 HBox,并添加一些按钮 HBox hbox = new HBox(); hbox.getChildren().addAll(new Button("Button 1"), new Button("Button 2"), new Button("Button 3")); // 创建一个 VBox,并添加一些按钮 VBox vbox = new VBox(); vbox.getChildren().addAll(new Button("Button 4"), new Button("Button 5"), new Button("Button 6")); // 将 HBoxVBox 添加到 Pane 中 pane.getChildren().addAll(hbox, vbox); // 创建一个 Scene,并将 Pane 设置为根节点 Scene scene = new Scene(pane, 300, 200); primaryStage.setScene(scene); primaryStage.show(); } public static void main(String[] args) { launch(args); } } ``` 这段代码创建了一个包含两个按钮的HBox和三个按钮的VBox,并将它们添加到一个Pane中。你可以根据需要修改节点的大小和位置,以及HBoxVBox之间的间距和对齐方式。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值