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 { MenuBar valikkopalkki; Menu valikko1, valikko2; TextField tiedosto; TextArea alue; String leikepoyta; Button uusi,avaa,tallenna,leikkaa,kopioi,liita; Lomake1(String otsikko) { super(otsikko); setSize(400,400); setLayout(new FlowLayout()); valikkopalkki = new MenuBar(); valikkopalkki.add(valikko1 = new Menu("Tiedosto")); valikkopalkki.add(valikko2 = new Menu("Muokkaa")); valikko1.addActionListener(this); valikko2.addActionListener(this); valikko1.add(new MenuItem("Uusi")); valikko1.add(new MenuItem("Avaa")); valikko1.add(new MenuItem("Tallenna")); valikko1.add(new MenuItem("Lopeta")); valikko2.add(new MenuItem("Leikkaa")); valikko2.add(new MenuItem("Kopioi")); valikko2.add(new MenuItem("Liitä")); /*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)); tiedosto.setText("koira.txt"); /*uusi.addActionListener(this); avaa.addActionListener(this); tallenna.addActionListener(this); leikkaa.addActionListener(this); kopioi.addActionListener(this); liita.addActionListener(this); addWindowListener(this);*/ setMenuBar(valikkopalkki); 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); try { buffw.write(alue.getText()); buffw.close(); } catch(IOException f){ tiedosto.setText("Virhe luotaessa tulostusvirtaa"); } } if (e.getActionCommand().equals("Lopeta")) { System.exit(0); } 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()); } } } /*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("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; rivir = buffr.read(); alue.appendText(rivir); buffr.close(); } catch(IOException f){ tiedosto.setText("Virhe luotaessa lukuvirtaa"); } } */