Notepad in Java

Notepad In Java

  1. import java.awt.*;  
  2. import java.awt.event.*;  
  3. import javax.swing.*;  
  4. import java.util.*;  
  5. import java.io.*;  
  6. class popup extends JFrame implements ActionListener {  
  7.  final JDesktopPane desk;  
  8.  JButton b, b1, b2, b3, b4, b5, b6, b7, b8, b9, b10, b11;  
  9.  JPopupMenu popmenu;  
  10.  JToolBar tb;  
  11.  JScrollPane scroll;  
  12.  JTextArea txt;  
  13.  JMenuItem cut, copy, paste;  
  14.  JMenu file, edit, format, view, help;  
  15.  JMenuItem new1, open, save, saveas, print, pageset, exit;  
  16.  JMenuItem cut1, copy1, paste1;  
  17.  JMenuItem font;  
  18.  JMenuItem viewhelp, abtnotepad;  
  19.  JMenuBar menubar;  
  20.  JCheckBoxMenuItem bold, italic, underline, word;  
  21.  JComboBox cb;  
  22.  String a[] = {  
  23.   "1",  
  24.   "2",  
  25.   "3"  
  26.  };  
  27.  popup() {  
  28.   setTitle("Notepad++");  
  29.   popmenu = new JPopupMenu();  
  30.   txt = new JTextArea();  
  31.   scroll = new JScrollPane(txt);  
  32.   txt.setLineWrap(true);  
  33.   cut = new JMenuItem("Cut"new ImageIcon("cut.jpg"));  
  34.   copy = new JMenuItem("Copy"new ImageIcon("copy.jpg"));  
  35.   paste = new JMenuItem("Paste"new ImageIcon("paste.jpg"));  
  36.   menubar = new JMenuBar();  
  37.   ImageIcon icon = new ImageIcon("notepad.jpg");  
  38.   setIconImage(icon.getImage());  
  39.   file = new JMenu("File");  
  40.   file.setMnemonic(KeyEvent.VK_F);  
  41.   edit = new JMenu("Edit");  
  42.   edit.setMnemonic(KeyEvent.VK_E);  
  43.   format = new JMenu("Format");  
  44.   format.setMnemonic(KeyEvent.VK_T);  
  45.   view = new JMenu("View");  
  46.   view.setMnemonic(KeyEvent.VK_V);  
  47.   help = new JMenu("Help");  
  48.   help.setMnemonic(KeyEvent.VK_H);  
  49.   menubar.add(file);  
  50.   menubar.add(edit);  
  51.   menubar.add(format);  
  52.   menubar.add(view);  
  53.   menubar.add(help);  
  54.   setJMenuBar(menubar);  
  55.   new1 = new JMenuItem("New"new ImageIcon("new.jpg"));  
  56.   new1.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_N, Event.CTRL_MASK));  
  57.   new1.addActionListener(this);  
  58.   open = new JMenuItem("Open"new ImageIcon("open.jpg"));  
  59.   open.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_O, Event.CTRL_MASK));  
  60.   save = new JMenuItem("Save"new ImageIcon("save.jpg"));  
  61.   save.addActionListener(this);  
  62.   save.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_S, Event.CTRL_MASK));  
  63.   saveas = new JMenuItem("Save as"new ImageIcon("saveas.jpg"));  
  64.   saveas.addActionListener(this);  
  65.   pageset = new JMenuItem("Page Setup");  
  66.   pageset.addActionListener(this);  
  67.   print = new JMenuItem("Print"new ImageIcon("print.jpg"));  
  68.   print.addActionListener(this);  
  69.   exit = new JMenuItem("Exit");  
  70.   exit.addActionListener(this);  
  71.   exit.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_Q, Event.CTRL_MASK));  
  72.   file.add(new1);  
  73.   file.add(open);  
  74.   file.add(save);  
  75.   file.add(saveas);  
  76.   file.addSeparator();  
  77.   file.add(pageset);  
  78.   file.add(print);  
  79.   file.addSeparator();  
  80.   file.add(exit);  
  81.   cut1 = new JMenuItem("Cut"new ImageIcon("cut.jpg"));  
  82.   copy1 = new JMenuItem("Copy"new ImageIcon("copy.jpg"));  
  83.   paste1 = new JMenuItem("Paste"new ImageIcon("paste.jpg"));  
  84.   edit.add(cut1);  
  85.   edit.add(copy1);  
  86.   edit.add(paste1);  
  87.   cut1.addActionListener(this);  
  88.   copy1.addActionListener(this);  
  89.   paste1.addActionListener(this);  
  90.   word = new JCheckBoxMenuItem("Word Wrap");  
  91.   format.add(word);  
  92.   viewhelp = new JMenuItem("View Help"new ImageIcon("help.jpg"));  
  93.   abtnotepad = new JMenuItem("About Notepad");  
  94.   help.add(viewhelp);  
  95.   help.add(abtnotepad);  
  96.   abtnotepad.addActionListener(this);  
  97.   bold = new JCheckBoxMenuItem("Bold"new ImageIcon("bold.jpg"));  
  98.   italic = new JCheckBoxMenuItem("Italic"new ImageIcon("italic.jpg"));  
  99.   underline = new JCheckBoxMenuItem("UnderLine"new ImageIcon("underline.jpg"));  
  100.   JMenu nn = new JMenu("Bold");  
  101.   nn.add(bold);  
  102.   nn.add(italic);  
  103.   nn.add(underline);  
  104.   format.add(nn);  
  105.   ButtonGroup bg = new ButtonGroup();  
  106.   JRadioButtonMenuItem left = new JRadioButtonMenuItem("Left"new ImageIcon("left.jpg"));  
  107.   JRadioButtonMenuItem center = new JRadioButtonMenuItem("Center"new ImageIcon("center.jpg"));  
  108.   JRadioButtonMenuItem right = new JRadioButtonMenuItem("Right"new ImageIcon("right.jpg"));  
  109.   JRadioButtonMenuItem just = new JRadioButtonMenuItem("Justify"new ImageIcon("just.jpg"));  
  110.   bg.add(left);  
  111.   bg.add(center);  
  112.   bg.add(right);  
  113.   bg.add(just);  
  114.   JMenu xxx = new JMenu("Align");  
  115.   xxx.add(left);  
  116.   xxx.add(center);  
  117.   xxx.add(right);  
  118.   xxx.add(just);  
  119.   format.addSeparator();  
  120.   format.add(xxx);  
  121.   tb = new JToolBar(SwingConstants.VERTICAL);  
  122.   tb.setBackground(Color.green);  
  123.   tb.setLayout(new GridLayout(10));  
  124.   b = new JButton(new ImageIcon("new.jpg"));  
  125.   b1 = new JButton(new ImageIcon("open.jpg"));  
  126.   b2 = new JButton(new ImageIcon("save.jpg"));  
  127.   b3 = new JButton(new ImageIcon("cut.jpg"));  
  128.   b4 = new JButton(new ImageIcon("copy.jpg"));  
  129.   b5 = new JButton(new ImageIcon("paste.jpg"));  
  130.   b6 = new JButton(new ImageIcon("left.jpg"));  
  131.   b6.addActionListener(this);  
  132.   b7 = new JButton(new ImageIcon("center.jpg"));  
  133.   b7.addActionListener(this);  
  134.   b8 = new JButton(new ImageIcon("right.jpg"));  
  135.   b8.addActionListener(this);  
  136.   b9 = new JButton(new ImageIcon("help.jpg"));  
  137.   b10 = new JButton(new ImageIcon("print.jpg"));  
  138.   b11 = new JButton(new ImageIcon("right.jpg"));  
  139.   cb = new JComboBox(a);  
  140.   b.addActionListener(this);  
  141.   b.setActionCommand("New");  
  142.   b1.addActionListener(this);  
  143.   b2.addActionListener(this);  
  144.   b3.addActionListener(this);  
  145.   b4.addActionListener(this);  
  146.   b5.addActionListener(this);  
  147.   b6.addActionListener(this);  
  148.   b7.addActionListener(this);  
  149.   b8.addActionListener(this);  
  150.   b9.addActionListener(this);  
  151.   b10.addActionListener(this);  
  152.   b11.addActionListener(this);  
  153.   italic.addActionListener(this);  
  154.   tb.add(b);  
  155.   tb.add(b1);  
  156.   tb.add(b2);  
  157.   tb.addSeparator();  
  158.   tb.add(cb);  
  159.   tb.add(b3);  
  160.   tb.add(b4);  
  161.   tb.add(b5);  
  162.   tb.addSeparator();  
  163.   tb.add(b6);  
  164.   tb.add(b7);  
  165.   tb.add(b8);  
  166.   tb.addSeparator();  
  167.   tb.add(b9);  
  168.   tb.add(b10);  
  169.   tb.add(b11);  
  170.   add(BorderLayout.NORTH, tb);  
  171.   desk = new JDesktopPane();  
  172.   add(desk);  
  173.   popmenu.add(cut);  
  174.   popmenu.add(copy);  
  175.   popmenu.add(paste);  
  176.   txt.addMouseListener(new MouseAdapter() {  
  177.    public void mouseReleased(MouseEvent e) {  
  178.     if (e.isPopupTrigger()) {  
  179.      popmenu.setLocation(e.getX(), e.getY());  
  180.      popmenu.setVisible(true);  
  181.     }  
  182.    }  
  183.   });  
  184.   add(scroll);  
  185.   txt.setFont(new Font("Times New Roman", Font.BOLD, 20));  
  186.   setSize(500400);  
  187.   setVisible(true);  
  188.   setDefaultCloseOperation(EXIT_ON_CLOSE);  
  189.  }  
  190.  public void actionPerformed(ActionEvent e) {  
  191.   if (e.getSource() == italic) {  
  192.    txt.setFont(new Font("Arial", Font.ITALIC, 22));  
  193.   }  
  194.   if (e.getSource() == cut1 || e.getSource() == b3) {  
  195.    txt.cut();  
  196.   }  
  197.   if (e.getSource() == copy1 || e.getSource() == b4) {  
  198.    txt.copy();  
  199.   }  
  200.   if (e.getSource() == paste1 || e.getSource() == b5) {  
  201.    txt.paste();  
  202.   }  
  203.   if (e.getSource() == new1 || e.getSource() == b) {  
  204.    new popup();  
  205.   }  
  206.   if (e.getSource() == exit) {  
  207.    System.exit(0);  
  208.   }  
  209.   if (e.getSource() == abtnotepad) {  
  210.    JOptionPane op = new JOptionPane();  
  211.    JOptionPane.showMessageDialog(this"Notepad is easily to note anything your reqiurnemet""ABOUTNOTEPAD", JOptionPane.INFORMATION_MESSAGE);  
  212.   }  
  213.   if (e.getSource() == save) {  
  214.    try {  
  215.     FileDialog save = new FileDialog(this"save dear", FileDialog.SAVE);  
  216.     save.show();  
  217.     FileWriter write = new FileWriter(save.getDirectory() + save.getFile());  
  218.     BufferedWriter wr = new BufferedWriter(write);  
  219.     wr.write(this.txt.getText());  
  220.     wr.close();  
  221.    } catch (Exception ex) {  
  222.     System.out.println(" " + ex);  
  223.    }  
  224.   }  
  225.   if (e.getSource() == open || e.getSource() == b1) {  
  226.    try {  
  227.     FileDialog fd = new FileDialog(this"Open File", FileDialog.LOAD);  
  228.     fd.show();  
  229.     System.out.println(fd.getDirectory() + fd.getFile());  
  230.     FileReader fr = new FileReader(fd.getDirectory() + fd.getFile());  
  231.     BufferedReader br = new BufferedReader(fr);  
  232.     String hi = br.readLine();  
  233.     while (hi != null) {  
  234.      txt.append(hi + "\n");  
  235.      hi = br.readLine();  
  236.     }  
  237.     fr.close();  
  238.    } catch (Exception ex) {  
  239.     System.out.print(" " + ex);  
  240.    }  
  241.   }  
  242.  }  
  243.  public static void main(String[] args) {  
  244.   new popup();  
  245.  }  
  246. }  
Next Recommended Reading What is Web Container in Java?