JavaFX+SceneBuilder窗口可变时的填充(拉伸)

小提示:SceneBuilder有Gluon版和Oracle版,这里使用的是Gluon版

  1. 效果图
    为什么蓝框随窗口拉伸而拉伸,因为窗口拉伸时只有BorderPane的center部分会宽度和高度都进行改变。
    在这里插入图片描述先看看效果图,确定是不是自己想找的文章,嘻嘻!

  2. 首先使用SceneBuilder布局,这里用到了多种布局
    在这里插入图片描述
    拖拽完控件后的样子
    在这里插入图片描述
    参考文档:JavaFX布局,很赞!
    https://blog.csdn.net/theonegis/article/details/50184811

  3. 使用SceneBuilder对控件进行属性设置

    3.1 Label 的四个设置
    在这里插入图片描述
    在这里插入图片描述
    在这里插入图片描述
    BorderPane左侧VBox的两个按钮设置略
    参考文档:HBox VBox 布局利用Priority实现布局自适应
    https://blog.csdn.net/cdc_csdn/article/details/80710001

    3.2 GridPane的设置
    在这里插入图片描述在这里插入图片描述
    3.3 设置窗口拉伸的最小值
    在这里插入图片描述

  4. fxml的完整代码

<?xml version="1.0" encoding="UTF-8"?>

<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.layout.BorderPane?>
<?import javafx.scene.layout.ColumnConstraints?>
<?import javafx.scene.layout.GridPane?>
<?import javafx.scene.layout.HBox?>
<?import javafx.scene.layout.RowConstraints?>
<?import javafx.scene.layout.VBox?>

<BorderPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/8.0.271" xmlns:fx="http://javafx.com/fxml/1">
   <top>
      <HBox prefHeight="100.0" prefWidth="200.0" BorderPane.alignment="CENTER">
         <children>
            <Label alignment="CENTER" maxHeight="Infinity" maxWidth="Infinity" style="-fx-background-color: red;" text="Label" HBox.hgrow="ALWAYS" />
         </children>
      </HBox>
   </top>
   <left>
      <VBox prefHeight="200.0" prefWidth="100.0" BorderPane.alignment="CENTER">
         <children>
            <Button maxHeight="Infinity" maxWidth="Infinity" mnemonicParsing="false" text="Button" VBox.vgrow="ALWAYS" />
            <Button maxHeight="Infinity" maxWidth="Infinity" mnemonicParsing="false" text="Button" VBox.vgrow="ALWAYS" />
         </children>
      </VBox>
   </left>
   <center>
      <GridPane style="-fx-border-color: blue; -fx-border-width: 5;" BorderPane.alignment="CENTER">
        <columnConstraints>
          <ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
          <ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
        </columnConstraints>
        <rowConstraints>
          <RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
          <RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
        </rowConstraints>
         <children>
            <Button mnemonicParsing="false" text="Button" GridPane.halignment="CENTER" />
            <Button mnemonicParsing="false" text="Button" GridPane.halignment="CENTER" GridPane.rowIndex="1" />
            <Button mnemonicParsing="false" text="Button" GridPane.columnIndex="1" GridPane.halignment="CENTER" />
            <Button mnemonicParsing="false" text="Button" GridPane.columnIndex="1" GridPane.halignment="CENTER" GridPane.rowIndex="1" />
         </children>
      </GridPane>
   </center>
</BorderPane>

小结:用最简洁的代码实现功能,是我这个小白向来奉行的原则~~~

我本来可以很开心,都是没钱害了我,暴风哭泣!

  • 2
    点赞
  • 18
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
要实现JavaFX窗口自适应,需要在布局中使用相对大小而不是固定大小,可以使用JavaFX的AnchorPane布局或GridPane布局来实现。 在AnchorPane布局中,可以使用AnchorPane.setTopAnchor、AnchorPane.setBottomAnchor、AnchorPane.setLeftAnchor和AnchorPane.setRightAnchor方法来设置组件的位置和大小。 在GridPane布局中,可以使用ColumnConstraints和RowConstraints来设置列和行的大小,然后使用GridPane.setHgrow和GridPane.setVgrow方法来设置组件的大小和位置。 另外,可以使用Scene.widthProperty和Scene.heightProperty来监听窗口的大小变化,并在变化更新布局。 下面是一个简单的例子,展示如何使用AnchorPane布局实现JavaFX窗口自适应: ```java public class Main extends Application { @Override public void start(Stage primaryStage) throws Exception { AnchorPane root = new AnchorPane(); Button button = new Button("Hello World"); root.getChildren().add(button); AnchorPane.setTopAnchor(button, 50.0); AnchorPane.setLeftAnchor(button, 50.0); AnchorPane.setRightAnchor(button, 50.0); AnchorPane.setBottomAnchor(button, 50.0); Scene scene = new Scene(root, 400, 300); primaryStage.setScene(scene); primaryStage.show(); scene.widthProperty().addListener((obs, oldVal, newVal) -> { AnchorPane.setLeftAnchor(button, newVal.doubleValue() / 4); AnchorPane.setRightAnchor(button, newVal.doubleValue() / 4); }); scene.heightProperty().addListener((obs, oldVal, newVal) -> { AnchorPane.setTopAnchor(button, newVal.doubleValue() / 4); AnchorPane.setBottomAnchor(button, newVal.doubleValue() / 4); }); } } ``` 在这个例子中,按钮的位置和大小使用AnchorPane.setTopAnchor、AnchorPane.setBottomAnchor、AnchorPane.setLeftAnchor和AnchorPane.setRightAnchor方法来设置。在窗口大小变化,使用Scene.widthProperty和Scene.heightProperty监听窗口的大小变化,并更新按钮的位置和大小,使其保持在窗口中间。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值