在JavaFX中使用If / Else语句

时间:2017-01-28 03:56:36

标签: java javafx

我正在尝试在我的Java FX程序中使用if / else语句,但即使满足“if”语句,它仍然会给我“else”输出。我检查了我的事件和我的fx id只是为了确保它们是对应的。

package sample;

import javafx.fxml.FXML;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;



public class Controller {
    @FXML
    private Label user;
    @FXML
    private Label pass;
    @FXML
    public Button button;
    @FXML
    public TextField username;
    @FXML
    public TextField password;
    @FXML
    public void doSomething(){
        if ((username.equals("John")) && (password.equals("Doe"))) {
            System.out.println("Welcome");
        } else{
            System.out.println("Please Try again");
        }
    }
}
<?import javafx.scene.layout.GridPane?>

<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.control.TextField?>

<GridPane fx:controller="sample.Controller"
          xmlns:fx="http://javafx.com/fxml" alignment="center" hgap="20" vgap="10">
    <Label text="Username" GridPane.columnIndex="0" GridPane.rowIndex="0" fx:id="user"/><TextField GridPane.columnIndex="0" GridPane.rowIndex="1" fx:id="username"/>
    <Label text="Password" GridPane.columnIndex="1" GridPane.rowIndex="0" fx:id="pass"/><TextField GridPane.columnIndex="1" GridPane.rowIndex="1" fx:id="password"/>
    <Button text="Login" GridPane.columnIndex="2" GridPane.rowIndex="1" GridPane.columnSpan="2" fx:id="button" onAction="#doSomething"/>
</GridPane>

1 个答案:

答案 0 :(得分:2)

而不是username.equals()password.equals()使用

username.getText().equals()password.getText().equals()

将给出TextArea的字符串值

相关问题