0

I just started programming in java and i am creating a simple waiting list. it al seems to work well but i decided to include a if else construction to check the textfield not beeing empty. the problem is that it seems to be ignored because i don't get a error or something.. and i googled alot for the if else example and i can't solve the problem somehow.. what am i doing wrong? below you can find the relevant code. Thanks in advance.

public void actionPerformed(ActionEvent e) {

    // check if veld1 is filled in.
    if ( veld1 == null || veld1.equals( "" ) ) { 
        // give error
        System.out.println("U heeft niets ingevuld in veld1");
    }
    else {
        veld4.setText( veld3.getText() );
        veld3.setText( veld2.getText() );
        veld2.setText( veld1.getText() );
        textveld1.append( veld4.getText() + "\n" );
        veld1.setText("");
    }
}
4

4 回答 4

8

看来 veld1 不是字符串,而是一些 Swing 控件。你可能想做

if(veld1.getText() == null || veld1.getText().equals( "" )
于 2012-09-05T20:18:24.827 回答
1

如果veld1持有 JTextField,您可能希望将语句更改为veld1 == null || veld1.getText() == null || veld1.getText().equals( "" ),因为在您当前的代码中,您检查字段本身是否存在,而不是其内容。

于 2012-09-05T20:18:41.547 回答
1

很难在没有看到其余部分的情况下授予,但veld1.equals("")看起来很可疑。您正在与veld1空字符串进行比较,但veld1看起来像一个组件。也许你的意思是veld1.getText().equals("")(并且,同样,veld1.getText() == null

于 2012-09-05T20:19:21.303 回答
0

veld1.equals("") 和 veld1.getText().equals("") 是不一样的,第一个是将 veld1 对象与一个空字符串进行比较,会一直为 false。

于 2012-09-05T20:19:13.450 回答