2013-11-04 107 views
0

,如果我不输入任何信息,然后点击保存并继续按钮,标签L4应更改为“不记录中输入” ...但它不会发生这样的,当我查看文件里面有一些数据。 所以这里的代码插入文件

import java.io.*; 
import java.awt.*; 
import java.awt.event.*; 
import java.util.*; 
class frameExe extends Frame implements ActionListener 
{ 
FileOutputStream fos=new FileOutputStream("student.txt"); 
PrintStream p=new PrintStream(fos); 
FileReader fr=new FileReader("student.txt"); 
BufferedReader br= new BufferedReader(fr); 
int m1,m2; 
Label l1,l2,l3,l4; 
TextField t1,t2,t3; 
TextArea ta1; 
Button b1,b2,b3; 
int total; 
int avg; 
String lines,details=""; 
public frameExe() throws Exception 
{ 
    setLayout(new FlowLayout()); 
    l1= new Label("name"); 
    l2= new Label(" mark1"); 
    l3= new Label("mark2"); 
    l4= new Label("details"); 
    t1=new TextField(20); 
    t2=new TextField(5); 
    t3=new TextField(5); 
    ta1=new TextArea(20,60); 
    b1=new Button("save & calculate the total"); 
    b2=new Button("clear"); 
    b3=new Button("Exit"); 
    add(l1); 
    add(t1); 
    add(l2); 
    add(t2); 
    add(l3); 
    add(t3); 
    add(b1); 
    add(b2); 
    add(b3); 
    add(l4); 
    add(ta1); 
    b1.addActionListener(this); 
    b2.addActionListener(this); 
    b3.addActionListener(this); 
    setVisible(true); 
} 
public void actionPerformed(ActionEvent ae) 
{ 
    if(ae.getSource()==b1)//save and continue button 
    { 
     if(((t1.getText())==null)||((t1.getText())=="")||((t1.getText())==" ")) 
     l4.setText("no record entered");// this is not working 
else 
{ 
     p.print("name = "); 
     p.print(t1.getText()); 
     p.print(" mark1 = "); 
     p.print(t2.getText()); 
     p.print(" mark2 = "); 
     p.print(t3.getText()); 
     m1=Integer.parseInt(t2.getText()); 
     m2=Integer.parseInt(t3.getText()); 
     total=m1+m2; 
     avg=total/2; 
     p.print(" total = "); 
     p.print(total); 
     p.print(" average = "); 
     p.println(avg); 
     t1.setText(""); 
     t2.setText(""); 
     t3.setText(""); 
     try 
     { 
      while((lines=br.readLine())!=null) 
      { 
       details+=lines; 
       details+='\n'; 
      } 
      ta1.setText(details); 
     } 
     catch(Exception e) 
     { 
      System.out.println("error "+e); 
     } 
}//else block ending 
    } 
    if(ae.getSource()==b2)//clear button 
    { 
     t1.setText(""); 
     t2.setText(""); 
     t3.setText(""); 
    } 
    if(ae.getSource()==b3)// exit button 
    { 
     frameExe.this.dispose(); 
    } 
} 
public static void main(String s[])throws Exception 
{ 
    frameExe f= new frameExe(); 
    f.setSize(400,200); 
    f.setVisible(true); 
} 
} 

回答

0

你的代码中的一个小错误。必须像

if(t1.getText().isEmpty()||t2.getText().isEmpty()||t3.getText().isEmpty()) { 
    l4.setText("no record entered"); 
} 

t1.getText()从未返回null,因为

的Java。AWT。Choice上#的getText()
返回 由该文本组件表示的文本。默认情况下,这是 一个空字符串。

您必须阅读Code Conventions for the Java Programming Language

0

您需要使用t1.getText())。等于( “”),而不是t1.getText())== “”