FXML:如何添加到 StackPane 响应行为?

问题描述 投票:0回答:1

我正在尝试使用 JavaFX StackPane 创建响应式布局。在 StackPane 内,我放置了几个 AnchorPanes,下面是 FXML 示例代码(此代码是我的互联网研究的示例):

<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.CheckBox?>
<?import javafx.scene.control.ComboBox?>
<?import javafx.scene.control.Hyperlink?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.control.PasswordField?>
<?import javafx.scene.control.TextField?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.layout.StackPane?>
<?import javafx.scene.text.Font?>
<StackPane xmlns="http://javafx.com/javafx/8.0.171" xmlns:fx="http://javafx.com/fxml/1" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="550.0" prefWidth="335.0" fx:controller="loginregisterforgotpass.FXMLDocumentController">
   <children>
      <AnchorPane fx:id="main_form" prefHeight="200.0" prefWidth="200.0" styleClass="white-bg" stylesheets="@../design/style.css">
         <children>
            <AnchorPane fx:id="login_form" layoutX="12.0" layoutY="15.0" prefHeight="520.0" prefWidth="312.0">
               <children>
                  <!--
                  <FontAwesomeIcon fill="#2a827b" glyphName="USER" layoutX="132.0" layoutY="77.0" size="5em" />
-->
                  <Label layoutX="93.0" layoutY="86.0" text="Sign In Account" textFill="#187778">
                     <font>
                        <Font name="Gill Sans MT" size="20.0" />
                     </font>
                  </Label>
                  <TextField fx:id="login_username" layoutX="37.0" layoutY="152.0" prefHeight="30.0" prefWidth="237.0" promptText="Username" styleClass="textfield" stylesheets="@../design/style.css" />
                  <PasswordField fx:id="login_password" layoutX="38.0" layoutY="197.0" prefHeight="30.0" prefWidth="237.0" promptText="Password" styleClass="textfield" stylesheets="@../design/style.css" />
                  <TextField fx:id="login_showPassword" layoutX="38.0" layoutY="197.0" prefHeight="30.0" prefWidth="237.0" promptText="Password" styleClass="textfield" stylesheets="@../design/style.css" visible="false" />
                  <CheckBox fx:id="login_selectShowPassword" layoutX="41.0" layoutY="234.0" mnemonicParsing="false" onAction="#showPassword" text="Show Password" />
                  <Button fx:id="login_btn" layoutX="38.0" layoutY="278.0" mnemonicParsing="false" onAction="#login" prefHeight="40.0" prefWidth="237.0" styleClass="btn" stylesheets="@../design/style.css" text="Login" />
                  <Label layoutX="103.0" layoutY="428.0" text="Register Account">
                     <font>
                        <Font name="Gill Sans MT" size="15.0" />
                     </font>
                  </Label>
                  <Button fx:id="login_createAccount" layoutX="37.0" layoutY="461.0" mnemonicParsing="false" onAction="#switchForm" prefHeight="30.0" prefWidth="237.0" styleClass="btn" stylesheets="@../design/style.css" text="Create Account" />
                  <Hyperlink fx:id="login_forgotPassword" layoutX="164.0" layoutY="229.0" onAction="#switchForm" text="Forgot Password?">
                     <font>
                        <Font size="13.0" />
                     </font>
                  </Hyperlink>
               </children>
            </AnchorPane>
            <AnchorPane fx:id="signup_form" layoutX="12.0" layoutY="15.0" prefHeight="520.0" prefWidth="312.0" visible="false">
               <children>
                  <Label layoutX="88.0" layoutY="74.0" text="Sign Up Account" textFill="#187778">
                     <font>
                        <Font name="Gill Sans MT" size="20.0" />
                     </font>
                  </Label>
                  <TextField fx:id="signup_email" layoutX="39.0" layoutY="114.0" prefHeight="30.0" prefWidth="237.0" promptText="Email Address" styleClass="textfield" stylesheets="@../design/style.css" />
                  <TextField fx:id="signup_username" layoutX="39.0" layoutY="155.0" prefHeight="30.0" prefWidth="237.0" promptText="Username" styleClass="textfield" stylesheets="@../design/style.css" />
                  <PasswordField fx:id="signup_password" layoutX="39.0" layoutY="197.0" prefHeight="30.0" prefWidth="237.0" promptText="Password" styleClass="textfield" stylesheets="@../design/style.css" />
                  <PasswordField fx:id="signup_cPassword" layoutX="37.0" layoutY="240.0" prefHeight="30.0" prefWidth="237.0" promptText="Confirm Password" styleClass="textfield" stylesheets="@../design/style.css" />
                  <Button fx:id="signup_btn" layoutX="37.0" layoutY="366.0" mnemonicParsing="false" onAction="#register" prefHeight="40.0" prefWidth="237.0" styleClass="btn" stylesheets="@../design/style.css" text="Sign Up" />
                  <Label layoutX="110.0" layoutY="443.0" text="Sign In Account">
                     <font>
                        <Font name="Gill Sans MT" size="15.0" />
                     </font>
                  </Label>
                  <Button fx:id="signup_loginAccount" layoutX="38.0" layoutY="476.0" mnemonicParsing="false" onAction="#switchForm" prefHeight="30.0" prefWidth="237.0" styleClass="btn" stylesheets="@../design/style.css" text="Login Account" />
                  <ComboBox fx:id="signup_selectQuestion" layoutX="37.0" layoutY="284.0" prefHeight="30.0" prefWidth="237.0" promptText="Select Question" />
                  <TextField fx:id="signup_answer" layoutX="37.0" layoutY="325.0" prefHeight="30.0" prefWidth="237.0" promptText="Answer" styleClass="textfield" stylesheets="@../design/style.css" />
               </children>
            </AnchorPane>
            <AnchorPane fx:id="forgot_form" layoutX="12.0" layoutY="15.0" prefHeight="520.0" prefWidth="312.0" visible="false">
               <children>
                  <Label layoutX="86.0" layoutY="84.0" text="Forget Password" textFill="#187778">
                     <font>
                        <Font name="Gill Sans MT" size="20.0" />
                     </font>
                  </Label>
                  <TextField fx:id="forgot_answer" layoutX="37.0" layoutY="230.0" prefHeight="30.0" prefWidth="237.0" promptText="Answer" styleClass="textfield" stylesheets="@../design/style.css" />
                  <Button fx:id="forgot_proceedBtn" layoutX="38.0" layoutY="277.0" mnemonicParsing="false" onAction="#forgotPassword" prefHeight="40.0" prefWidth="237.0" styleClass="btn" stylesheets="@../design/style.css" text="Proceed" />
                  <Button fx:id="forgot_backBtn" layoutX="37.0" layoutY="461.0" mnemonicParsing="false" onAction="#switchForm" prefHeight="30.0" prefWidth="237.0" styleClass="btn" stylesheets="@../design/style.css" text="Back" />
                  <ComboBox fx:id="forgot_selectQuestion" layoutX="37.0" layoutY="184.0" prefHeight="30.0" prefWidth="237.0" promptText="Select Question" />
                  <TextField fx:id="forgot_username" layoutX="37.0" layoutY="141.0" prefHeight="30.0" prefWidth="237.0" promptText="Username" />
               </children>
            </AnchorPane>
            <AnchorPane fx:id="changePass_form" layoutX="12.0" layoutY="15.0" prefHeight="520.0" prefWidth="312.0" visible="false">
               <children>
                  <Label layoutX="86.0" layoutY="84.0" text="Forget Password" textFill="#187778">
                     <font>
                        <Font name="Gill Sans MT" size="20.0" />
                     </font>
                  </Label>
                  <Button fx:id="changePass_proceedBtn" layoutX="36.0" layoutY="260.0" mnemonicParsing="false" onAction="#changePassword" prefHeight="40.0" prefWidth="237.0" styleClass="btn" stylesheets="@../design/style.css" text="Change Password" />
                  <Button fx:id="changePass_backBtn" layoutX="37.0" layoutY="461.0" mnemonicParsing="false" onAction="#switchForm" prefHeight="30.0" prefWidth="237.0" styleClass="btn" stylesheets="@../design/style.css" text="Back" />
                  <PasswordField fx:id="changePass_password" layoutX="36.0" layoutY="167.0" prefHeight="30.0" prefWidth="237.0" promptText="Password" />
                  <PasswordField fx:id="changePass_cPassword" layoutX="36.0" layoutY="209.0" prefHeight="30.0" prefWidth="237.0" promptText="Confirm Password" />
               </children>
            </AnchorPane>
         </children>
      </AnchorPane>
   </children>
</StackPane>
由于其分层架构,StackPane 似乎是创建登录/注册对话框的最佳解决方案。如何向此 FXML 代码添加响应行为?

java user-interface javafx fxml scenebuilder
1个回答
0
投票
我通过用 VBox 和 HBox 包围 AnchorPanes 列表解决了这个问题:

<StackPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="550.0" prefWidth="325.0" xmlns="http://javafx.com/javafx/22" xmlns:fx="http://javafx.com/fxml/1" fx:controller=""> <children> <VBox maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="550.0" prefWidth="325.0"> <HBox VBox.vgrow="ALWAYS"> <AnchorPane> </AnchorPane> </children> </AnchorPane> </HBox> </VBox> </children> </StackPane>
    
© www.soinside.com 2019 - 2024. All rights reserved.