我有一个 JavaFx 项目,它使用登录屏幕为经过认证的用户切换页面。但是,当我在文本字段和密码字段中使用正确的凭据按下登录按钮时,没有任何反应,也没有给出错误。有人可以帮我弄清楚为什么没有发生场景切换吗?
这是我的登录页面的 FXML 文件:
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.control.PasswordField?>
<?import javafx.scene.control.TextField?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.layout.BorderPane?>
<?import javafx.scene.text.Font?>
<?import javafx.scene.text.Text?>
<BorderPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="500.0" prefWidth="700.0" xmlns="http://javafx.com/javafx/20.0.1" xmlns:fx="http://javafx.com/fxml/1" fx:controller="com.example.mall.HelloController">
<left>
<AnchorPane prefHeight="500.0" prefWidth="350.0" style="-fx-background-color: #6D5090;" BorderPane.alignment="CENTER">
<children>
<Text fill="WHITE" layoutX="92.0" layoutY="244.0" strokeType="OUTSIDE" strokeWidth="0.0" text="The Shopping Mall">
<font>
<Font size="20.0" />
</font>
</Text>
</children>
</AnchorPane>
</left>
<right>
<AnchorPane prefHeight="200.0" prefWidth="415.0" BorderPane.alignment="CENTER">
<children>
<TextField id="username" fx:id="usernameField" layoutX="101.0" layoutY="208.0" onAction="#login" promptText="Username" style="-fx-background-color: transparent; -fx-border-color: #0598ff; -fx-border-width: 0px 0px 2px 0px;" />
<PasswordField id="password" fx:id="passwordField" layoutX="102.0" layoutY="261.0" onAction="#login" promptText="Password" style="-fx-background-color: transparent; -fx-border-color: #0598ff; -fx-border-width: 0px 0px 2px 0px;" />
<Button fx:id="loginButton" layoutX="129.0" layoutY="335.0" mnemonicParsing="false" onAction="#login" prefHeight="34.0" prefWidth="90.0" style="-fx-background-color: #6D5090;" text="Login" textFill="WHITE" />
<Text fill="#6d5090" layoutX="125.0" layoutY="129.0" strokeType="OUTSIDE" strokeWidth="0.0" text="User Login">
<font>
<Font size="22.0" />
</font>
</Text>
<Button layoutX="320.0" mnemonicParsing="false" onAction="#exit" prefHeight="28.0" prefWidth="30.0" style="-fx-background-color: transparent;">
<graphic>
<Text fill="#6d5090" strokeType="OUTSIDE" strokeWidth="0.0" text="X">
<font>
<Font size="16.0" />
</font>
</Text>
</graphic>
</Button>
<Label fx:id="errorLabel" layoutX="106.0" layoutY="402.0" text="Incorrect Login. Try Again" textFill="#ff4d4d" visible="false" />
</children>
</AnchorPane>
</right>
</BorderPane>
这是我的控制器类:
package com.example.mall;
import javafx.application.Application;
import javafx.event.EventHandler;
import javafx.fxml.FXMLLoader;
import javafx.scene.Node;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.input.MouseEvent;
import javafx.stage.Stage;
import javafx.stage.StageStyle;
import javafx.scene.Parent;
import java.io.IOException;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.control.PasswordField;
import javafx.scene.control.TextField;
import java.util.HashMap;
import java.io.IOException;
import java.net.URL;
import java.util.ResourceBundle;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.PasswordField;
import javafx.scene.control.TextField;
public class HelloController {
private Stage stage;
private Scene scene;
private Parent root;
@FXML
private Label errorLabel;
@FXML
private Button loginButton;
@FXML
private PasswordField passwordField;
@FXML
private TextField usernameField;
@FXML
private void exit(ActionEvent event) {
System.exit(0);
}
@FXML
public void login(ActionEvent event) throws IOException {
HashMap<String,String> logininfo = new HashMap<String,String>();
logininfo.put("customer","customer123");
logininfo.put("staff","staff123");
logininfo.put("ceo","ceo123");
String username;
username = usernameField.getText();
System.out.println(username);
String password;
password = String.valueOf(passwordField.getText());
System.out.println(password);
if (event.getSource()==loginButton){
//if(logininfo.containsKey(username)){
System.out.println("Switching scenesaaaaaaa");
if(logininfo.get(username).equals(password)){
System.out.println("Switching scenes");
switchScene(event, "ShoppingInterface.fxml");
}
//}
}
}
public void switchScene(ActionEvent event,String fxmlFileName) throws IOException {
System.out.println("Switching to scene: " + fxmlFileName);
FXMLLoader loader = new FXMLLoader(getClass().getResource(fxmlFileName));
Scene newScene = new Scene(loader.load());
Stage primaryStage = (Stage) ((Node)event.getSource()).getScene().getWindow();
primaryStage.setScene(newScene);
primaryStage.show();
Parent root = FXMLLoader.load(getClass().getResource("Login.fxml"));
}
}
这是我的主文件:
package com.example.mall;
import javafx.application.Application;
import javafx.event.EventHandler;
import javafx.fxml.FXMLLoader;
import javafx.scene.Scene;
import javafx.scene.input.MouseEvent;
import javafx.stage.Stage;
import javafx.stage.StageStyle;
import javafx.scene.Parent;
import java.io.IOException;
public class UserLogin extends Application {
private double xOffset = 0;
private double yOffset = 0;
@Override
public void start(Stage stage) throws IOException {
Parent root = FXMLLoader.load(getClass().getResource("Login.fxml"));
Scene scene = new Scene(root);
stage.initStyle(StageStyle.DECORATED.UNDECORATED);
root.setOnMousePressed(new EventHandler<MouseEvent>() {
@Override
public void handle(MouseEvent event){
xOffset = event.getScreenX();
yOffset = event.getScreenY();
}
});
root.setOnMouseDragged(new EventHandler<MouseEvent>() {
@Override
public void handle(MouseEvent event){
stage.setX(event.getScreenX() - xOffset );
stage.setY(event.getScreenY() - yOffset );
}
});
stage.setScene(scene);
stage.show();
}
public static void main(String[] args) {
launch();
}
}
我已经确保我的 onAction 链接到正确的方法名称,我什至尝试在输入用户名和密码的整个过程中使用打印语句,但即使这些语句也不会在终端内打印。
未来读者的解决方案:
解决方案是将所有内容复制到一个新项目中。之后就成功了。错误是提问者本人造成的。
感谢@James_D 解决了这个问题。