import java.io.*; import java.awt.*; import javax.swing.*; import java.awt.event.*; public class Redaktor1 extends JPanel implements ActionListener{ JTextArea sisu=new JTextArea(); JMenuBar riba=new JMenuBar(); JMenu fmenyy=new JMenu("Fail"); JMenuItem uus=new JMenuItem("Uus"); JMenuItem ava=new JMenuItem("Ava"); JMenuItem salvesta=new JMenuItem("Salvesta"); JMenuItem salvestanimega=new JMenuItem("Salvesta nimega"); JFileChooser d=new JFileChooser(new File(".")); File f; JFrame kest; public Redaktor1(JFrame kest){ kest.setJMenuBar(riba); riba.add(fmenyy); fmenyy.add(uus); fmenyy.add(ava); fmenyy.add(salvesta); fmenyy.add(salvestanimega); uus.addActionListener(this); ava.addActionListener(this); salvesta.addActionListener(this); salvestanimega.addActionListener(this); setLayout(new BorderLayout()); add(new JScrollPane(sisu)); this.kest=kest; } public void actionPerformed(ActionEvent e){ try{ if(e.getSource()==uus){ f=null; sisu.setText(""); kest.setTitle("Uus fail"); } if(e.getSource()==ava){ kysiFailiNimi(); char[] puhver=new char[(int)f.length()]; FileReader lugeja=new FileReader(f); lugeja.read(puhver); lugeja.close(); sisu.setText(new String(puhver)); } if(e.getSource()==salvesta){ if(f==null){ kysiFailiNimi(); } salvestaFaili(); } if(e.getSource()==salvestanimega){ kysiFailiNimi(); salvestaFaili(); } } catch(Exception ex){ ex.printStackTrace(); } } void kysiFailiNimi(){ d.showDialog(this, "Vali fail"); f=d.getSelectedFile(); naitaFailiNimi(); } void salvestaFaili() throws IOException{ if(f==null){return;} FileWriter fw=new FileWriter(f); String s=sisu.getText(); fw.write(s, 0, s.length()); fw.close(); } void naitaFailiNimi(){ if(f!=null){ kest.setTitle(f.toString()); } } public static void main(String[] arg){ JFrame f=new JFrame("Redaktor"); Redaktor1 r=new Redaktor1(f); f.add(r); f.setSize(400, 400); f.setVisible(true); } }