如果我没有输入任何详细信息并单击保存并继续按钮,标签 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);
}
}