onAction="#handleButton1" 在 JavaFX FXML 中不起作用

onAction="#handleButton1" not working in JavaFX FXML

我使用 IntelliJ IDEA 2021.3 和 JavaFx17。

我正在尝试更改出现的屏幕,但程序甚至无法打开。因为 onAction 按钮无法解析 handleButton1。我在 SceneBuilder 中创建屏幕 我​​添加了控制器 class 并在导入按钮中添加了按钮功能,但它不起作用。

我的控制器Class

package com.example.family_tree;

import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.Node;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.stage.Stage;
import java.awt.event.ActionEvent;
import java.io.IOException;
public class Controller {
    Stage stage;
    Scene scene;
    Parent root;

    @FXML
    //Button createButton, importButton;

    public void handleButton1(ActionEvent event) throws IOException {

        Parent root = FXMLLoader.load(getClass().getResource("import.fxml"));
        stage = (Stage) ((Node) event.getSource()).getScene().getWindow();
        scene = new Scene(root, 750, 750);
        stage.setScene(scene);
        stage.show();
    }
}

主要Class

public class Main extends Application {
    @Override
    public void start(Stage stage) throws IOException {
        Parent root = FXMLLoader.load(getClass().getResource("Main.fxml"));
        stage.setScene(new Scene(root,750,750));
        stage.setTitle("Family Tree Application");
       // stage.initStyle(StageStyle.DECORATED);

        stage.show();
        stage.setResizable(true);

    }

    public static void main(String[] args) {
        launch();
    }
}

和 fxml 文件

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

<?import javafx.geometry.Insets?>
<?import javafx.scene.Cursor?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.effect.ColorAdjust?>
<?import javafx.scene.image.Image?>
<?import javafx.scene.image.ImageView?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.layout.Pane?>
<?import javafx.scene.layout.VBox?>
<?import javafx.scene.text.Font?>

<AnchorPane focusTraversable="true" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="719.0" prefWidth="1217.0" xmlns="http://javafx.com/javafx/17" xmlns:fx="http://javafx.com/fxml/1" fx:controller="com.example.family_tree.Controller">
   <children>
      <VBox layoutY="-7.0" prefHeight="719.0" prefWidth="310.0" style="-fx-background-color: #26a69a;">
         <children>
            <Pane prefHeight="234.0" prefWidth="309.0">
               <children>
                  <Label alignment="TOP_LEFT" layoutX="48.0" layoutY="164.0" prefHeight="56.0" prefWidth="207.0" text="FAMILY TREE  " textAlignment="CENTER" textOverrun="CLIP">
                     <font>
                        <Font name="Symbol" size="33.0" />
                     </font>
                     <effect>
                        <ColorAdjust brightness="-0.54" contrast="0.45" hue="-0.96" />
                     </effect>
                     <cursor>
                        <Cursor fx:constant="TEXT" />
                     </cursor>
                  </Label>
                  <ImageView fitHeight="150.0" fitWidth="200.0" layoutX="87.0" layoutY="14.0" pickOnBounds="true" preserveRatio="true">
                     <image>
                        <Image url="@family_tree.png" />
                     </image>
                  </ImageView>
               </children>
            </Pane>
            <Button alignment="BASELINE_LEFT" graphicTextGap="10.0" mnemonicParsing="false" prefHeight="55.0" prefWidth="309.0" style="-fx-background-color: #0097A7;" text="CREATE FAMILY TREE">
               <padding>
                  <Insets left="50.0" />
               </padding>
               <font>
                  <Font name="Arial" size="15.0" />
               </font>
            </Button>
            <Button alignment="BASELINE_LEFT" graphicTextGap="10.0" mnemonicParsing="false" onAction="#handleButton1" prefHeight="56.0" prefWidth="309.0" style="-fx-background-color: #0097A7;" text="IMPORT FAMILY TREE">
               <padding>
                  <Insets left="50.0" />
               </padding>
               <font>
                  <Font name="Arial" size="15.0" />
               </font>
            </Button>
            <Button alignment="BASELINE_LEFT" graphicTextGap="10.0" mnemonicParsing="false" prefHeight="56.0" prefWidth="309.0" style="-fx-background-color: #0097A7;" text="HELP">
               <padding>
                  <Insets left="50.0" />
               </padding>
               <font>
                  <Font name="Arial" size="15.0" />
               </font>
            </Button>
            <Button alignment="BASELINE_LEFT" graphicTextGap="10.0" mnemonicParsing="false" prefHeight="56.0" prefWidth="309.0" style="-fx-background-color: #0097A7;" text="MADE BY" wrapText="true">
               <padding>
                  <Insets left="50.0" />
               </padding>
               <font>
                  <Font name="Arial" size="15.0" />
               </font>
            </Button>
         </children>
      </VBox>
      <Button layoutX="587.0" layoutY="208.0" mnemonicParsing="false" prefHeight="339.0" prefWidth="339.0" style="-fx-background-color: #4CAF50;">
         <graphic>
            <ImageView fitHeight="339.0" fitWidth="391.0" pickOnBounds="true" preserveRatio="true">
               <image>
                  <Image url="@Adsız.png" />
               </image>
            </ImageView>
         </graphic>
      </Button>
      <Pane layoutX="309.0" layoutY="82.0" prefHeight="80.0" prefWidth="907.0">
         <children>
            <Label layoutY="-14.0" prefHeight="80.0" prefWidth="907.0" style="-fx-background-color: #26a69a;" text="CREATE NEW FAMILY TREE" textAlignment="CENTER">
               <font>
                  <Font name="Times New Roman" size="31.0" />
               </font>
            </Label>
         </children>
      </Pane>
      <VBox layoutX="983.0" layoutY="208.0" prefHeight="200.0" prefWidth="100.0" />
   </children>
</AnchorPane>

不要错误地混合 Swing/AWT 代码和 JavaFX 代码(通常也不是故意的)。

不要写:

import java.awt.event.ActionEvent;

写:

import javafx.event.ActionEvent;