2016-05-07 7 views
0

EDIT:答えのためのjnsのおかげで、簡単な間違いです。 .equals()に文字列をチェックする必要があります。javafxログインユーザーインターフェイスのテキストが認識されません

私はjavafx/scenebuilderを使って簡単なログインGUIを作って、usernameフィールドとpasswordフィールドでユーザー入力を読むことに問題があります。私が起こっているのは、handleLoginメソッドが3つの異なるアクション、1つは正しい資格情報であるuser = "test"とpass = "test1"を考慮に入れようとすることです。次に、フィールドが空白のままである場合、警告アラートが表示されます。最後は、エラーアラートを発行するために入力されたものです。現在、フィールドに何があっても、ログインボタンを押すと、エラーアラートのみが表示されます。 MainApp、LoginOverview、LoginOverviewControllerという3つのファイルがあります。 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> 

すべてのヘルプは高く評価され、感謝!

答えて

0

あなたは同じ参照同じではない値のためにテストしている:

if (usernameField.getText() == "test" && passwordField.getText() == "test1") 

使用すると、代わりに等しい:

if ("test".equals(usernameField.getText()) && "test1".equals(passwordField.getText())) 
+0

ありがとうございました!このような明白な間違い – programStudent

関連する問題