JAvaFX元素调整大小以适应窗口

JAvaFX元素调整大小以适应窗口,javafx,resize,Javafx,Resize,我只是在尝试JavaFX,我正在努力尝试它,因为它被认为是未来。我的问题是,组件不能随屏幕调整大小。我试图改变HBoxz HPane的限制条件,但他们似乎把事情搞砸了 这是我的FXML文件 <?xml version="1.0" encoding="UTF-8"?> <?import javafx.scene.control.Button?> <?import javafx.scene.control.TableColumn?> <?import ja

我只是在尝试JavaFX,我正在努力尝试它,因为它被认为是未来。我的问题是,组件不能随屏幕调整大小。我试图改变HBoxz HPane的限制条件,但他们似乎把事情搞砸了

这是我的FXML文件

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

<?import javafx.scene.control.Button?>
<?import javafx.scene.control.TableColumn?>
<?import javafx.scene.control.TableView?>
<?import javafx.scene.control.TextField?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.layout.HBox?>
<?import javafx.scene.layout.StackPane?>
<?import javafx.scene.text.Font?>
<?import javafx.scene.text.Text?>
<?import org.kordamp.ikonli.javafx.FontIcon?>

<AnchorPane id="AnchorPane" prefHeight="671.0" prefWidth="1020.0" stylesheets="@../../../resources/css/MainCSS.css" xmlns="http://javafx.com/javafx/9.0.1" xmlns:fx="http://javafx.com/fxml/1" fx:controller="com.arpentechnologies.software.core.paneladmin.gestionroles.FXMLGestionRolesController">
    <children>
        <Button id="backButton" fx:id="volverPanelAdminButton" layoutX="14.0" layoutY="14.0" mnemonicParsing="false" onAction="#onClickVolverInicio" prefHeight="56.0" prefWidth="54.0" styleClass="backButton" stylesheets="@../../../resources/css/MainCSS.css" AnchorPane.leftAnchor="8.0">
            <graphic>
                <FontIcon iconLiteral="mdi-keyboard-backspace" iconSize="30" wrappingWidth="30.0" />
            </graphic>
        </Button>
        <StackPane layoutX="14.0" layoutY="83.0" prefHeight="50.0" prefWidth="253.0" AnchorPane.leftAnchor="8.0">
            <children>
                <TextField prefHeight="45.0" prefWidth="283.0" promptText="Buscar...">
                    <font>
                        <Font size="18.0" />
                    </font>
                </TextField>
            </children>
        </StackPane>
        <StackPane layoutX="267.0" layoutY="83.0" prefHeight="50.0" prefWidth="50.0">
            <children>
                <FontIcon iconLiteral="mdi-send" iconSize="36" text="" />
            </children>
        </StackPane>
        <StackPane layoutX="860.0" layoutY="77.0" prefHeight="62.0" prefWidth="200.0" AnchorPane.rightAnchor="8.0">
            <children>
                <Button fx:id="anadirRolButton" mnemonicParsing="false" onAction="#onClickAnadirRol" prefHeight="39.0" prefWidth="231.0" text=" Añadir Rol">
                    <font>
                        <Font size="18.0" />
                    </font>
                    <graphic>
                        <FontIcon iconLiteral="mdi-account-plus" iconSize="26" />
                    </graphic>
                </Button>
            </children>
        </StackPane>
        <HBox layoutX="14.0" layoutY="152.0" maxHeight="1.7976931348623157E308" maxWidth="1.7976931348623157E308" prefHeight="511.0" prefWidth="1060.0">
            <children>
                <TableView prefHeight="495.0" prefWidth="1049.0" HBox.hgrow="ALWAYS">
                    <columns>
                        <TableColumn prefWidth="75.0" text="Nombre" />
                        <TableColumn prefWidth="75.0" text="Rol Padre" />
                        <TableColumn prefWidth="75.0" text="Módulos" />
                        <TableColumn prefWidth="75.0" text="Permisos" />
                        <TableColumn prefWidth="75.0" text="Editar" />
                        <TableColumn prefWidth="75.0" text="Borrar" />
                    </columns>
                </TableView>
            </children>
        </HBox>
        <Text layoutX="425.0" layoutY="65.0" strokeType="OUTSIDE" strokeWidth="0.0" text="Gestión de Roles" wrappingWidth="224.0546875">
            <font>
                <Font size="30.0" />
            </font>
        </Text>
    </children>
