JavaFX,我的注释和我的事件出错

JavaFX, Error with my annotation and my on event

在控制器文件中显示 public void handle(ActionEvent event)event 是红色的,因为我是 JavaFX 的新手,所以我仍然不知道是什么导致了这个问题,关于我的任何想法可以做什么,我可以做什么来改进我的代码?

我不太了解高级词汇及其含义,所以如果能有更简单的方法来解释您的解释中的含义,那就太好了。

Main.java

package sample;

import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Alert;
import javafx.scene.layout.Pane;
import javafx.stage.Stage;
import javafx.stage.Window;

public class Main extends Application {

    @Override
    public void start(Stage primaryStage) throws Exception{
        Parent root = FXMLLoader.load(getClass().getResource("Loginone.fxml"));
        primaryStage.setTitle("Login1st");
        primaryStage.setScene(new Scene(root, 900, 600));
        primaryStage.show();

    }

    public void showAlert(Alert.AlertType alertType, Window owner, String title, String message) {
      Alert alert = new Alert(alertType);
      alert.setTitle(title);
      alert.setHeaderText(null);
      alert.setContentText(message);
      alert.initOwner(owner);
      alert.show();
    }

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

Controller.java

package sample;

import javafx.event.ActionEvent;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.fxml.FXML;
import javafx.scene.control.Alert;
import javafx.scene.control.Button;
import javafx.scene.control.PasswordField;
import javafx.scene.control.TextField;
import javafx.scene.layout.Pane;
import javafx.scene.text.Text;
import javafx.stage.Window;
import sample.Main;

public class Controller {

  @FXML
  private Pane wholeLightPurple;

  @FXML
  private Pane purpleBox;

  @FXML
  private Text LoginTitle;

  @FXML
  private TextField userIDbox;

  @FXML
  private Text userIDText;

  @FXML
  private Text passwordText;

  @FXML
  public Button submitButton;

  @FXML
  private PasswordField passwordField;

  @Override
  submitButton.setOnAction(new EventHandler<ActionEvent>()) {
     public void handle(ActionEvent event) {
       if (passwordField.getText().isEmpty()) {
         showAlert(Alert.AlertType.ERROR, purpleBox.getScene().getWindow(), "Form Error!", "Please Fill out the Form");
       }
    }

  }


  public void showAlert(Alert.AlertType alertType, Window owner, String title, String message) {
    Alert alert = new Alert(alertType);
    alert.setTitle(title);
    alert.setHeaderText(null);
    alert.setContentText(message);
    alert.initOwner(owner);
    alert.show();
  }






}

Loginone.fxml

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

<?import javafx.scene.control.Button?>
<?import javafx.scene.control.PasswordField?>
<?import javafx.scene.control.TextField?>
<?import javafx.scene.effect.DropShadow?>
<?import javafx.scene.layout.Pane?>
<?import javafx.scene.text.Font?>
<?import javafx.scene.text.Text?>

<Pane fx:id="wholeLightPurple" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="548.0" prefWidth="828.0" scaleShape="false" style="-fx-background-color: #945cb4;" xmlns="http://javafx.com/javafx/8.0.171" xmlns:fx="http://javafx.com/fxml/1">
   <children>
      <Pane fx:id="purpleBox" layoutX="484.0" prefHeight="548.0" prefWidth="345.0" scaleShape="false" style="-fx-background-color: #3c1361;">
         <effect>
            <DropShadow />
         </effect>
         <children>
            <Text id="LoginTitle" fx:id="LoginTitle" fill="#d0b4dc" fontSmoothingType="LCD" layoutX="112.0" layoutY="106.0" strokeType="OUTSIDE" strokeWidth="0.0" text="Login" textAlignment="CENTER">
               <font>
                  <Font name="Times New Roman Bold" size="48.0" />
               </font>
            </Text>
            <TextField fx:id="userIDbox" layoutX="66.0" layoutY="249.0" prefHeight="25.0" prefWidth="213.0" style="-fx-background-color: #FFFFFF;" />
            <Text fx:id="userIDText" fill="WHITE" layoutX="66.0" layoutY="238.0" strokeType="OUTSIDE" strokeWidth="0.0" text="User ID :">
               <font>
                  <Font name="Times New Roman Bold" size="14.0" />
               </font>
            </Text>
            <Text fx:id="passwordText" fill="WHITE" layoutX="66.0" layoutY="322.0" strokeType="OUTSIDE" strokeWidth="0.0" text="Password :">
               <font>
                  <Font name="Times New Roman Bold" size="14.0" />
               </font>
            </Text>
            <Button fx:id="submitButton" alignment="CENTER" layoutX="123.0" layoutY="387.0" mnemonicParsing="false" prefHeight="16.0" prefWidth="98.0" style="-fx-background-color: #ffffff;" text="Submit" textAlignment="CENTER" wrapText="true">
               <font>
                  <Font name="System Bold" size="12.0" />
               </font></Button>
            <PasswordField fx:id="passwordField" layoutX="66.0" layoutY="326.0" prefHeight="25.0" prefWidth="213.0" />
         </children>
      </Pane>
   </children>
</Pane>

您在问题中发布的代码无法编译。我修好了它。它出现在下面。将其与您的代码进行比较。

我更改了文件 Controller.java。向 submitButton 添加事件处理程序的代码是错误的。它不在方法内部,所以我添加了方法 initialize(),它会在您加载 FXML 文件时自动调用。

我还更改了文件 Loginone.fxml。它缺少控制器的名称 class。
我没改Main.

Controller.java

package sample;

import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.fxml.FXML;
import javafx.scene.control.Alert;
import javafx.scene.control.Button;
import javafx.scene.control.PasswordField;
import javafx.scene.control.TextField;
import javafx.scene.layout.Pane;
import javafx.scene.text.Text;
import javafx.stage.Window;

public class Controller {

