java - 在 JavaFX 中使用 If/Else 语句

标签 java javafx

我尝试在 Java FX 程序中使用 if/else 语句,但即使满足“if”语句,它也会不断向我提供“else”输出。我检查了我的 onaction 事件和我的 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>

最佳答案

使用username.equals()password.equals()代替

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

这将给出 TextArea 的字符串值

关于java - 在 JavaFX 中使用 If/Else 语句,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41906165/

相关文章:

java - 在 JSF - 尝试实现类似于 "#{bean.run(3)}"的东西

java - import javafx 无法解析

javaFX MediaPlayer getCurrentTime() 不可靠

javafx fxml 组合框错误

调整大小时显示 JavaFX TextArea 滚动条,即使不需要它

JavaFX - 有没有一种方法可以使用 imageview 多次打印一张图像?

java - 为什么我的自定义 JavaFX MapView 请求通过 OSM 获得 403 错误?

java - ImageView 导致延迟

java - 如何在 Eclipse Indigo 中添加 jdk8

java - Sikuli 无法通过 Jenkins 在屏幕上找到图像