Professional Documents
Culture Documents
Note Pad
Note Pad
*; 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.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()); //--------------------------------------------------------------------------------------------------------------------------------
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(); }
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)
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.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()+" ]"); }