import java.awt.*; import javax.swing.*; import javax.swing.event.*; import java.io.*; import java.applet.Applet; import java.util.StringTokenizer; import java.awt.event.*; import java.awt.datatransfer.*; public class ver4 extends Applet implements ActionListener, TextListener{ TextArea ta=new TextArea(); TextArea ts=new TextArea(); TextArea stat=new TextArea(100, 5); Button avafail=new Button("ava fail"); Button salvestafail=new Button("salvesta fail"); Button kopeeri=new Button("kopeeri"); Button aseta=new Button("aseta"); Button puhasta=new Button("puhasta"); Panel nupupaneel=new Panel(new GridLayout(1, 5)); Panel alapaneel=new Panel(new GridLayout(1, 1)); Clipboard malu = getToolkit().getSystemClipboard(); JFileChooser valija=new JFileChooser(new File(".")); JTextField sfnimi=new JTextField(); //JPanel p=new JPanel(new GridLayout(2, 1)); JTabbedPane paneel=new JTabbedPane(); public ver4(){ setLayout(new BorderLayout()); add(paneel, BorderLayout.CENTER); paneel.add("Puhtand", ta); add(alapaneel, BorderLayout.NORTH); add(stat, BorderLayout.EAST); alapaneel.add(nupupaneel); nupupaneel.add(avafail); nupupaneel.add(salvestafail); nupupaneel.add(kopeeri); nupupaneel.add(aseta); nupupaneel.add(puhasta); avafail.addActionListener(this); salvestafail.addActionListener(this); kopeeri.addActionListener(this); aseta.addActionListener(this); puhasta.addActionListener(this); ta.addTextListener(this); paneel.addChangeListener(new SListener()); } public void textValueChanged(TextEvent e){ arvutaStatistika(); } void arvutaStatistika(){ int a=0; //statistika String s=ta.getText(); StringTokenizer sonu = new StringTokenizer(s, " \n"); while (sonu.hasMoreTokens()){ a++; sonu.nextToken(); } stat.setText(""); stat.append("P: "+s.length()+"\n"); stat.append("S: "+a+"\n"); stat.append("R: "+new StringTokenizer(s, "\n").countTokens()+"\n"); stat.append("L: "+new StringTokenizer(s, ".?!").countTokens()+"\n"); } public void actionPerformed(ActionEvent e){ if(e.getSource()==avafail){ //faili avamine valija.showOpenDialog(new JFrame()); try{ BufferedReader failist=new BufferedReader(new FileReader(valija.getSelectedFile())); String rida=failist.readLine(); paneel.add("Mustand", ts); while(rida!=null){ ts.append(rida+"\n"); rida=failist.readLine(); } failist.close(); }catch(Exception viga){} } if(e.getSource()==salvestafail){ //salvestamine sfnimi.setText(JOptionPane.showInputDialog("Mis nimi failile anda?")); try{ PrintWriter faili=new PrintWriter(new FileWriter(sfnimi.getText())); faili.println(ta.getText()); faili.close(); }catch(Exception viga){ //System.out.println("probleem kirjutamisel"); } } if(e.getSource()==kopeeri){ //copy StringSelection data = new StringSelection(ta.getText()); malu.setContents(data, data); } if(e.getSource()==aseta){ //paste Transferable andmed = malu.getContents(this); String s; try { s = (String)(andmed.getTransferData(DataFlavor.stringFlavor)); } catch (Exception viga) { s = viga.getMessage(); } ta.setText(s); } if(e.getSource()==puhasta){ //clear ta.setText(""); } } public static void main(String[] argumendid){ Frame f=new Frame(); f.setSize(500, 500); f.add(new ver4()); f.setVisible(true); f.addWindowListener(new WindowAdapter(){ public void windowClosing(WindowEvent e){ System.exit(0); } }); } class SListener implements ChangeListener{ public void stateChanged(ChangeEvent l){ int source=paneel.getSelectedIndex(); System.out.println(""+source); } } }