英文:
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>
这是运行应用程序的屏幕截图。
英文:
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<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>
Here is a screen capture of the running app.
答案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) -> {
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();
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论