EDIT: Danke an jns für die Antwort, einfacher Fehler. Hab dir .equals() um eine Zeichenkette zu prüfen.Text von meinem javafx Login Benutzerschnittstelle wird nicht erkannt
also habe ich eine einfache Login-GUI mit javafx/scenebuilder gemacht und habe ein Problem mit dem Lesen der Benutzereingabe im Feld Benutzername und Passwort. Was ich versuche zu geschehen, ist, dass die Methode handleLogin versucht, drei verschiedene Aktionen zu berücksichtigen. Eine davon sind die korrekten Anmeldeinformationen, user = "test" und pass = "test1". Der nächste ist, wenn die Felder leer bleiben, sollte eine Warnmeldung angezeigt werden. Der letzte ist für alles andere, das eingegeben wird, um eine Fehlerwarnung auszugeben. Derzeit, egal, was in den Feldern ist, beim Drücken der Login-Taste, nur die Fehlermeldung angezeigt. Es gibt insgesamt drei Dateien namens MainApp, LoginOverview und LoginOverviewController. Here is a screenshot for what is currently shown
MainApp.java
package main.launch;
import java.io.IOException;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Scene;
import javafx.scene.layout.AnchorPane;
import javafx.stage.Stage;
import main.launch.view.LoginOverviewController;
public class MainApp extends Application {
public Stage primaryStage;
/**
* Constructor
*/
public MainApp() {
}
@Override
public void start(Stage primaryStage) {
this.primaryStage = primaryStage;
this.primaryStage.setTitle("TroubleTicketSystem");
showLoginOverview();
}
/**
* Shows the person overview inside the root layout.
*/
public void showLoginOverview() {
try {
// Load login overview.
FXMLLoader loader = new FXMLLoader();
loader.setLocation(MainApp.class.getResource("view/LoginOverview.fxml"));
AnchorPane loginOverview = (AnchorPane) loader.load();
Scene scene = new Scene(loginOverview);
primaryStage.setScene(scene);
primaryStage.show();
// Give the controller access to the main app.
LoginOverviewController controller = loader.getController();
controller.setMainApp(this);
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* Returns the main stage.
* @return
*/
public Stage getPrimaryStage() {
return primaryStage;
}
public static void main(String[] args) {
launch(args);
}
}
LoginOverviewController.java
package main.launch.view;
import javafx.fxml.FXML;
import javafx.scene.control.Alert;
import javafx.scene.control.PasswordField;
import javafx.scene.control.TextField;
import javafx.scene.control.Alert.AlertType;
import main.launch.MainApp;
public class LoginOverviewController{
@FXML
private TextField usernameField;
@FXML
private PasswordField passwordField;
// Reference to the main application.
private MainApp mainApp;
/**
* The constructor.
* The constructor is called before the initialize() method.
*/
public LoginOverviewController() {
}
/**
* Called when the user clicks the login button.
*
*/
@FXML
private void handleLogin(){
if (usernameField.getText() == "test" && passwordField.getText() == "test1")
{
Alert alert = new Alert(AlertType.INFORMATION);
alert.initOwner(mainApp.getPrimaryStage());
alert.setTitle("Login");
alert.setHeaderText("Successfull Login");
alert.setContentText("You are now logged in, testuser!");
alert.showAndWait();
}
else if (usernameField.getText() == "" || passwordField.getText() == ""){
Alert alert = new Alert(AlertType.WARNING);
alert.initOwner(mainApp.getPrimaryStage());
alert.setTitle("No information");
alert.setHeaderText("Missing credentials");
alert.setContentText("Please enter in your username and password.");
alert.showAndWait();
}
else {
Alert alert = new Alert(AlertType.ERROR);
alert.initOwner(mainApp.getPrimaryStage());
alert.setTitle("Error");
alert.setHeaderText("Wrong information");
alert.setContentText("Please try again.");
alert.showAndWait();
}
}
/**
* Is called by the main application to give a reference back to itself.
*
* @param mainApp
*/
public void setMainApp(MainApp mainApp) {
this.mainApp = mainApp;
}
}
LoginOverview.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.text.Font?>
<AnchorPane prefHeight="400.0" prefWidth="500.0" xmlns="http://javafx.com/javafx/8.0.65" xmlns:fx="http://javafx.com/fxml/1" fx:controller="main.launch.view.LoginOverviewController">
<children>
<Button layoutX="188.0" layoutY="301.0" mnemonicParsing="false" onAction="#handleLogin" prefHeight="50.0" prefWidth="125.0" text="Login" />
<Label layoutX="126.0" layoutY="153.0" prefHeight="32.0" prefWidth="70.0" text="Username" />
<Label layoutX="126.0" layoutY="191.0" prefHeight="32.0" prefWidth="62.0" text="Password" />
<TextField fx:id="usernameField" layoutX="197.0" layoutY="158.0" />
<Label layoutX="87.0" layoutY="77.0" text="Trouble Ticket System" textFill="#0d00ff">
<font>
<Font size="36.0" />
</font>
</Label>
<PasswordField fx:id="passwordField" layoutX="197.0" layoutY="196.0" />
</children>
</AnchorPane>
Jede Hilfe ist willkommen, danke!
Thank you! so ein offensichtlicher Fehler – programStudent