Advertisement
iamaamir

Notepad

Feb 6th, 2015
341
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.62 KB | None | 0 0
  1. package notepad;
  2. /**
  3.  * @author toffe boy Aamir
  4.  */
  5. //@URL aamir-4u.blogspot.com
  6. import java.util.Date;
  7. import java.awt.BorderLayout;
  8. import java.awt.Color;
  9. import java.awt.Container;
  10. import java.awt.FlowLayout;
  11. import java.awt.Toolkit;
  12. import java.awt.datatransfer.Clipboard;
  13. import java.awt.datatransfer.StringSelection;
  14. import java.awt.datatransfer.Transferable;
  15. import static java.awt.event.InputEvent.*;
  16. import java.awt.event.ActionEvent;
  17. import java.awt.event.ActionListener;
  18. import java.io.BufferedReader;
  19. import java.io.BufferedWriter;
  20. import java.io.File;
  21. import java.io.FileReader;
  22. import java.io.FileWriter;
  23. import javax.swing.ImageIcon;
  24. import javax.swing.JFileChooser;
  25. import javax.swing.JFrame;
  26. import javax.swing.JLabel;
  27. import javax.swing.JMenu;
  28. import javax.swing.JMenuBar;
  29. import javax.swing.JMenuItem;
  30. import javax.swing.JPanel;
  31. import javax.swing.JTextArea;
  32. import javax.swing.KeyStroke;
  33. import javax.swing.JScrollPane;
  34. import javax.swing.UIManager;
  35.  
  36. /**
  37.  *
  38.  * @author toffe boy Aamir
  39.  */
  40. public final class Notepad extends JFrame implements ActionListener{
  41.     ImageIcon iconclose =new ImageIcon("src/img/cancel.png");
  42.     ImageIcon iconopen =new ImageIcon("src/img/briefcase.png");
  43.     ImageIcon iconsave =new ImageIcon("src/img/door_out.png");
  44.     ImageIcon helpicon = new ImageIcon("src/img/Help.png");
  45.     JLabel watch;
  46.     JMenuBar mb;
  47.     JScrollPane scp = new JScrollPane();
  48.     JMenu file,edit,view,about;
  49.     JMenuItem op,sa,cl,cop,pas,cut,help;
  50.     JTextArea out;
  51.     JPanel p = new JPanel();
  52.     public static void main(String[] args) throws InterruptedException {
  53.         Notepad note= new Notepad();
  54.        note.setSize(640,480);
  55.        note.setVisible(true);
  56.        note.setTitle("Pad Pad");
  57.        note.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  58.        setDefaultLookAndFeelDecorated(true);
  59.     }
  60.     Notepad() throws InterruptedException {
  61.         try{
  62.         UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
  63.         }
  64.         catch(Exception e){
  65.             System.out.println(e);
  66.         }
  67.         Container c = getContentPane();
  68.         c.add(p);
  69.         p.setLayout(new BorderLayout());
  70.         //Show Date
  71.         //SimpleDateFormat sdf = new SimpleDateFormat("E dd-MM-yyyy 'at' hh:mm:ss: a ");
  72.         //Date date = new Date();
  73.         watch = new JLabel(time());
  74.  
  75.         watch.setLayout(new FlowLayout());
  76.         watch.setBorder(new javax.swing.border.LineBorder(Color.WHITE, 5));
  77.         p.add("South",watch);
  78.  
  79.         //Scroll bar
  80.         p.add(scp);
  81.         scp.setBackground(Color.BLACK);
  82.         //Area of out put/input
  83.         out = new JTextArea();
  84.         scp.getViewport().add(out);
  85.         //MenuBar
  86.         mb = new JMenuBar();
  87.         p.add("North",mb);
  88.         //Menu
  89.         file = new JMenu("File");
  90.         file.setMnemonic('F');
  91.         edit = new JMenu("Edit");
  92.         view = new JMenu("View");
  93.         about= new JMenu("About");
  94.         mb.add(file);
  95.         mb.add(edit);
  96.         mb.add(view);
  97.         mb.add(about);
  98.         //File SubMenu Items
  99.         op = new JMenuItem("Open");
  100.         op.setAccelerator(KeyStroke.getKeyStroke('O',CTRL_DOWN_MASK));
  101.         op.setMnemonic('o');
  102.         sa = new JMenuItem("Save");
  103.         sa.setAccelerator(KeyStroke.getKeyStroke('S',CTRL_DOWN_MASK));
  104.         sa.setMnemonic('s');
  105.         cl = new JMenuItem("Exit");
  106.         cl.setAccelerator(KeyStroke.getKeyStroke('X',ALT_DOWN_MASK));
  107.         cl.setMnemonic('x');
  108.         file.add(op);
  109.         file.add(sa);
  110.         file.addSeparator();
  111.         file.add(cl);
  112.         //Actions on Buttons
  113.         op.addActionListener(this);
  114.         op.setIcon(iconopen);
  115.         sa.addActionListener(this);
  116.         sa.setIcon(iconsave);
  117.         cl.addActionListener(this);
  118.         cl.setIcon(iconclose);
  119.  
  120.         //Edit Sub Menu
  121.         cop = new JMenuItem("Copy");
  122.         cut = new JMenuItem("Cut");
  123.         pas = new JMenuItem("Paste");
  124.         edit.add(cut);
  125.         edit.add(cop);
  126.         edit.add(pas);
  127.          //Action on Buttons
  128.         cut.addActionListener(this);
  129.         cop.addActionListener(this);
  130.         pas.addActionListener(this);
  131.        
  132.         // About Menu
  133.         help = new JMenuItem("Help");
  134.         help.setIcon(helpicon);
  135.         about.add(help);
  136.         help.addActionListener(this);
  137.        
  138.     }
  139.     //Method for open File
  140.     public void open(){
  141.         //file Chooser
  142.         JFileChooser choose = new JFileChooser();
  143.         // display box
  144.         int i = choose.showOpenDialog(this);
  145.         if (i==JFileChooser.APPROVE_OPTION){
  146.             File f =choose.getSelectedFile();
  147.             String filename = f.getPath();
  148.             String str = "";
  149.             String str1="";
  150. //Reading File
  151.             try{
  152.     BufferedReader br = new BufferedReader(new FileReader(filename));
  153.     //Read File
  154.     while((str=br.readLine())!=null)
  155.         str1 +=str+"\n";
  156.     out.setText(str1);
  157.     br.close();
  158. }
  159.             catch(Exception e){}
  160.         }
  161.     }
  162.     //Method for Save File
  163.     public void save(){
  164. JFileChooser choose = new JFileChooser();
  165. int i = choose.showSaveDialog(this);
  166. if (i==choose.APPROVE_OPTION){
  167.     File f = choose.getSelectedFile();
  168.     String saved = f.getPath()+".txt";
  169.     try{
  170.         BufferedWriter savefile = new BufferedWriter(new FileWriter(saved));
  171.         savefile.write(this.out.getText());
  172.         //out is the object of text Area where user writting text
  173.         savefile.flush();
  174.         savefile.close();
  175.  
  176.     }
  177.     catch(Exception e){System.out.println(e);}
  178.  
  179.     }
  180.     }
  181.  
  182.     public  void copyme(String writeMe) {
  183.         // get the system clipboard
  184.         Clipboard systemClipboard =
  185.             Toolkit
  186.                 .getDefaultToolkit()
  187.                 .getSystemClipboard();
  188.         // set the textual content on the clipboard to our
  189.         // transferable object
  190.         // we use the
  191.         Transferable transferableText =
  192.             new StringSelection(writeMe);
  193.         systemClipboard.setContents(
  194.             transferableText,
  195.             null);
  196.     }
  197.    
  198.     public void pasteme(){
  199.        
  200.     }
  201.  
  202.     //Action Performe
  203.     public void actionPerformed(ActionEvent e){
  204.         if (op.isArmed()){
  205.             this.open();
  206.  
  207.         }
  208.         else if(cl.isArmed())
  209.         {
  210. System.exit(0);
  211.  }
  212.  else if (sa.isArmed()){
  213.      this.save();
  214.  }
  215.  else if (cop.isArmed()){
  216.      this.copyme(out.getSelectedText());
  217.  }
  218.        
  219.  else if (pas.isArmed()){
  220.      this.pasteme();
  221.      
  222.  }
  223.  else if (help.isArmed()){
  224.     //create a new class to show something about you
  225.      About.AboutI();
  226.  }
  227.  
  228.     }
  229.  
  230.     public String time() throws InterruptedException{
  231.         Thread.sleep(1000);
  232.         return new Date().toString();
  233.     }
  234. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement