Пример: студенти и оценки

Клас студент - име, оценка и преобразуване до низ  от символи:

import java.io.*;
class Student implements Serializable{
    private static final long serialVersionUID = 1L;
    String name; int note;
    Student(String name, int note){this.name = name;this.note = note;}
    public String toString(){
        return name+"   "+note;
    }
}


Версия 1 (  Apl ): Създаване на списък от студенти и оценки:

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.ArrayList;

public class Apl extends JFrame{
    private static final long serialVersionUID = 1L;
    protected JButton ad;
    protected JTextField tf[]= new JTextField[2];
    protected JTextArea slist;

    protected JPanel contr,plst;
    

    protected AdSt adSt;
    protected ArrayList<Student> prs=new ArrayList<Student>(10);

    Apl(int x, int y, int ln, int ht){
        this.setLayout(new BorderLayout());
        this.setBounds(x, y, ln, ht);
        this.setVisible(true);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setTitle("Student's directory - version 1");
        slist = new JTextArea(5, 10);
        slist.setEditable(false);

        contr = new JPanel(new FlowLayout());
        plst = new JPanel(new FlowLayout());

        ad = new JButton("add");
        contr.add(ad);
        tf[0]= new JTextField("name?",10);
        tf[1]= new JTextField("note?",3);
        contr.add(tf[0]);
        contr.add(tf[1]);
        ad.addActionListener(adSt=new AdSt());
        tf[1].addActionListener(adSt);
        plst.add(new JScrollPane(slist));
        add("North",contr);
        add("Center",plst);
        revalidate();
    }
    class AdSt implements ActionListener{
        public void actionPerformed(ActionEvent e ){
            Student s;
            int nt;
            String n=tf[0].getText();
            try{
                nt=Integer.parseInt(tf[1].getText());
            }
            catch(NumberFormatException ex){
                tf[1].setText("note?");
                return;
            }
            prs.add(s=new Student(n,nt));
            tf[0].setText("name?");tf[1].setText("note?");
            slist.append(s+"\n");
            slist.setCaretPosition(slist.getDocument().getLength());
            
            revalidate();
        }
    }
    public static void main(String [] arg){
        new Apl(20,20,400,300);
    }
}


Версия 2
(  Apl1 ): Наследява функционалността на версия 1 и добавя съхраняване във файл и четене от файл:

import javax.swing.*;
import java.awt.event.*;
import java.io.*;
import java.util.ArrayList;

public class Apl1 extends Apl{
    private static final long serialVersionUID = 1L;
    protected JButton load,save;  
    Apl1(){
        super(20,20,400,250);
        load = new JButton("load");
        load.addActionListener(new Load());
        contr.add(load);
        save = new JButton("save");
        save.addActionListener(new Save());
        contr.add(save);
        setTitle("Student's directory - version 2");
        revalidate();
    }
    public static void main(String arg[]){
        new Apl1();     
    }
    class Save implements ActionListener  {
        public void actionPerformed(ActionEvent e ){
            ObjectOutputStream oos = null;
            try{
                oos = new ObjectOutputStream (
                    new FileOutputStream ("save.ser"));
                oos.writeObject(prs);
                tf[0].setText("saved");
            }
            catch (IOException ex){
                System.out.println(ex);
            }
            try{
                if(oos!=null)oos.close();
            }
            catch (IOException ex){}
        }
    }
    
    class Load implements ActionListener  {
        @SuppressWarnings("unchecked")               
        public void actionPerformed(ActionEvent e ){
            ObjectInputStream ios = null;
            try{
                ios = new ObjectInputStream (new FileInputStream ("save.ser"));             
                prs= (ArrayList<Student>)ios.readObject();
            }
            catch (Exception ex){
                tf[0].setText("Error in save.ser");
            }
            try{
                if(ios!=null)ios.close();
            }
            catch (IOException ex){}
            slist.setText("");
            for(Student s : prs){
                 slist.append(s+"\n");
            }
            slist.setCaretPosition(slist.getDocument().getLength());
        }       
    }
}



Версия 3 (  Apl2 ): Наследява функционалността на версия две и добавя изчистване на полетата при подготовка за нов запис:

import java.awt.event.*;
public class Apl2 extends Apl1{
    private static final long serialVersionUID = 1L;
    public static void main(String[] args) {
        new Apl2();            
    }
    Apl2(){
        tf[0].addFocusListener(new InName());
        tf[1].addFocusListener(new InNote());
        this.setTitle("Student's directory - version 3");
    }
    class InName implements FocusListener{        
        public void focusGained(FocusEvent e) {
            if (tf[0].getText().equals("name?"))
                tf[0].setText("");
        }
        public void focusLost(FocusEvent e){
            if (tf[0].getText().equals(""))
                tf[0].setText("name?");            
        }        
    }
    class InNote implements FocusListener{        
        public void focusGained(FocusEvent e) {
            if (tf[1].getText().equals("note?"))
                tf[1].setText("");
        }
        public void focusLost(FocusEvent e){
            if (tf[1].getText().equals(""))
                tf[1].setText("note?");            
        }
    }
}