import java.awt.*; import java.awt.event.*; import java.io.*; public class Muistio { public static void main(String[] args) { Lomake1 viite = new Lomake1("Muistio"); } } class Lomake1 extends Frame implements ActionListener, WindowListener { TextField tiedosto; TextArea alue; String leikepoyta; Button uusi,avaa,tallenna,leikkaa,kopioi,liita; Lomake1(String otsikko) { super(otsikko); setSize(400,300); setLayout(new FlowLayout()); add(uusi = new Button("Uusi")); add(avaa = new Button("Avaa")); add(tallenna = new Button("Tallenna")); add(leikkaa = new Button("Leikkaa")); add(kopioi = new Button("Kopioi")); add(liita = new Button("Liitä")); add(tiedosto = new TextField(40)); add(alue = new TextArea(10,40)); uusi.addActionListener(this); avaa.addActionListener(this); tallenna.addActionListener(this); kopioi.addActionListener(this); liita.addActionListener(this); addWindowListener(this); setVisible(true); } public void windowClosing(WindowEvent e) { System.exit(0); } public void windowClosed(WindowEvent e) {} public void windowDeiconified(WindowEvent e) {} public void windowIconified(WindowEvent e) {} public void windowActivated(WindowEvent e) {} public void windowDeactivated(WindowEvent e) {} public void windowOpened(WindowEvent e) {} public void actionPerformed(ActionEvent e) { if (e.getActionCommand().equals("Uusi")) { alue.selectAll(); alue.replaceText("",alue.getSelectionStart(),alue.getSelectionEnd()); } if (e.getActionCommand().equals("Avaa")) { FileReader filer = null; try { filer = new FileReader(tiedosto.getText()); } catch(IOException f){ tiedosto.setText("Virhe luotaessa lukuvirtaa"); } BufferedReader buffr = new BufferedReader(filer); try { String rivir = null; do { rivir = buffr.readLine(); if (rivir != null){ alue.appendText(rivir); alue.appendText("\n"); } } while (rivir != null); buffr.close(); } catch(IOException f){ tiedosto.setText("Virhe luotaessa lukuvirtaa"); } } if (e.getActionCommand().equals("Tallenna")) { FileWriter filew = null; try { filew = new FileWriter(tiedosto.getText()); } catch(IOException f){ tiedosto.setText("Virhe luotaessa tulostusvirtaa"); } BufferedWriter buffw = new BufferedWriter(filew); alue.selectAll(); String riviw =alue.getSelectedText(); try { buffw.write(riviw); buffw.close(); } catch(IOException f){ tiedosto.setText("Virhe luotaessa tulostusvirtaa"); } } if (e.getActionCommand().equals("Leikkaa")) { leikepoyta = alue.getSelectedText(); alue.replaceText("",alue.getSelectionStart(),alue.getSelectionEnd()); } if (e.getActionCommand().equals("Kopioi")) { leikepoyta = alue.getSelectedText(); } if (e.getActionCommand().equals("Liitä")) { alue.replaceText(leikepoyta,alue.getSelectionStart(),alue.getSelectionEnd()); } } }