JavaFX可调整大小的UI

我想在javafx中设计一个resizabel ui。我已经设计了一个具有主场景的workin UI,然后根据主场景中的按钮加载不同的场景。我想调整ui的大小,因此我在主场景中添加了一个锚定窗格并可以正常工作用于主窗格中的组件。但它不适用于我单击主场景上的按钮后加载的场景。有什么方法可以使用在主屏幕fxml中定义的定位窗格来调整其他场景fxml的大小?请帮助。

这是主要的adminpanel fxml,其中包含mani ui场景。它还包含一个“ centerPane”,在其中我根据在主UI中单击的按钮加载相应的fxml。锚窗格“ adminPanelHBox”将应用于bot工具栏和窗格。但是只有工具栏会根据窗口大小调整大小,但是加载了不同fxml的窗格根本不会调整大小。所以我也想在调整主窗口大小的同时调整那些大小。我尝试通过在单个fxmls中添加定位窗格。但没有运气。

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

    <?import javafx.scene.*?>
    <?import java.lang.*?>
    <?import javafx.geometry.*?>
    <?import javafx.scene.control.*?>
    <?import javafx.scene.image.*?>
    <?import javafx.scene.layout.*?>
    <?import javafx.geometry.Insets?>
    <?import javafx.scene.control.Button?>
    <?import javafx.scene.control.ToolBar?>
    <?import javafx.scene.control.Tooltip?>
    <?import javafx.scene.image.Image?>
    <?import javafx.scene.image.ImageView?>
    <?import javafx.scene.layout.HBox?>
    <?import javafx.scene.layout.Pane?>
    <?import javafx.scene.layout.VBox?>
    <?import javafx.scene.layout.AnchorPane?>

    <AnchorPane fx:id="adminPanelHBox" maxHeight="690.0" maxWidth="950.0" minHeight="690.0" minWidth="950.0" prefHeight="690.0" prefWidth="950.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="com.ibadmin.adminpanel.AdminPanelController">
       <children>
                    <ToolBar layoutX="14.0" orientation="VERTICAL" prefHeight="515.0" prefWidth="203.0" stylesheets="@root.css" AnchorPane.bottomAnchor="175.0" AnchorPane.leftAnchor="14.0" AnchorPane.rightAnchor="726.0" AnchorPane.topAnchor="0.0">
                        <items>
                        <ImageView fitHeight="33.0" fitWidth="200.0">
                            <image>
                                <Image url="@Logo.gif" />
                            </image>
                        </ImageView>
                            <Button id="loginButton" fx:id="loginButton" alignment="TOP_LEFT" mnemonicParsing="false" onaction="#showLoginUI" prefWidth="200.0" text="%login">
                                <graphic>
                                    <ImageView fitHeight="35.0" fitWidth="35.0" pickOnBounds="true" preserveRatio="true">
                                        <image>
                                            <Image url="@login.png" />
                                        </image>
                                    </ImageView>
                                </graphic>
                   <tooltip>
                      <Tooltip text="%menu_login_tooltip" />
                   </tooltip>
                            </Button>
                            <Button id="statsGenButton" fx:id="statsGenButton" alignment="TOP_LEFT" disable="true" mnemonicParsing="false" onaction="#showStatsGenUI" prefWidth="200.0" text="%statsGen">
                                <graphic>
                                    <ImageView fitHeight="35.0" fitWidth="35.0" pickOnBounds="true" preserveRatio="true">
                                        <image>
                                            <Image url="@statsgen.png" />
                                        </image>
                                    </ImageView>
                                </graphic>
                   <tooltip>
                      <Tooltip text="%menu_genration_stats_tooltip" />
                   </tooltip>
                            </Button>
                            <Button id="settingsButton" fx:id="settingsButton" alignment="TOP_LEFT" disable="true" layoutX="10.0" layoutY="13.0" mnemonicParsing="false" onmouseclicked="#showSettingsUI" prefWidth="200.0" text="%settings">
                                <graphic>
                                    <ImageView fitHeight="35.0" fitWidth="35.0" pickOnBounds="true" preserveRatio="true">
                                        <image>
                                            <Image url="@settings.png" />
                                        </image>
                                    </ImageView>
                                </graphic>
                   <tooltip>
                      <Tooltip text="%menu_settings_tooltip" />
                   </tooltip>
                            </Button>
                            <Button id="userButton" fx:id="userButton" alignment="TOP_LEFT" disable="true" layoutX="66.0" layoutY="13.0" mnemonicParsing="false" onaction="#showUserUI" prefWidth="200.0" text="%user">
                                <graphic>
                                    <ImageView fitHeight="35.0" fitWidth="35.0" pickOnBounds="true" preserveRatio="true">
                                        <image>
                                            <Image url="@user.png" />
                                        </image>
                                    </ImageView>
                                </graphic>
                   <tooltip>
                      <Tooltip text="%menu_user_tooltip" />
                   </tooltip>
                            </Button>
                            <Button id="userGroupButton" fx:id="userGroupButton" alignment="TOP_LEFT" disable="true" layoutX="66.0" layoutY="13.0" mnemonicParsing="false" onaction="#showUserGroupUI" prefWidth="200.0" text="%userGroup">
                                <graphic>
                                    <ImageView fitHeight="35.0" fitWidth="35.0" pickOnBounds="true" preserveRatio="true">
                                        <image>
                                            <Image url="@usergroup.png" />
                                        </image>
                                    </ImageView>
                                </graphic>
                   <tooltip>
                      <Tooltip text="%menu_user_group_tooltip" />
                   </tooltip>
                            </Button>
                            <Button id="projectsButton" fx:id="projectsButton" alignment="TOP_LEFT" disable="true" layoutX="10.0" layoutY="13.0" mnemonicParsing="false" onaction="#showProjectsUI" prefWidth="200.0" text="%projects">
                                <graphic>
                                    <ImageView fitHeight="35.0" fitWidth="35.0" pickOnBounds="true" preserveRatio="true">
                                        <image>
                                            <Image url="@Projects.png" />
                                        </image>
                                    </ImageView>
                                </graphic>
                   <tooltip>
                      <Tooltip text="%menu_project_tooltip" />
                   </tooltip>
                            </Button>
                            <Button id="statsUserButton" fx:id="connecedClientsButton" alignment="TOP_LEFT" disable="true" layoutX="178.0" layoutY="13.0" mnemonicParsing="false" onaction="#showconnectedClientsUI" prefWidth="200.0" text="%connectedClients">
                                <graphic>
                                    <ImageView fitHeight="35.0" fitWidth="35.0" pickOnBounds="true" preserveRatio="true">
                                        <image>
                                            <Image url="@statsuser.png" />
                                        </image>
                                    </ImageView>
                                </graphic>
                   <tooltip>
                      <Tooltip text="%menu_connected_clients_tooltip" />
                   </tooltip>
                            </Button>
                            <Button id="rendererButton" fx:id="rendererButton" alignment="TOP_LEFT" disable="true" layoutX="66.0" layoutY="13.0" mnemonicParsing="false" onaction="#showRenderersUI" prefWidth="200.0" text="%renderers">
                                <graphic>
                                    <ImageView fitHeight="35.0" fitWidth="35.0" pickOnBounds="true" preserveRatio="true">
                                        <image>
                                            <Image url="@renderers.png" />
                                        </image>
                                    </ImageView>
                                </graphic>
                   <tooltip>
                      <Tooltip text="%menu_renderer_tooltip" />
                   </tooltip>
                            </Button>
                            <Button id="repositoryButton" fx:id="repositoryButton" alignment="TOP_LEFT" disable="true" layoutX="10.0" layoutY="244.0" mnemonicParsing="false" onaction="#showRepositoryUI" prefWidth="200.0" text="%repository">
                                <graphic>
                                    <ImageView fitHeight="35.0" fitWidth="35.0" pickOnBounds="true" preserveRatio="true">
                                        <image>
                                            <Image url="@repository.png" />
                                        </image>
                                    </ImageView>
                                </graphic>
                   <tooltip>
                      <Tooltip text="%menu_repository_tooltip" />
                   </tooltip>
                            </Button>
                            <Button id="aboutButton" fx:id="aboutButton" alignment="TOP_LEFT" disable="true" layoutX="20.0" layoutY="23.0" mnemonicParsing="false" onaction="#showaboutUI" prefWidth="200.0" text="%about">
                                <graphic>
                                    <ImageView fitHeight="35.0" fitWidth="35.0" pickOnBounds="true" preserveRatio="true">
                                        <image>
                                            <Image url="@about.png" />
                                        </image>
                                    </ImageView>
                                </graphic>
                   <tooltip>
                      <Tooltip text="%menu_about_tooltip" />
                   </tooltip>
                            </Button>
                        </items>

                    </ToolBar>
            <Pane fx:id="centerPane" layoutX="240.0" layoutY="45.0" pickOnBounds="false" AnchorPane.bottomAnchor="645.0" AnchorPane.leftAnchor="240.0" AnchorPane.rightAnchor="710.0" AnchorPane.topAnchor="45.0" /> 


       </children>

    </AnchorPane>`

这是我单击登录按钮时要加载的loginpanel fxml。我也尝试添加单独的定位窗格来登录fxml。但没有运气。

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

<?import javafx.geometry.Insets?>
<?import javafx.geometry.Rectangle2D?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.CheckBox?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.control.PasswordField?>
<?import javafx.scene.control.ScrollPane?>
<?import javafx.scene.control.TextField?>
<?import javafx.scene.control.Tooltip?>
<?import javafx.scene.image.Image?>
<?import javafx.scene.image.ImageView?>
<?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?>
<?import javafx.scene.text.Font?>

<VBox id="loginFXML" fx:id="loginFXML" maxHeight="1.7976931348623157E308" maxWidth="1.7976931348623157E308" spacing="5.0" styleclass="parent-inner-panel" stylesheets="@content.css" xmlns="http://javafx.com/javafx/8.0.65" xmlns:fx="http://javafx.com/fxml/1" fx:controller="com.ibadmin.login.LoginGridPaneController">
   <children>
      <Label styleclass="headerLabel" stylesheets="@dark.css" text="%login" textFill="WHITE">
         <graphic>
            <ImageView fitHeight="35.0" fitWidth="35.0" pickOnBounds="true" preserveRatio="true">
               <image>
                  <Image url="@login.png" />
               </image>
            </ImageView>
         </graphic>
         <font>
            <Font name="System Bold" size="12.0" />
         </font>
      </Label>
      <VBox styleclass="border_pane" stylesheets="@dark.css" VBox.vgrow="ALWAYS">
         <children>
            <HBox alignment="CENTER_LEFT">
               <children>
                  <Label contentDisplay="CENTER" prefWidth="110.0" text="%serverInfo" textAlignment="CENTER" />
                  <ScrollPane fx:id="serverInfoScrollPane">
                     <content>
                        <GridPane fx:id="serverInfoGridPane" alignment="TOP_CENTER" hgap="1.0" vgap="1.0">
                          <columnConstraints>
                            <ColumnConstraints hgrow="SOMETIMES" maxWidth="200.0" minWidth="200.0" prefWidth="200.0" />
                            <ColumnConstraints hgrow="SOMETIMES" maxWidth="200.0" minWidth="200.0" prefWidth="200.0" />
                          </columnConstraints>
                          <rowConstraints>
                            <RowConstraints maxHeight="162.0" minHeight="162.0" prefHeight="162.0" vgrow="SOMETIMES" />
                          </rowConstraints>
                        </GridPane>
                     </content>
                     <padding>
                        <Insets bottom="2.0" left="2.0" right="2.0" top="2.0" />
                     </padding>
                  </ScrollPane>
                  <VBox alignment="CENTER_LEFT">
                     <children>
                        <Button minHeight="25.0" minWidth="29.0" mnemonicParsing="false" onaction="#deleteServerInfoButtonaction">
                           <tooltip>
                              <Tooltip text="%loginDeleteServerTooltip" />
                           </tooltip>
                           <VBox.margin>
                              <Insets bottom="5.0" left="5.0" right="5.0" top="5.0" />
                           </VBox.margin>
                           <graphic>
                              <ImageView fitHeight="15.0" fitWidth="19.0" pickOnBounds="true" preserveRatio="true">
                                 <image>
                                    <Image url="@delete.png" />
                                 </image>
                                 <viewport>
                                    <Rectangle2D />
                                 </viewport>
                              </ImageView>
                           </graphic>
                        </Button>
                        <Button fx:id="addServerInfoButton" minHeight="25.0" minWidth="29.0" mnemonicParsing="false" onaction="#addServerInfoaction">
                           <tooltip>
                              <Tooltip text="%loginAddServerTooltip" />
                           </tooltip>
                           <VBox.margin>
                              <Insets bottom="5.0" left="5.0" right="5.0" top="5.0" />
                           </VBox.margin>
                           <graphic>
                              <ImageView fitHeight="15.0" fitWidth="19.0" pickOnBounds="true" preserveRatio="true">
                                 <image>
                                    <Image url="@add.png" />
                                 </image>
                              </ImageView>
                           </graphic>
                        </Button>
                     </children>
                  </VBox>
               </children>
               <VBox.margin>
                  <Insets bottom="5.0" />
               </VBox.margin>
            </HBox>
            <GridPane>
              <columnConstraints>
                <ColumnConstraints hgrow="SOMETIMES" maxWidth="110.0" minWidth="110.0" prefWidth="110.0" />
                <ColumnConstraints hgrow="SOMETIMES" maxWidth="430.0" minWidth="430.0" prefWidth="430.0" />
                  <ColumnConstraints hgrow="SOMETIMES" />
              </columnConstraints>
              <rowConstraints>
                <RowConstraints maxHeight="35.0" minHeight="35.0" prefHeight="35.0" vgrow="SOMETIMES" />
                <RowConstraints maxHeight="35.0" minHeight="35.0" prefHeight="35.0" vgrow="SOMETIMES" />
              </rowConstraints>
               <children>
                            <Label id="usernameLabel" text="%user">
                     <GridPane.margin>
                        <Insets />
                     </GridPane.margin>
                  </Label>

                                                                <Label id="passwordLabel" text="%password" GridPane.rowIndex="1">
                     <GridPane.margin>
                        <Insets />
                     </GridPane.margin>
                  </Label>
                            <TextField id="usernameTextField" fx:id="usernameTextField" maxWidth="405.0" GridPane.columnIndex="1" GridPane.columnSpan="2">
                     <GridPane.margin>
                        <Insets />
                     </GridPane.margin>
                  </TextField>
                            <PasswordField id="passwordPasswordField" fx:id="passwordPasswordField" maxWidth="405.0" minWidth="405.0" prefWidth="405.0" GridPane.columnIndex="1" GridPane.columnSpan="2" GridPane.rowIndex="1">
                     <GridPane.margin>
                        <Insets />
                     </GridPane.margin>
                  </PasswordField>
                  <CheckBox fx:id="rememberMeCheckBox" mnemonicParsing="false" onaction="#rememberMeCheckBoxaction" stylesheets="@dark.css" text="%rememberMe" GridPane.columnIndex="2" />
                  <Button fx:id="forgotPasswordButton" alignment="TOP_LEFT" contentDisplay="RIGHT" mnemonicParsing="false" onaction="#forgotPasswordButtonaction" text="%forgotPassword" GridPane.columnIndex="2" GridPane.rowIndex="1">
                     <graphic>
                        <ImageView fitHeight="15.0" fitWidth="19.0" pickOnBounds="true" preserveRatio="true">
                           <image>
                              <Image url="@forgotpw.png" />
                           </image>
                        </ImageView>
                     </graphic></Button>
               </children>
               <VBox.margin>
                  <Insets />
               </VBox.margin>
            </GridPane>
            <GridPane prefHeight="55.0">
              <columnConstraints>
                <ColumnConstraints hgrow="SOMETIMES" percentWidth="15.0" />
                <ColumnConstraints hgrow="SOMETIMES" percentWidth="27.0" prefWidth="200.0" />
                  <ColumnConstraints hgrow="SOMETIMES" percentWidth="27.0" prefWidth="200.0" />
                  <ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" percentWidth="27.0" prefWidth="200.0" />
              </columnConstraints>
              <rowConstraints>
                <RowConstraints minHeight="50.0" vgrow="SOMETIMES" />
                  <RowConstraints minHeight="50.0" vgrow="SOMETIMES" />
              </rowConstraints>
               <children>
                  <Button id="loginButton" fx:id="loginButton" contentDisplay="CENTER" defaultButton="true" maxHeight="45.0" maxWidth="1.7976931348623157E308" minWidth="-Infinity" mnemonicParsing="false" onaction="#login" styleclass="default_button" stylesheets="@dark.css" GridPane.columnIndex="1">
                     <tooltip>
                        <Tooltip text="%loginConnectToolTip" />
                     </tooltip>
                     <graphic>
                        <Label prefWidth="160.0" text="%connect">
                           <padding>
                              <Insets left="30.0" />
                           </padding>
                        </Label>
                     </graphic>
                     <GridPane.margin>
                        <Insets right="5.0" />
                     </GridPane.margin>
                  </Button>
                  <Button fx:id="disconnectButton" contentDisplay="CENTER" disable="true" maxHeight="45.0" maxWidth="1.7976931348623157E308" mnemonicParsing="false" onaction="#disconnectButtonClicked" GridPane.columnIndex="2">
                     <tooltip>
                        <Tooltip text="%loginDIcconnectToolTip" />
                     </tooltip>
                     <graphic>
                        <Label prefWidth="160.0" text="%disconnect">
                           <padding>
                              <Insets left="30.0" />
                           </padding>
                        </Label>
                     </graphic>
                     <GridPane.margin>
                        <Insets right="5.0" />
                     </GridPane.margin>
                  </Button>
                  <ImageView disable="true" fitHeight="35.0" fitWidth="35.0" nodeOrientation="INHERIT" pickOnBounds="true" preserveRatio="true" GridPane.columnIndex="1">
                     <image>
                        <Image url="@Connect.png" />
                     </image>
                     <GridPane.margin>
                        <Insets bottom="5.0" left="5.0" right="5.0" top="5.0" />
                     </GridPane.margin>
                  </ImageView>
                  <ImageView fitHeight="35.0" fitWidth="35.0" pickOnBounds="true" preserveRatio="true" GridPane.columnIndex="2">
                     <image>
                        <Image url="@Disconnect.png" />
                     </image>
                     <GridPane.margin>
                        <Insets bottom="5.0" left="5.0" right="5.0" top="5.0" />
                     </GridPane.margin>
                  </ImageView>
                  <Button fx:id="shutdownServerButton" contentDisplay="CENTER" disable="true" maxHeight="45.0" maxWidth="1.7976931348623157E308" mnemonicParsing="false" onaction="#shutdownServerButtonClicked" GridPane.columnIndex="1" GridPane.rowIndex="1">
                     <tooltip>
                        <Tooltip text="%loginShutdownServertToolTip" />
                     </tooltip>
                     <graphic>
                        <Label prefWidth="180.0" text="%shutdownServer">
                           <padding>
                              <Insets left="33.0" />
                           </padding>
                        </Label>
                     </graphic>
                     <GridPane.margin>
                        <Insets right="5.0" />
                     </GridPane.margin>
                  </Button>
                  <ImageView fitHeight="35.0" fitWidth="35.0" pickOnBounds="true" preserveRatio="true" GridPane.columnIndex="1" GridPane.rowIndex="1">
                     <image>
                        <Image url="@Hard_Restart.png" />
                     </image>
                     <GridPane.margin>
                        <Insets bottom="5.0" left="5.0" right="5.0" top="5.0" />
                     </GridPane.margin>
                  </ImageView>
                  <Button fx:id="softRestartButton" contentDisplay="CENTER" disable="true" maxHeight="45.0" maxWidth="1.7976931348623157E308" mnemonicParsing="false" onaction="#softRestartButtonClicked" textOverrun="CENTER_ELLIPSIS" visible="true" GridPane.columnIndex="2" GridPane.rowIndex="1">
                     <tooltip>
                        <Tooltip text="%loginSoftRestartToolTip" />
                     </tooltip>
                     <graphic>
                        <Label prefHeight="17.0" prefWidth="160.0" text="%softRestart">
                           <padding>
                              <Insets left="30.0" />
                           </padding>
                        </Label>
                     </graphic>
                     <GridPane.margin>
                        <Insets right="5.0" />
                     </GridPane.margin>
                  </Button>
                  <Button fx:id="hardRestartButton" contentDisplay="CENTER" disable="true" maxHeight="45.0" maxWidth="1.7976931348623157E308" mnemonicParsing="false" onaction="#hardRestartButtonClicked" visible="true" GridPane.columnIndex="3" GridPane.rowIndex="1">
                     <tooltip>
                        <Tooltip text="%loginHardRestartToolTip" />
                     </tooltip>
                     <graphic>
                        <Label prefHeight="17.0" prefWidth="160.0" text="%hardRestart">
                           <padding>
                              <Insets left="30.0" />
                           </padding>
                        </Label>
                     </graphic>
                     <GridPane.margin>
                        <Insets right="5.0" />
                     </GridPane.margin>
                  </Button>
                  <ImageView fx:id="hardRestartImageView" fitHeight="35.0" fitWidth="35.0" pickOnBounds="true" preserveRatio="true" GridPane.columnIndex="3" GridPane.rowIndex="1">
                     <image>
                        <Image url="@Shutdown.png" />
                     </image>
                     <GridPane.margin>
                        <Insets bottom="5.0" left="5.0" right="5.0" top="5.0" />
                     </GridPane.margin>
                  </ImageView>
                  <ImageView fitHeight="35.0" fitWidth="35.0" pickOnBounds="true" preserveRatio="true" GridPane.columnIndex="2" GridPane.rowIndex="1">
                     <image>
                        <Image url="@Soft_Restart.png" />
                     </image>
                     <GridPane.margin>
                        <Insets bottom="5.0" left="5.0" right="5.0" top="5.0" />
                     </GridPane.margin>
                  </ImageView>
               </children>
               <VBox.margin>
                  <Insets />
               </VBox.margin>
            </GridPane>
         </children>
      </VBox>
   </children>
   <padding>
      <Insets bottom="5.0" left="5.0" right="5.0" top="5.0" />
   </padding>
</VBox>`

