document.write('
Data hosted with ♥ by Pastebin.com - Download Raw - See Original
  1. /**
  2.  * Class notepad yang berisikan seluruh command untuk melaksanakan program notepad
  3.  *
  4.  * @Muhammad Akmal Joedhiawan
  5.  * @05111940000125
  6.  */
  7.  
  8.  
  9. import javax.swing.*;
  10. import java.awt.*;
  11. import java.awt.event.*;
  12. import java.util.Scanner;
  13. import java.io.*;
  14.  
  15. public class Notepad extends JFrame implements ActionListener {
  16.     private TextArea textArea = new TextArea("", 0,0, TextArea.SCROLLBARS_VERTICAL_ONLY);
  17.     private MenuBar menuBar = new MenuBar(); // MenuBar
  18.     private Menu file = new Menu();
  19.     private MenuItem openFile = new MenuItem();  // opsi open
  20.     private MenuItem saveFile = new MenuItem(); // opsi save
  21.     private MenuItem close = new MenuItem(); // opsi close
  22.     private MenuItem newFile = new MenuItem(); // opsi new
  23.  
  24.     public Notepad() {
  25.         this.setSize(800, 600); // ukuran notepad
  26.         this.setTitle("Java Notepad");
  27.         setDefaultCloseOperation(EXIT_ON_CLOSE);
  28.         this.textArea.setFont(new Font("Century Gothic", Font.BOLD, 12));
  29.         this.getContentPane().setLayout(new BorderLayout());
  30.         this.getContentPane().add(textArea);
  31.  
  32.         // menu bar GUI
  33.         this.setMenuBar(this.menuBar);
  34.         this.menuBar.add(this.file);
  35.         // menu file
  36.         this.file.setLabel("File");
  37.  
  38.         // opsi open
  39.         this.openFile.setLabel("Open");
  40.         this.openFile.addActionListener(this);
  41.         this.openFile.setShortcut(new MenuShortcut(KeyEvent.VK_O, false));
  42.         this.file.add(this.openFile);
  43.  
  44.         // opsi save
  45.         this.saveFile.setLabel("Save");
  46.         this.saveFile.addActionListener(this);
  47.         this.saveFile.setShortcut(new MenuShortcut(KeyEvent.VK_S, false));
  48.         this.file.add(this.saveFile);
  49.  
  50.         // opsi close
  51.         this.close.setLabel("Close");
  52.         this.close.setShortcut(new MenuShortcut(KeyEvent.VK_F4, false));
  53.         this.close.addActionListener(this);
  54.         this.file.add(this.close);
  55.        
  56.         // opsi new
  57.         this.newFile.setLabel("New");
  58.         this.newFile.addActionListener(this);
  59.         this.newFile.setShortcut(new MenuShortcut(KeyEvent.VK_N, false));
  60.         this.file.add(this.newFile);
  61.     }
  62.  
  63.     public void actionPerformed (ActionEvent e) {
  64.        
  65.         if (e.getSource() == this.close)
  66.             this.dispose();
  67.         else if (e.getSource() == this.openFile) {
  68.             JFileChooser open = new JFileChooser();
  69.             int option = open.showOpenDialog(this);
  70.             if (option == JFileChooser.APPROVE_OPTION) {
  71.                 this.textArea.setText("");
  72.                 try {
  73.                     Scanner scan = new Scanner(new FileReader(open.getSelectedFile().getPath()));
  74.                     while (scan.hasNext())
  75.                         this.textArea.append(scan.nextLine() + "\\n");
  76.                 } catch (Exception ex) {
  77.                     System.out.println(ex.getMessage());
  78.                 }
  79.             }
  80.         }
  81.         else if (e.getSource() == this.saveFile) {
  82.             JFileChooser save = new JFileChooser();
  83.             int option = save.showSaveDialog(this);
  84.             if (option == JFileChooser.APPROVE_OPTION) {
  85.                 try {
  86.                     BufferedWriter out = new BufferedWriter(new FileWriter(save.getSelectedFile().getPath()));
  87.                     out.write(this.textArea.getText());
  88.                     out.close();
  89.                 } catch (Exception ex) {
  90.                     System.out.println(ex.getMessage());
  91.                 }
  92.             }
  93.         }
  94.         else if (e.getSource() == this.newFile) {
  95.             textArea.setText("");
  96.         }
  97.     }
  98. }
  99.  
');