mnaufaldillah

Editor Tugas 11

Jan 4th, 2021
306
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.35 KB | None | 0 0
  1. import java.awt.*;
  2. import javax.swing.*;
  3. import java.io.*;
  4. import java.awt.event.*;
  5. import java.awt.Container;
  6. import javax.swing.plaf.metal.*;
  7. import javax.swing.text.*;
  8. /**
  9.  * Write a description of class Editor here.
  10.  *
  11.  * @author Muhammad Naufaldillah
  12.  * @version 3 Januari 2021
  13.  */
  14. public class Editor extends JFrame implements ActionListener
  15. {
  16.     // Text component
  17.     JTextArea t;
  18.    
  19.     // Frame
  20.     JFrame f;
  21.    
  22.     // Constructor
  23.     Editor()
  24.     {
  25.         // Create a frame
  26.         f = new JFrame("editor");
  27.        
  28.         try
  29.         {
  30.             // Set metl look and feel
  31.             UIManager.setLookAndFeel("javax.swing.plaf.metal.MetalLookAndFeel");
  32.            
  33.             // Set theme to ocean
  34.             MetalLookAndFeel.setCurrentTheme(new OceanTheme());
  35.         }
  36.         catch(Exception e)
  37.         {
  38.            
  39.         }
  40.        
  41.         // Text component
  42.         t = new JTextArea();
  43.        
  44.         // Create a menubar
  45.         JMenuBar mb = new JMenuBar();
  46.        
  47.         // Create a menu for menu
  48.         JMenu m1 = new JMenu("File");
  49.        
  50.         // Create menu items
  51.         JMenuItem mi1 = new JMenuItem("New");
  52.         JMenuItem mi2 = new JMenuItem("Open");
  53.         JMenuItem mi3 = new JMenuItem("Save");
  54.         JMenuItem mi9 = new JMenuItem("Print");
  55.        
  56.         // Add action listener
  57.         mi1.addActionListener(this);
  58.         mi2.addActionListener(this);
  59.         mi3.addActionListener(this);
  60.         mi9.addActionListener(this);
  61.        
  62.         m1.add(mi1);
  63.         m1.add(mi2);
  64.         m1.add(mi3);
  65.         m1.add(mi9);
  66.        
  67.         // Create a menu for menu
  68.         JMenu m2 = new JMenu("Edit");
  69.        
  70.         // Create menu items
  71.         JMenuItem mi4 = new JMenuItem("cut");
  72.         JMenuItem mi5 = new JMenuItem("copy");
  73.         JMenuItem mi6 = new JMenuItem("paste");
  74.        
  75.         // Add action listener
  76.         mi4.addActionListener(this);
  77.         mi5.addActionListener(this);
  78.         mi6.addActionListener(this);
  79.        
  80.         m2.add(mi4);
  81.         m2.add(mi5);
  82.         m2.add(mi6);
  83.        
  84.         JMenuItem mc = new JMenuItem("close");
  85.        
  86.         mc.addActionListener(this);
  87.        
  88.         mb.add(m1);
  89.         mb.add(m2);
  90.         mb.add(mc);
  91.        
  92.         f.setJMenuBar(mb);
  93.         f.add(t);
  94.         f.setSize(500, 500);
  95.         f.show();
  96.     }
  97.    
  98.     public void actionPerformed(ActionEvent e)
  99.     {
  100.         String s = e.getActionCommand();
  101.        
  102.         if(s.equals("cut"))
  103.         {
  104.             t.cut();
  105.         }
  106.         else if(s.equals("copy"))
  107.         {
  108.             t.copy();
  109.         }
  110.         else if(s.equals("paste"))
  111.         {
  112.             t.paste();
  113.         }
  114.         else if(s.equals("Save"))
  115.         {
  116.             // Create an object of JFileChooser class
  117.             JFileChooser j = new JFileChooser("f:");
  118.            
  119.             // Invoke the showsSaveDialog function to show the save dialog
  120.             int r = j.showSaveDialog(null);
  121.            
  122.             if(r == JFileChooser.APPROVE_OPTION)
  123.             {
  124.                 // Set the label to the path of selected directory
  125.                 File fi = new File(j.getSelectedFile().getAbsolutePath());
  126.                
  127.                 try
  128.                 {
  129.                     // Create a file writer
  130.                     FileWriter wr = new FileWriter(fi, false);
  131.                    
  132.                     // Create buffered writer to write
  133.                     BufferedWriter w = new BufferedWriter(wr);
  134.                    
  135.                     // Write
  136.                     w.write(t.getText());
  137.                    
  138.                     w.flush();
  139.                     w.close();
  140.                 }
  141.                 catch(Exception evt)
  142.                 {
  143.                     JOptionPane.showMessageDialog(f, evt.getMessage());
  144.                 }
  145.             }
  146.             // If the user cancelled the operation
  147.             else
  148.             {
  149.                 JOptionPane.showMessageDialog(f, "the user cancelled the operation");
  150.             }
  151.         }
  152.         else if(s.equals("Print"))
  153.         {
  154.             try
  155.                 {
  156.                     // print the file
  157.                     t.print();
  158.                 }
  159.                 catch(Exception evt)
  160.                 {
  161.                     JOptionPane.showMessageDialog(f, evt.getMessage());
  162.                 }
  163.         }
  164.         else if(s.equals("Open"))
  165.         {
  166.             // Create an object of JFileChooser class
  167.             JFileChooser j = new JFileChooser("f:");
  168.            
  169.             // Invoke the showsSaveDialog function to show the save dialog
  170.             int r = j.showOpenDialog(null);
  171.            
  172.             if(r == JFileChooser.APPROVE_OPTION)
  173.             {
  174.                 // Set the label to the path of selected directory
  175.                 File fi = new File(j.getSelectedFile().getAbsolutePath());
  176.                
  177.                 try
  178.                 {
  179.                     // String
  180.                     String s1 = "", sl = "";
  181.                    
  182.                     // File reader
  183.                     FileReader fr = new FileReader(fi);
  184.                    
  185.                     // Buffered reader
  186.                     BufferedReader br = new BufferedReader(fr);
  187.                    
  188.                     // Initialize sl
  189.                     sl = br.readLine();
  190.                    
  191.                     // Take the input from the file
  192.                     while((s1 = br.readLine()) != null)
  193.                     {
  194.                         sl = sl + "\n" + s1;
  195.                     }
  196.                    
  197.                     // Set the text
  198.                     t.setText(sl);
  199.                 }
  200.                 catch(Exception evt)
  201.                 {
  202.                     JOptionPane.showMessageDialog(f, evt.getMessage());
  203.                 }
  204.             }
  205.             // If the user cancelled the operation
  206.             else
  207.             {
  208.                 JOptionPane.showMessageDialog(f, "the user cancelled the operation");
  209.             }
  210.         }
  211.         else if(s.equals("New"))
  212.         {
  213.             t.setText("");
  214.         }
  215.         else if(s.equals("Open"))
  216.         {
  217.             f.setVisible(false);
  218.         }
  219.     }
  220.    
  221.     // Main class
  222.     public static void main(String args[])
  223.     {
  224.         Editor e = new Editor();
  225.     }
  226. }
Advertisement
Add Comment
Please, Sign In to add comment