这是单击登录按钮时调用的功能

  `
     @FXML
      private void showLoginUI() {
        try {
          minimizeHistoryInProjectsTabIfOpen();
          if (!isCurrentNode(ConstantClass.LOGIN_GRID_PANE_FXML_ID)) {


            Node node = getIndexofUIFromID(centerPane.getchildren(),ConstantClass.LOGIN_GRID_PANE_FXML_ID);

            if (node == null) {
              loginGridPaneController =
                  (LoginGridPaneController) addNode(ConstantClass.LOGIN_GRID_PANE_FXML_ID,ConstantClass.LOGIN_GRID_PANE_FXML);
              loginGridPaneController.setadminPanelController(this);
              setButtonColorOnFocus(loginButton);
              setButtonColorOnFocusLost(loginButton);
            } else {
              changeUIForThisnode(node);
              loginGridPaneController.getcredentialsDetails();

              setButtonColorOnFocus(loginButton);
              setButtonColorOnFocusLost(loginButton);
            }
          }
        } catch (Exception e) {
          MessageBox.displayErrorMessage(MessageKey.ADMINPANEL_SHOW_LOGIN_UI_FILE_MISSING_ERROR);
          log.error(e.getMessage(),e);
        }
      }
 private void changeUIForThisnode(Node node) {
    if (currentNode != null) {
      currentNode.setVisible(false);
    }
    node.setVisible(true);
    currentNode = node;
  }`
money000000 回答:JavaFX可调整大小的UI

暂时没有好的解决方案,如果你有好的解决方案,请发邮件至:iooj@foxmail.com
本文链接:https://www.f2er.com/2841462.html

大家都在问