Anran

Notepad.java

Dec 28th, 2020
149
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.48 KB | None | 0 0
  1.  
  2. /**
  3.  * Class utama dari aplikasi Notepad
  4.  *
  5.  * @author Andika Nugrahanto
  6.  * @version 28/12/2020
  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(); // Membuat MenuBar
  18.     private Menu file = new Menu(); // Membuat menu File
  19.    
  20.     private MenuItem newFile = new MenuItem(); // Opsi New
  21.     private MenuItem openFile = new MenuItem();  // Opsi Open
  22.     private MenuItem saveFile = new MenuItem(); // Opsi Save
  23.     private MenuItem close = new MenuItem(); // Opsi Close
  24.  
  25.     public Notepad() {
  26.         this.setSize(1280, 720); // Set ukuran Notepad
  27.         this.setTitle("Java Notepad"); // Set Nama aplikasi
  28.         setDefaultCloseOperation(EXIT_ON_CLOSE); // Set operasi penutupan aplikasi
  29.         this.textArea.setFont(new Font("Century Gothic", Font.BOLD, 12)); // Set font default di TextArea
  30.        
  31.         this.getContentPane().setLayout(new BorderLayout());
  32.         this.getContentPane().add(textArea);
  33.  
  34.         // Menambah menu bar ke GUI
  35.         this.setMenuBar(this.menuBar);
  36.         this.menuBar.add(this.file);
  37.  
  38.         this.file.setLabel("File");
  39.        
  40.         //Opsi New
  41.         this.newFile.setLabel("New"); //label opsi
  42.         this.newFile.addActionListener(this); //action listener supaya tahu ketika diklik
  43.         this.newFile.setShortcut(new MenuShortcut(KeyEvent.VK_N, false)); // Shortcut ctrl + n
  44.         this.file.add(this.newFile); // menambahkan ke menu "File"
  45.        
  46.         //Opsi Open
  47.         this.openFile.setLabel("Open");
  48.         this.openFile.addActionListener(this);
  49.         this.openFile.setShortcut(new MenuShortcut(KeyEvent.VK_O, false)); // Shortcut ctrl + o
  50.         this.file.add(this.openFile);
  51.  
  52.         //Opsi Save
  53.         this.saveFile.setLabel("Save");
  54.         this.saveFile.addActionListener(this);
  55.         this.saveFile.setShortcut(new MenuShortcut(KeyEvent.VK_S, false)); // Shortcut ctrl + s
  56.         this.file.add(this.saveFile);
  57.  
  58.         //Opsi Close
  59.         this.close.setLabel("Close");
  60.         this.close.setShortcut(new MenuShortcut(KeyEvent.VK_F4, false)); // Shortcut ctrl + F4
  61.         this.close.addActionListener(this);
  62.         this.file.add(this.close);
  63.     }
  64.  
  65.     public void actionPerformed (ActionEvent e) {
  66.         // Jika memilih Opsi Close
  67.         if (e.getSource() == this.close)
  68.             this.dispose(); // Dispose semua resource dan tutup aplikasi
  69.  
  70.         // Jika memilih opsi Open
  71.         else if (e.getSource() == this.openFile) {
  72.             JFileChooser open = new JFileChooser(); // Membuka file chooser
  73.             int option = open.showOpenDialog(this); // Untuk mendapatkan opsi yang dipilih user (approve atau cancel)
  74.            
  75.             // Jika user klik OK, terdapat "APPROVE_OPTION"
  76.             if (option == JFileChooser.APPROVE_OPTION) {
  77.                 this.textArea.setText(""); // Clear TextArea kemudian membuka isi File
  78.                 try {
  79.                     // Membuat Scanner untuk membaca File
  80.                     Scanner scan = new Scanner(new FileReader(open.getSelectedFile().getPath()));
  81.                     while (scan.hasNext())
  82.                         this.textArea.append(scan.nextLine() + "\n");
  83.                 } catch (Exception ex) {
  84.                     System.out.println(ex.getMessage());
  85.                 }
  86.             }
  87.         }
  88.  
  89.         // Jika memilih opsi Save
  90.         else if (e.getSource() == this.saveFile) {
  91.             JFileChooser save = new JFileChooser(); // Membuka File Chooser
  92.             int option = save.showSaveDialog(this);
  93.    
  94.             if (option == JFileChooser.APPROVE_OPTION) {
  95.                 try {
  96.                    
  97.                     BufferedWriter out = new BufferedWriter(new FileWriter(save.getSelectedFile().getPath()));
  98.                     out.write(this.textArea.getText());
  99.                     out.close();
  100.                 } catch (Exception ex) {
  101.                     System.out.println(ex.getMessage());
  102.                 }
  103.             }
  104.         }
  105.        
  106.         // Jika memilih opsi New
  107.         else if (e.getSource() == this.newFile) {
  108.             textArea.setText("");
  109.         }
  110.     }
  111.    
  112.    
  113.     public static void main(String args[]) {
  114.         Notepad app = new Notepad();
  115.         app.setVisible(true);
  116.    
  117.    
  118. }
  119. }
Advertisement
Add Comment
Please, Sign In to add comment