    @FXML
    private Pane wholeLightPurple;

    @FXML
    private Pane purpleBox;

    @FXML
    private Text loginTitle;

    @FXML
    private TextField userIDbox;

    @FXML
    private Text userIDText;

    @FXML
    private Text passwordText;

    @FXML
    public Button submitButton;

    @FXML
    private PasswordField passwordField;

    public void initialize() {
        submitButton.setOnAction(new EventHandler<ActionEvent>() {
            public void handle(ActionEvent event) {
                if (passwordField.getText().isEmpty()) {
                    showAlert(Alert.AlertType.ERROR, purpleBox.getScene().getWindow(), "Form Error!", "Please Fill out the Form");
                }
            }
        });
    }

    public void showAlert(Alert.AlertType alertType, Window owner, String title, String message) {
        Alert alert = new Alert(alertType);
        alert.setTitle(title);
        alert.setHeaderText(null);
        alert.setContentText(message);
        alert.initOwner(owner);
        alert.show();
    }
}

Loginone.fxml

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

<?import javafx.scene.control.Button?>
<?import javafx.scene.control.PasswordField?>
<?import javafx.scene.control.TextField?>
<?import javafx.scene.effect.DropShadow?>
<?import javafx.scene.layout.Pane?>
<?import javafx.scene.text.Font?>
<?import javafx.scene.text.Text?>

<Pane fx:id="wholeLightPurple" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="548.0" prefWidth="828.0" scaleShape="false" style="-fx-background-color: #945cb4;" xmlns="http://javafx.com/javafx/8.0.171" xmlns:fx="http://javafx.com/fxml/1" fx:controller="sample.Controller">
   <children>
      <Pane fx:id="purpleBox" layoutX="484.0" prefHeight="548.0" prefWidth="345.0" scaleShape="false" style="-fx-background-color: #3c1361;">
         <effect>
            <DropShadow />
         </effect>
         <children>
            <Text id="loginTitle" fx:id="LoginTitle" fill="#d0b4dc" fontSmoothingType="LCD" layoutX="112.0" layoutY="106.0" strokeType="OUTSIDE" strokeWidth="0.0" text="Login" textAlignment="CENTER">
               <font>
                  <Font name="Times New Roman Bold" size="48.0" />
               </font>
            </Text>
            <TextField fx:id="userIDbox" layoutX="66.0" layoutY="249.0" prefHeight="25.0" prefWidth="213.0" style="-fx-background-color: #FFFFFF;" />
            <Text fx:id="userIDText" fill="WHITE" layoutX="66.0" layoutY="238.0" strokeType="OUTSIDE" strokeWidth="0.0" text="User ID :">
               <font>
                  <Font name="Times New Roman Bold" size="14.0" />
               </font>
            </Text>
            <Text fx:id="passwordText" fill="WHITE" layoutX="66.0" layoutY="322.0" strokeType="OUTSIDE" strokeWidth="0.0" text="Password :">
               <font>
                  <Font name="Times New Roman Bold" size="14.0" />
               </font>
            </Text>
            <Button fx:id="submitButton" alignment="CENTER" layoutX="123.0" layoutY="387.0" mnemonicParsing="false" prefHeight="16.0" prefWidth="98.0" style="-fx-background-color: #ffffff;" text="Submit" textAlignment="CENTER" wrapText="true">
               <font>
                  <Font name="System Bold" size="12.0" />
               </font></Button>
            <PasswordField fx:id="passwordField" layoutX="66.0" layoutY="326.0" prefHeight="25.0" prefWidth="213.0" />
         </children>
      </Pane>
   </children>
</Pane>

这是 运行 应用程序的屏幕截图。

使用FXML控制器时请记得使用Initializable接口... 我像这样更改了您的控制器 class。 我解决了你的问题。我认为这 class 可以帮助您找到解决用户问题的方法。

import java.net.URL;
import java.util.ResourceBundle;
import javafx.event.ActionEvent;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Alert;
import javafx.scene.control.Button;
import javafx.scene.control.PasswordField;
import javafx.scene.control.TextField;
import javafx.scene.layout.Pane;
import javafx.scene.text.Text;
import javafx.stage.Window;

public class Controller implements Initializable {

    @FXML
    private Pane wholeLightPurple;

    @FXML
    private Pane purpleBox;

    @FXML
    private Text LoginTitle;

    @FXML
    private TextField userIDbox;

    @FXML
    private Text userIDText;

    @FXML
    private Text passwordText;

    @FXML
    public Button submitButton;

    @FXML
    private PasswordField passwordField;

    @Override
    public void initialize(URL location, ResourceBundle resources) {
        submitButton.setOnAction((event) -> {
        if (passwordField.getText().isEmpty()) {
            showAlert(Alert.AlertType.ERROR, purpleBox.getScene().getWindow(), "Form Error!", "Please Fill out the Form");
        }
       }
    );
    }

  public void showAlert(Alert.AlertType alertType, Window owner, String title, String message) {
    Alert alert = new Alert(alertType);
    alert.setTitle(title);
    alert.setHeaderText(null);
    alert.setContentText(message);
    alert.initOwner(owner);
    alert.show();
  }
}