/** * save the data to the specified file * */ private void save(){ try{ PrintWriter pw = new PrintWriter(new FileWriter(new File(FILE_NAME))); pw.write(str.getText()+"\n"); pw.write(inter.getText()+"\n"); pw.close(); saveOK("saving complete!"); }catch(Exception e){ warn(e.toString()); } }
/** * print the content of the specified file to the textArea * */ private void print(){ try{ BufferedReader br = new BufferedReader(new FileReader(FILE_NAME)); String tmp; info.setText(""); info.setForeground(Color.BLACK); while((tmp=br.readLine())!=null) info.append(tmp+"\n");
/** * test if the integer is ok * @return true if is an integer,false otherwise */ private boolean verify(){ String tmp = inter.getText(); if(tmp==null || tmp.length()==0){ warn("incorrect integer!"); return false; } else{ try{ Integer.parseInt(tmp); }catch(Exception e){ warn("not an integer!"); return false; } }
return true; }
/** * print the warning information to the info TextArea * @param str the information to print */ private void warn(String str){ info.setForeground(Color.RED); info.setText(str); }
/** * if the data saved successfully,print the information * @param str the information to print */ private void saveOK(String str){ info.setForeground(Color.BLUE); info.setText(str); }
/** * main method * @param args */ public static void main(String args[]){ new SaveData().setVisible(true); } }
import java.awt.event.*;
import java.io.*;
import java.awt.*;public class SaveData extends JFrame implements ActionListener{
private JTextField str,inter;
private JButton save,print;
private JTextArea info;
private static final String FILE_NAME = "SaveData.txt";
/**
* the constructor,initialize the layout
*/
public SaveData(){
str = new JTextField(20);
inter = new JTextField(20);
save = new JButton("save");
print = new JButton("print");
info = new JTextArea(6,30);
save.addActionListener(this);
print.addActionListener(this);
JPanel p1 = new JPanel();
p1.add(new JLabel("string:"));
p1.add(str);
JPanel p2 = new JPanel();
p2.add(new JLabel("integer:"));
p2.add(inter);
JPanel p3 = new JPanel();
p3.add(save);
p3.add(print);
Container c = this.getContentPane();
c.setLayout(new FlowLayout());
c.add(p1);
c.add(p2);
c.add(p3);
c.add(new JScrollPane(info));
this.setSize(new Dimension(400,300));
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public void actionPerformed(ActionEvent e){
if(e.getSource() == save){
if(verify())
save();
}
else if(e.getSource() == print){
print();
}
}
/**
* save the data to the specified file
*
*/
private void save(){
try{
PrintWriter pw = new PrintWriter(new FileWriter(new File(FILE_NAME)));
pw.write(str.getText()+"\n");
pw.write(inter.getText()+"\n");
pw.close();
saveOK("saving complete!");
}catch(Exception e){
warn(e.toString());
}
}
/**
* print the content of the specified file to the textArea
*
*/
private void print(){
try{
BufferedReader br = new BufferedReader(new FileReader(FILE_NAME));
String tmp;
info.setText("");
info.setForeground(Color.BLACK);
while((tmp=br.readLine())!=null)
info.append(tmp+"\n");
br.close();
}catch(IOException ioe){
warn(ioe.toString());
}
}
/**
* test if the integer is ok
* @return true if is an integer,false otherwise
*/
private boolean verify(){
String tmp = inter.getText();
if(tmp==null || tmp.length()==0){
warn("incorrect integer!");
return false;
}
else{
try{
Integer.parseInt(tmp);
}catch(Exception e){
warn("not an integer!");
return false;
}
}
return true;
}
/**
* print the warning information to the info TextArea
* @param str the information to print
*/
private void warn(String str){
info.setForeground(Color.RED);
info.setText(str);
}
/**
* if the data saved successfully,print the information
* @param str the information to print
*/
private void saveOK(String str){
info.setForeground(Color.BLUE);
info.setText(str);
}
/**
* main method
* @param args
*/
public static void main(String args[]){
new SaveData().setVisible(true);
}
}