You are on page 1of 8

import java.awt.*; import java.awt.event.*; import java.io.*; import java.util.*; import javax.swing.*; import javax.swing.event.

*; public class notepad extends JFrame { public static final String name="NOTEPAD"; JTextArea m_editor; JFileChooser m_chooser; File m_currentfile; boolean m_textchanged=false; JMenu editmenu,helpmenu; JMenuItem edititem1,edititem2,edititem3,edititem4,edititem5,edititem6,helpitem1,helpitem2,helpitem3; notepad() { super(name); setSize(450,350); m_editor=new JTextArea(); JScrollPane ps=new JScrollPane(m_editor); getContentPane().add(ps,BorderLayout.CENTER); JMenuBar menubar=new JMenuBar(); //----------------------------------------------------------------------------------------------------------------------------editmenu=new JMenu("Edit");

editmenu.add(new JSeparator());

edititem3=new JMenuItem("CUT"); edititem4=new JMenuItem("COPY"); edititem5=new JMenuItem("PASTE"); edititem1=new JMenuItem("Delete");

editmenu.add(edititem3); editmenu.add(edititem4); editmenu.add(edititem5); editmenu.add(edititem6);

edititem3.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_X,ActionEvent.CTRL_MASK)); edititem4.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_C,ActionEvent.CTRL_MASK)); edititem5.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_V,ActionEvent.CTRL_MASK)); edititem6.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_D,ActionEvent.CTRL_MASK));

edititem3.addActionListener(new Mycut()); edititem4.addActionListener(new Mycopy()); edititem5.addActionListener(new Mypaste()); edititem6.addActionListener(new Mydelete()); //----------------------------------------------------------------------------------------------------------------------------//--------------------------------------------help menu-------------------------------------------------JMenu helpmenu=new JMenu("HELP");

editmenu.add(new JSeparator()); JMenuItem helpitem1=new JMenuItem("Key short cuts"); JMenuItem helpitem2=new JMenuItem("About us"); JMenuItem helpitem3=new JMenuItem("contact me"); helpmenu.add(helpitem1); helpmenu.add(helpitem2); helpmenu.add(helpitem3); //----------------------------------------------------help actionlistener helpitem1.addActionListener(new MyHelp1()); helpitem2.addActionListener(new MyHelp2()); helpitem3.addActionListener(new MyHelp3()); //--------------------------------------------------------------------------------------------------------------------------------

JMenu file=new JMenu("File"); m_chooser=new JFileChooser(); JMenuItem item1=new JMenuItem("New");

item1.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_N,ActionEvent.CTRL_MASK));//new short cut ActionListener first= new ActionListener() { public void actionPerformed(ActionEvent ae) { if(!prompttosave()) return; newDocument(); }

}; item1.addActionListener(first); file.add(item1); JMenuItem item2=new JMenuItem("Open");

item2.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_O,ActionEvent.CTRL_MASK));//open short cut first= new ActionListener() { public void actionPerformed(ActionEvent ae) { if(!prompttosave()) return; openDocument(); } }; item2.addActionListener(first); file.add(item2); JMenuItem item3=new JMenuItem("Save");

item3.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_S,ActionEvent.CTRL_MASK));//new short cut first= new ActionListener() { public void actionPerformed(ActionEvent ae) { if(!m_textchanged)

return; saveFile(false); } }; item3.addActionListener(first); file.add(item3); JMenuItem item4=new JMenuItem("Save As");

item4.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_S,ActionEvent.CTRL_MASK+ActionE vent.SHIFT_MASK));//saveas shortcut first= new ActionListener() { public void actionPerformed(ActionEvent ae) { saveFile(true); } }; item4.addActionListener(first); file.add(item4); JMenuItem item5=new JMenuItem("Exit");

item5.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_W,ActionEvent.CTRL_MASK)); first= new ActionListener() { public void actionPerformed(ActionEvent ae) { System.exit(0);

} }; item5.addActionListener(first); file.add(item5); menubar.add(file); menubar.add(editmenu); menubar.add(helpmenu); setJMenuBar(menubar); WindowListener wndcloser=new WindowAdapter() { public void windowClosing(WindowEvent e) { if(!prompttosave()) return; System.exit(0); } }; addWindowListener(wndcloser);

} boolean prompttosave() { if(!m_textchanged) return true; int result=JOptionPane.showConfirmDialog(this,"Do you want to save the changes?",name,JOptionPane.YES_NO_CANCEL_OPTION,JOptionPane.QUESTION_MESSAGE);

switch(result) { case JOptionPane.YES_OPTION: if(!saveFile(false)) return false; return true; case JOptionPane.NO_OPTION: return true; case JOptionPane.CANCEL_OPTION: return false; } return true; } void newDocument() { m_editor.setText(""); m_currentfile=null; setTitle(name + "[ "+getDocumentName()+" ]"); m_textchanged=false; m_editor.getDocument().addDocumentListener(new UpdateListener()); } void openDocument() { if(m_chooser.showOpenDialog(notepad.this)!= JFileChooser.APPROVE_OPTION) return;

File f=m_chooser.getSelectedFile(); if(f==null || !f.isFile()) return; m_currentfile=f; try { FileReader in=new FileReader(m_currentfile); m_editor.read(in,null); in.close(); setTitle(name + "[ "+getDocumentName()+" ]"); }

You might also like