import java.awt.*; import java.awt.event.*; import java.io.*; public class Muistio { public static void main(String[] args) { Lomake Sovellus = new Lomake("Muistio"); } } class Lomake extends Frame implements ActionListener, WindowListener { TextArea alue; TextField tiedosto; String leikepoyta; Button uusi, avaa, tallenna, leikkaa, kopioi, liita; Lomake(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); leikkaa.addActionListener(this); kopioi.addActionListener(this); liita.addActionListener(this); tallenna.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("Leikkaa")) { leikepoyta = alue.getSelectedText(); //tiedosto.setText(leikepoyta); alue.replaceText("", alue.getSelectionStart(), alue.getSelectionEnd()); } if (e.getActionCommand().equals("Kopioi")) { leikepoyta = alue.getSelectedText(); //tiedosto.setText(leikepoyta); } if (e.getActionCommand().equals("Liitä")) { alue.replaceText(leikepoyta, alue.getSelectionStart(), alue.getSelectionEnd()); } if (e.getActionCommand().equals("Avaa")) { FileReader filer = null; try { filer = new FileReader(tiedosto.getText()); } catch ( IOException ex ) { tiedosto.setText("Virhe"); } BufferedReader buffr = new BufferedReader(filer); try{ String rivi = null; do { rivi = buffr.readLine(); alue.appendText(rivi); alue.appendText("\n"); } while ( rivi != null); buffr.close(); } catch ( IOException ex ) { tiedosto.setText("Virhe"); } } if (e.getActionCommand().equals("Tallenna")) { FileWriter filew = null; try { filew = new FileWriter(tiedosto.getText()); } catch ( IOException ex ) { tiedosto.setText("Virhe"); } BufferedWriter buffw = new BufferedWriter(filew); try { buffw.write(alue.getText()); buffw.close(); } catch ( IOException ex ) { tiedosto.setText("Virhe"); } } } }