JavaFX中,出现了关于我的注解和我的事件处理的错误。

huangapple go评论82阅读模式
英文:

JavaFX, Error with my annotation and my on event

问题

在控制器文件中,public void handle(ActionEvent event) 中的 event 是红色的,由于我对JavaFX不太熟悉,我仍然不知道是什么引起了这个问题,有关我可以做什么以及如何改进我的代码的任何想法吗?

我对高级词汇和它们的含义不太熟悉,所以如果您能以更简单的方式解释您的解释,那将会很好。

英文:

In the controller file where it says public void handle(ActionEvent event) the event is in red and since I'm new to JavaFX I still don't know what causes this issue, any ideas on what I can do, and what I can do to improve on my code?

I'm not too good with the high-level words and meaning of them so if there can be an easier way of explaining what that means in your explanation it would be great.

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>

答案1

得分: 0

我修复了你在问题中发布的代码。修复后的代码如下。请将其与你的代码进行比较。

我更改了文件 Controller.java。原本添加到 submitButton 的事件处理程序的代码是错误的,因为它不在任何方法内部。因此,我添加了一个名为 initialize() 的方法,它会在加载FXML文件时自动调用。

我还更改了文件 Loginone.fxml,因为它缺少控制器类的名称。
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>

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

JavaFX中,出现了关于我的注解和我的事件处理的错误。

英文:

The code you posted in your question does not compile. I fixed it. It appears below. Compare it with your code.

I changed file Controller.java. The code that adds an event handler to submitButton was wrong. It was not inside a method, so I added method initialize() which is automatically called when you load the FXML file.

I also changed file Loginone.fxml. It was missing the name of the controller class.
I did not change 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&lt;ActionEvent&gt;() {
            public void handle(ActionEvent event) {
                if (passwordField.getText().isEmpty()) {
                    showAlert(Alert.AlertType.ERROR, purpleBox.getScene().getWindow(), &quot;Form Error!&quot;, &quot;Please Fill out the Form&quot;);
                }
            }
        });
    }

    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

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

Here is a screen capture of the running app.

JavaFX中,出现了关于我的注解和我的事件处理的错误。

答案2

得分: 0

请记住,在使用FXML控制器时,请使用Initializable接口...
我将您的控制器类更改如下。
我解决了您的问题。我认为这个类可能会帮助您找到用户问题的解决方案。

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();
    }
}
英文:

Please remember to use Initializable interface when you use FXML controller...
I changed your controller class like this.
I solved you question. I think this class may help you to find a solution for user problem.

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) -&gt; {
if (passwordField.getText().isEmpty()) {
showAlert(Alert.AlertType.ERROR, purpleBox.getScene().getWindow(), &quot;Form Error!&quot;, &quot;Please Fill out the Form&quot;);
}
}
);
}
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();
}
}

huangapple
  • 本文由 发表于 2020年8月1日 15:01:02
  • 转载请务必保留本文链接:https://go.coder-hub.com/63202644.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定