在 Controller 文件中显示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.javapackage 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
。它缺少 Controller 类的名称。
我没有更改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>
这是正在运行的应用程序的屏幕截图。关于java - JavaFX,我的注释和on事件出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63202644/