</AnchorPane>

我尝试对您的fxml文件进行一些更改,以获得响应性布局以满足您的需要

可调整大小的表视图和文本从中心移动到中心 左

  • 更改包含tableView的HBox的锚定约束
  • 将角色的管理放入HBox中
  • 这是更改后的FXML文件:

    <?xml version="1.0" encoding="UTF-8"?>
    
    <?import javafx.scene.control.Button?>
    <?import javafx.scene.control.TableColumn?>
    <?import javafx.scene.control.TableView?>
    <?import javafx.scene.control.TextField?>
    <?import javafx.scene.layout.AnchorPane?>
    <?import javafx.scene.layout.HBox?>
    <?import javafx.scene.layout.StackPane?>
    <?import javafx.scene.text.Font?>
    <?import javafx.scene.text.Text?>
    
    <AnchorPane id="AnchorPane" prefHeight="671.0" prefWidth="1020.0" stylesheets="@../../../resources/css/MainCSS.css" xmlns="http://javafx.com/javafx/8.0.141" xmlns:fx="http://javafx.com/fxml/1">
        <children>
            <Button id="backButton" fx:id="volverPanelAdminButton" layoutX="14.0" layoutY="14.0" mnemonicParsing="false" prefHeight="56.0" prefWidth="54.0" styleClass="backButton" stylesheets="@../../../resources/css/MainCSS.css" AnchorPane.leftAnchor="8.0">
                <graphic>
                </graphic>
            </Button>
            <StackPane layoutX="14.0" layoutY="83.0" prefHeight="50.0" prefWidth="253.0" AnchorPane.leftAnchor="8.0">
                <children>
                    <TextField prefHeight="45.0" prefWidth="283.0" promptText="Buscar...">
                        <font>
                            <Font size="18.0" />
                        </font>
                    </TextField>
                </children>
            </StackPane>
            <StackPane layoutX="267.0" layoutY="83.0" prefHeight="50.0" prefWidth="50.0">
                <children>
                </children>
            </StackPane>
            <StackPane layoutX="860.0" layoutY="77.0" prefHeight="62.0" prefWidth="200.0" AnchorPane.rightAnchor="8.0">
                <children>
                    <Button fx:id="anadirRolButton" mnemonicParsing="false" prefHeight="39.0" prefWidth="231.0" text=" Añadir Rol">
                        <font>
                            <Font size="18.0" />
                        </font>
                        <graphic>
                        </graphic>
                    </Button>
                </children>
            </StackPane>
            <HBox layoutX="14.0" layoutY="152.0" maxHeight="1.7976931348623157E308" maxWidth="1.7976931348623157E308" prefHeight="511.0" prefWidth="1060.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0">
                <children>
                    <TableView prefHeight="495.0" prefWidth="1049.0" HBox.hgrow="ALWAYS">
                        <columns>
                            <TableColumn prefWidth="75.0" text="Nombre" />
                            <TableColumn prefWidth="75.0" text="Rol Padre" />
                            <TableColumn prefWidth="75.0" text="Módulos" />
                            <TableColumn prefWidth="75.0" text="Permisos" />
                            <TableColumn prefWidth="75.0" text="Editar" />
                            <TableColumn prefWidth="75.0" text="Borrar" />
                        </columns>
                    </TableView>
                </children>
            </HBox>
          <HBox alignment="CENTER" layoutX="174.0" layoutY="14.0" prefHeight="100.0" prefWidth="200.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0">
             <children>
                  <Text strokeType="OUTSIDE" strokeWidth="0.0" text="Gestión de Roles" wrappingWidth="224.0546875" HBox.hgrow="ALWAYS">
                      <font>
                          <Font size="30.0" />
                      </font>
                  </Text>
             </children>
          </HBox>
        </children>
    </AnchorPane>
    

    我已经改变了安可凡的HBox限制,使其保持不变,从而确保了响应能力

    <?xml version="1.0" encoding="UTF-8"?>
    
    <?import javafx.scene.control.Button?>
    <?import javafx.scene.control.TableColumn?>
    <?import javafx.scene.control.TableView?>
    <?import javafx.scene.control.TextField?>
    <?import javafx.scene.layout.AnchorPane?>
    <?import javafx.scene.layout.HBox?>
    <?import javafx.scene.layout.StackPane?>
    <?import javafx.scene.text.Font?>
    <?import javafx.scene.text.Text?>
    
    <AnchorPane id="AnchorPane" prefHeight="671.0" prefWidth="1020.0" stylesheets="@../../../resources/css/MainCSS.css" xmlns="http://javafx.com/javafx/8.0.102" xmlns:fx="http://javafx.com/fxml/1" fx:controller="com.arpentechnologies.software.core.paneladmin.gestionroles.FXMLGestionRolesController">
        <children>
            <Button id="backButton" fx:id="volverPanelAdminButton" layoutX="14.0" layoutY="14.0" mnemonicParsing="false" onAction="#onClickVolverInicio" prefHeight="56.0" prefWidth="54.0" styleClass="backButton" stylesheets="@../../../resources/css/MainCSS.css" AnchorPane.leftAnchor="8.0">
                <graphic>
                    <FontIcon iconLiteral="mdi-keyboard-backspace" iconSize="30" wrappingWidth="30.0" />
                </graphic>
            </Button>
            <StackPane layoutX="14.0" layoutY="83.0" prefHeight="50.0" prefWidth="253.0" AnchorPane.leftAnchor="8.0">
                <children>
                    <TextField prefHeight="45.0" prefWidth="283.0" promptText="Buscar...">
                        <font>
                            <Font size="18.0" />
                        </font>
                    </TextField>
                </children>
            </StackPane>
            <StackPane layoutX="267.0" layoutY="83.0" prefHeight="50.0" prefWidth="50.0">
                <children>
                    <FontIcon iconLiteral="mdi-send" iconSize="36" text="?" />
                </children>
            </StackPane>
            <StackPane layoutX="860.0" layoutY="77.0" prefHeight="62.0" prefWidth="200.0" AnchorPane.rightAnchor="8.0">
                <children>
                    <Button fx:id="anadirRolButton" mnemonicParsing="false" onAction="#onClickAnadirRol" prefHeight="39.0" prefWidth="231.0" text=" A�adir Rol">
                        <font>
                            <Font size="18.0" />
                        </font>
                        <graphic>
                            <FontIcon iconLiteral="mdi-account-plus" iconSize="26" />
                        </graphic>
                    </Button>
                </children>
            </StackPane>
            <HBox layoutX="14.0" layoutY="152.0" maxHeight="1.7976931348623157E308" maxWidth="1.7976931348623157E308" prefHeight="511.0" prefWidth="1060.0" AnchorPane.bottomAnchor="8.0" AnchorPane.leftAnchor="14.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="152.0">
                <children>
                    <TableView prefHeight="495.0" prefWidth="1049.0" HBox.hgrow="ALWAYS">
                        <columns>
                            <TableColumn prefWidth="75.0" text="Nombre" />
                            <TableColumn prefWidth="75.0" text="Rol Padre" />
                            <TableColumn prefWidth="75.0" text="M�dulos" />
                            <TableColumn prefWidth="75.0" text="Permisos" />
                            <TableColumn prefWidth="75.0" text="Editar" />
                            <TableColumn prefWidth="75.0" text="Borrar" />
                        </columns>
                    </TableView>
                </children>
            </HBox>
            <Text layoutX="425.0" layoutY="65.0" strokeType="OUTSIDE" strokeWidth="0.0" text="Gesti�n de Roles" wrappingWidth="224.0546875">
                <font>
                    <Font size="30.0" />
                </font>
            </Text>
        </children>
    </AnchorPane>
    

    您还可以添加滚动窗格

    <?xml version="1.0" encoding="UTF-8"?>
    
    <?import javafx.scene.control.Button?>
    <?import javafx.scene.control.ScrollPane?>
    <?import javafx.scene.control.TableColumn?>
    <?import javafx.scene.control.TableView?>
    <?import javafx.scene.control.TextField?>
    <?import javafx.scene.layout.AnchorPane?>
    <?import javafx.scene.layout.HBox?>
    <?import javafx.scene.layout.StackPane?>
    <?import javafx.scene.text.Font?>
    <?import javafx.scene.text.Text?>
    
    <AnchorPane id="AnchorPane" prefHeight="671.0" prefWidth="1020.0" stylesheets="@../../../resources/css/MainCSS.css" xmlns="http://javafx.com/javafx/8.0.102" xmlns:fx="http://javafx.com/fxml/1" fx:controller="com.arpentechnologies.software.core.paneladmin.gestionroles.FXMLGestionRolesController">
        <children>
          <ScrollPane prefHeight="200.0" prefWidth="200.0" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0">
            <content>
              <AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="670.0" prefWidth="1005.0">
                   <children>
                      <HBox layoutX="389.0" layoutY="30.0">
                         <children>
                              <Text strokeType="OUTSIDE" strokeWidth="0.0" text="Gesti�n de Roles" wrappingWidth="241.0546875" HBox.hgrow="NEVER">
                                  <font>
                                      <Font size="30.0" />
                                  </font>
                              </Text>
                         </children>
                      </HBox>
                        <Button id="backButton" fx:id="volverPanelAdminButton" layoutX="14.0" layoutY="25.0" mnemonicParsing="false" onAction="#onClickVolverInicio" prefHeight="50.0" prefWidth="115.0" styleClass="backButton" stylesheets="@../../../resources/css/MainCSS.css" AnchorPane.leftAnchor="14.0">
                            <graphic>
                                <FontIcon iconLiteral="mdi-keyboard-backspace" iconSize="30" wrappingWidth="30.0" />
                            </graphic>
                        </Button>
                        <StackPane layoutX="14.0" layoutY="87.0" prefHeight="50.0" prefWidth="253.0" AnchorPane.leftAnchor="14.0">
                            <children>
                                <TextField promptText="Buscar...">
                                    <font>
                                        <Font size="18.0" />
                                    </font>
                                </TextField>
                            </children>
                        </StackPane>
                        <StackPane layoutX="280.0" layoutY="87.0" prefHeight="50.0" prefWidth="50.0">
                            <children>
                                <FontIcon iconLiteral="mdi-send" iconSize="36" text="?" />
                            </children>
                        </StackPane>
                        <StackPane layoutX="784.0" layoutY="81.0" prefHeight="62.0" prefWidth="200.0" AnchorPane.rightAnchor="21.0">
                            <children>
                                <Button fx:id="anadirRolButton" mnemonicParsing="false" onAction="#onClickAnadirRol" text=" A�adir Rol">
                                    <font>
                                        <Font size="18.0" />
                                    </font>
                                    <graphic>
                                        <FontIcon iconLiteral="mdi-account-plus" iconSize="26" />
                                    </graphic>
                                </Button>
                            </children>
                        </StackPane>
                        <HBox maxHeight="1.7976931348623157E308" maxWidth="1.7976931348623157E308" AnchorPane.bottomAnchor="8.0" AnchorPane.leftAnchor="14.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="152.0">
                            <children>
                                <TableView HBox.hgrow="ALWAYS">
                                    <columns>
                                        <TableColumn prefWidth="75.0" text="Nombre" />
                                        <TableColumn prefWidth="75.0" text="Rol Padre" />
                                        <TableColumn prefWidth="75.0" text="M�dulos" />
                                        <TableColumn prefWidth="75.0" text="Permisos" />
                                        <TableColumn prefWidth="75.0" text="Editar" />
                                        <TableColumn prefWidth="75.0" text="Borrar" />
                                    </columns>
                                </TableView>
                            </children>
                        </HBox>
                   </children>
                </AnchorPane>
            </content>
          </ScrollPane>
        </children>
    </AnchorPane>
    
    
    
    哪个组件不调整大小?是否只是
    表格视图
    ?表格视图不调整大小,文本从中心向左移动。