import java.awt.*; import java.io.*; import java.awt.event.*; import java.applet.Applet; import java.util.StringTokenizer; public class Redaktor1 extends Applet implements ActionListener, TextListener{ TextArea ta=new TextArea(); TextArea taStatistika=new TextArea(5, 10); TextField tfFailiNimi=new TextField(); Button loe=new Button("Loe"); Button salvesta=new Button("Salvesta"); Panel nupupaneel=new Panel(new GridLayout(1, 2)); Panel alapaneel=new Panel(new GridLayout(2, 1)); public Redaktor1(){ setLayout(new BorderLayout()); nupupaneel.add(loe); nupupaneel.add(salvesta); alapaneel.add(nupupaneel); alapaneel.add(tfFailiNimi); add(ta, BorderLayout.CENTER); add(taStatistika, BorderLayout.EAST); add(alapaneel, BorderLayout.SOUTH); salvesta.addActionListener(this); loe.addActionListener(this); ta.addTextListener(this); } void arvutaStatistika(){ String s=ta.getText(); taStatistika.setText(""); taStatistika.append("Pikkus: "+s.length()+"\n"); StringTokenizer st=new StringTokenizer(s, " \n"); int loendur=0; while(st.hasMoreTokens()){ st.nextToken(); loendur++; } taStatistika.append("Sõnu: "+loendur+"\n"); taStatistika.append("Ridu: "+ new StringTokenizer(s, "\n").countTokens()+"\n"); taStatistika.append("Ridu: "+ new StringTokenizer(s, ".?!").countTokens()+"\n"); } public void textValueChanged(TextEvent e){ arvutaStatistika(); } public void actionPerformed(ActionEvent e){ if(e.getSource()==salvesta){ try{ PrintWriter faili=new PrintWriter( new FileWriter(tfFailiNimi.getText()) ); faili.println(ta.getText()); faili.close(); }catch(Exception viga){ System.out.println("probleem kirjutamisel"); } } if(e.getSource()==loe){ try{ BufferedReader failist=new BufferedReader( new FileReader(tfFailiNimi.getText()) ); String rida=failist.readLine(); while(rida!=null){ ta.append(rida+"\n"); rida=failist.readLine(); } failist.close(); }catch(Exception viga){} } } public static void main(String[] argumendid){ Frame f=new Frame(); f.setSize(300, 300); f.add(new Redaktor1()); f.setVisible(true); f.addWindowListener(new WindowAdapter(){ public void windowClosing(WindowEvent e){ System.exit(0); } }); } }