簡體   English   中英

如何使用if else在TextField中設置var值

[英]How to use if else to set var value in TextField

我可以問我如何立即將名為txtUserName的TextField命名為'Aime',並將我的txtPassword等同於'Joy',以便它顯示消息“User Name and Password Match!” 任何人? 請幫我 :(

public void actionPerformed(ActionEvent e){
    if (e.getSource()== btnClear){
    txtUserName.setText("");
    }
    if(e.getSource() == btnLogin){
        if (txtUserName.setText="Aime" && txtPassword.setText="JOy")){
            JOptionPane.showMessageDialog(null, "User Name and Password Match!");
        }
        else {
            JOptionPane.showMessageDialog(null, "User Name and Password Invalid!");
        }

這些是需要的小修改。

if (txtUserName.getText().equals("Aime") && txtPassword.getText().equals("Joy")){
            JOptionPane.showMessageDialog(null, "User Name and Password Match!");
}else{
        JOptionPane.showMessageDialog(null, "User Name and Password Invalid!");
}

供您參考: Java中== vs equals()之間有什么區別?

你有兩個問題:

txtUserName.setText="Aime" && txtPassword.setText="JOy"

我猜您正在嘗試檢查用戶輸入,因此您需要使用gettext方法...

另一方面,你不能使用==來比較字符串,但是你輸錯了==而不是比較分配,無效的分配,因為你不能以這種方式設置那個視圖的文本......

試着改為

txtUserName.getText().toString().equals("Aime") && ...

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM