import java.awt.*; import java.awt.event.*; //sündmused import java.applet.Applet; //aken import java.io.*; // failide jaox import java.util.*; public class Redaktor1 extends Applet implements ActionListener //et kuulax sõnumeid { TextArea ta = new TextArea(); Button salvestusnupp = new Button("Salvesta"); Button lugemisnupp = new Button("Loe"); Button statistika = new Button("Statistika"); Panel nupupaneel = new Panel(new GridLayout(1,2)); // 1 rida, 2 veergu Panel alapaneel = new Panel(new GridLayout(2,1)); TextField textivali = new TextField(""); public Redaktor1() //konstruktor, asjad ekraanile { setLayout(new BorderLayout()); //servadesse ja keskele paigutamisex nupupaneel.add(lugemisnupp); nupupaneel.add(salvestusnupp); nupupaneel.add(statistika); alapaneel.add(textivali); alapaneel.add(nupupaneel); add(alapaneel, BorderLayout.NORTH); // paneel koos nuppudega add(ta, BorderLayout.CENTER); //textivälja paneme salvestusnupp.addActionListener(this);//et kuulax lugemisnupp.addActionListener(this); statistika.addActionListener(this); } public void actionPerformed(ActionEvent e) { try { if(e.getSource()==statistika){ int summa=0; String s=ta.getText(); StringTokenizer stk=new StringTokenizer(s); while(stk.hasMoreTokens()){ try{ int arv=Integer.parseInt(stk.nextToken()); summa+=arv; } catch (Exception viga){ta.setText("Probleem: "+ viga);} } //int tahed=0; //String s=ta.getText(); //tahed=s.length(); //if(e.getSource()==statistika){ PrintWriter faili = new PrintWriter (new FileWriter("statistika.txt")); faili.print("Pikkus: "+summa); faili.close(); } if (e.getSource()==salvestusnupp) //salvestamine { PrintWriter faili = new PrintWriter (new FileWriter(textivali.getText())); faili.print(ta.getText()); faili.close(); } if (e.getSource()==lugemisnupp) { long failiPikkus = new File(textivali.getText()).length(); //faili pikkus char andmed[] = new char[(int)failiPikkus]; //teeme failipikkuse massiivi FileReader failist = new FileReader(textivali.getText()); // faili lugemine failist.read(andmed, 0, andmed.length); //loeme siis andmed ta.setText(new String(andmed)); } } catch(IOException ioe){ta.setText("Probleem: "+ ioe);} } public static void main(String[] argumendid) { Frame f = new Frame("Textiredaktor"); f.add(new Redaktor1()); f.setSize(300,300); f.setVisible(true); } };