2017-05-07 3 views
1

내가 무엇을 하려는지 이해했다고 생각합니다. 사용자가 기본 텍스트를 변경하지 않고 확인을 누르면 프롬프트가 표시됩니다. 그러나 첫 번째 조건은 TRUE로 보인다. if 문 또는 my code의 문제인지 나는 모른다. 전체 클래스를 포함시켰다. unnessasary 코드를 읽지 않도록 주석을 읽도록한다.else의 Java executuion이 필요하지만 항상 실행중인 경우

확인 = JButton의

텍스트 = JTextField를

int c = 0; 
public class handler implements ActionListener{ 
public void actionPerformed(ActionEvent e){ 
// 
//The following lines are not nessary for the question. 
if(ok==e.getSource()){ 
    if(!(male.isSelected() || female.isSelected()) && c==0){ 
     c++; 
     JOptionPane.showMessageDialog(null,"Hey you haven't selected your gender. Do you wish to proceed","Warning",JOptionPane.PLAIN_MESSAGE); 
    } 
} 
//well these lines were not necessary 
//They were just in the same class 
// 
// 
//Here the else condition should execute 
// 
if(ok==e.getSource()) && (text.getText() != "Enter your name")){ 
    JOptionPane.showMessageDialog(null,"Your name is "+text.getText(),"Name",JOptionPane.PLAIN_MESSAGE); 
}else if((ok==e.getSource()) && (text.getText() == "Enter your name")){ 
    JOptionPane.showMessageDialog(null,"Hey type in your name buddy ","Name",JOptionPane.PLAIN_MESSAGE); 
} 
} 
} 

그래서 문제는 우리가 기본 텍스트를 변경하지 않을 때해야하는 경우 "당신의 이름을 입력합니다"는 다른 상태의 경우 조건이 실행됩니다 있다는 것입니다 . 텍스트를 변경하려고 시도하고 if 블록도 실행 중입니다. 그리고 당신의 대답에 대한 완전한 설명을하십시오. 나는 녹슨 기술을 가진 초보자입니다.

text.getText() == "Enter your name") 

사용할 수 같음 :

text.getText().equals("Enter your name") 

당신은 그것을 원래 코드에 두 장소에서해야

답변

1

당신은 여기에서 예를 들어, 문자열을 비교하기 위해 다른 방법을 사용합니다.

+0

효과가있었습니다. 그러나 두 번째 옵션 만. 첫 번째와 두 번째 옵션을 사용할 때의 차이점을 설명해 주시겠습니까? 하지만이 형식 (text.getText()). equals ("사용자 이름 입력") 만 가능합니다. text.getText() 주위의 괄호를 제거하자 마자 다시 작동을 멈 춥니 다. –

관련 문제