操作按钮不起作用(SceneBuilder JavaFX)

Action Button does not work (SceneBuilder JavaFX)

我正在尝试使用 Scene Builder 创建的按钮切换场景。

那是我的控制器class

public class Controller implements Initializable {

    @FXML Button LoginButton;

       @FXML private void handleButtonAction(ActionEvent event) throws IOException {

            Parent tableViewParent = FXMLLoader.load(getClass().getResource("../FXML/MainMenu.fxml"));
            Scene tableViewScene = new Scene(tableViewParent);

            Stage window = (Stage) ((Node)event.getSource()).getScene().getWindow();
            window.setScene(tableViewScene);
            window.show();
        }


    @Override
    public void initialize(URL location, ResourceBundle resources) {


    }

还有 FXML

<VBox prefHeight="400.0" prefWidth="640.0" xmlns="http://javafx.com/javafx/9.0.4" xmlns:fx="http://javafx.com/fxml/1" fx:controller="Controllers.Controller">

    <MenuBar VBox.vgrow="NEVER" />
    <AnchorPane id="login" maxHeight="-1.0" maxWidth="-1.0" prefHeight="-1.0" prefWidth="-1.0" style="-fx-background-color: #ccf2ff #ccf2ff;" VBox.vgrow="ALWAYS">
         <children>
            <AnchorPane id="login2" layoutX="320.0" prefHeight="371.0" prefWidth="316.0">
               <children>
                  <TextField alignment="TOP_CENTER" layoutX="75.0" layoutY="95.0" promptText="Login">
                     <effect>
                        <InnerShadow />
                     </effect></TextField>
                  <PasswordField alignment="TOP_CENTER" layoutX="75.0" layoutY="159.0" promptText="Password">
                     <effect>
                        <InnerShadow blurType="TWO_PASS_BOX" />
                     </effect></PasswordField>
                  <Button layoutX="129.0" layoutY="220.0" mnemonicParsing="false" onAction="#handleButtonAction" text="Button" />

               </children>
            </AnchorPane>
         </children>
    </AnchorPane>
      <AnchorPane />
  </children>
</VBox>

出于某种原因,IntelliJ 显示错误无法解析 'handleButtonAction'

onAction="#handleButtonAction"

在您的控制器中编写以下代码

@FXML
protected void handleButtonAction() {
    System.out.println("In handleButtonAction");
// your logic 
}

FXML 文件

<Button layoutX="129.0" layoutY="220.0" mnemonicParsing="false" onAction="#handleButtonAction" text="Button" />

我希望它会起作用...我使用了 scenebuilder 用于 FXML,如果不起作用请给我发送错误截图

您可以检索场景(和舞台)以及属于它的任何 FXML 元素。你只需要做 anyElement.getScene().getWindow()

public class Controller {

   @FXML 
   Button loginButton;

   @FXML 
   private void handleButtonAction() {

      Parent tableViewParent = FXMLLoader.load(getClass().getResource("../FXML/MainMenu.fxml"));
      Scene tableViewScene = new Scene(tableViewParent);

      // Instead of retrieving the stage by the event's source, you can do it by one of your FXML component.
      Stage window = (Stage) loginButton.getScene().getWindow();
      window.setScene(tableViewScene);
      window.show();
   }


   @FXML
   public void initialize() {
      //initialize the field you want here.
   }
}

以及 FXML:

<VBox prefHeight="400.0" prefWidth="640.0" xmlns="http://javafx.com/javafx/9.0.4" xmlns:fx="http://javafx.com/fxml/1" fx:controller="Controllers.Controller">
   <!-- Fabian was right, you forgot this children. -->
   <children>
      <MenuBar VBox.vgrow="NEVER" />
         <AnchorPane id="login" maxHeight="-1.0" maxWidth="-1.0" prefHeight="-1.0" prefWidth="-1.0" style="-fx-background-color: #ccf2ff #ccf2ff;" VBox.vgrow="ALWAYS">
            <children>
               <AnchorPane id="login2" layoutX="320.0" prefHeight="371.0" prefWidth="316.0">
                  <children>
                     <TextField alignment="TOP_CENTER" layoutX="75.0" layoutY="95.0" promptText="Login">
                        <effect>
                           <InnerShadow />
                        </effect>
                     </TextField>
                     <PasswordField alignment="TOP_CENTER" layoutX="75.0" layoutY="159.0" promptText="Password">
                        <effect>
                           <InnerShadow blurType="TWO_PASS_BOX" />
                        </effect>
                     </PasswordField>
                  <Button fx:id="loginButton" layoutX="129.0" layoutY="220.0" mnemonicParsing="false" onAction="#handleButtonAction" text="Button" />
               </children>
            </AnchorPane>
         </children>
      </AnchorPane>
      <AnchorPane />
   </children>
</VBox>

您的代码有些 explanation/remarks。

  • 自 JavaFX 8 起无法实现 Initializable。initialize 带有 FXML 注释的方法现在就足够了(查看 note in interface definition)。
  • 将 loginButton 的第一个字母改为小写 (Java naming convention)
  • Fabian 的评论是对的,您忘记了 FXML 中 VBox 节点的子元素起始元素。
  • 我没有在您的 FXML 中找到登录按钮 fx:id。你忘记了吗?我在 FXML 的 Button 元素中添加了它。 (我不知道它是不是有效的登录按钮...)