naufalphew

Notepad

Dec 28th, 2020
147
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.16 KB | None | 0 0
  1. import javax.swing.*;
  2. import java.awt.*;
  3. import java.awt.event.*;
  4. import java.util.Scanner;
  5. import java.io.*;
  6.  
  7. public class Notepad extends JFrame implements ActionListener {
  8.   private TextArea textArea = new TextArea("", 0,0, TextArea.SCROLLBARS_VERTICAL_ONLY);
  9.   // membuat menu bar
  10.   private MenuBar menuBar = new MenuBar();
  11.     private Menu file = new Menu(); // menubar file
  12.     private MenuItem newfile = new MenuItem(); //program new
  13.     private MenuItem openFile = new MenuItem(); // program open
  14.     private MenuItem saveFile = new MenuItem(); // program save
  15.     private MenuItem close = new MenuItem(); // program close
  16.  
  17.  
  18.     public Notepad() {
  19.         this.setSize(500, 300); //setting ukuran dari windownya
  20.         this.setTitle("Java Notepad Sederhana"); //nama program text editor
  21.         setDefaultCloseOperation(EXIT_ON_CLOSE); //mengatur pengaturan default close untuk exit
  22.         this.textArea.setFont(new Font("Century Gothic", Font.BOLD, 12)); //penentuan font
  23.        
  24.         this.getContentPane().setLayout(new BorderLayout());
  25.         this.getContentPane().add(textArea);
  26.  
  27.         //menampulkan menubar pada GUI
  28.         this.setMenuBar(this.menuBar);
  29.         this.menuBar.add(this.file);
  30.         this.file.setLabel("File");
  31.  
  32.  
  33.         this.newfile.setLabel("New");
  34.         this.newfile.addActionListener(this);
  35.         this.newfile.setShortcut(new MenuShortcut(KeyEvent.VK_N, false));
  36.         this.file.add(this.newfile);
  37.        
  38.  
  39.         this.openFile.setLabel("Open"); //menentukan tablenya
  40.         this.openFile.addActionListener(this); //untuk responsif
  41.         this.openFile.setShortcut(new MenuShortcut(KeyEvent.VK_O, false)); //setting keyboard shortcut
  42.         this.file.add(this.openFile); // tambahkan ke menu "File"
  43.  
  44.  
  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.        
  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.  
  57.     public void actionPerformed (ActionEvent e) {
  58.         //opsi close
  59.         if (e.getSource() == this.close)
  60.             this.dispose(); //menutup program
  61.  
  62.         //opsi open
  63.         else if (e.getSource() == this.openFile) {
  64.             JFileChooser open = new JFileChooser(); //membuka file dari browser file
  65.             int option = open.showOpenDialog(this); //verifikasi
  66.             // open dialog untuk memverifikasi kemuan user
  67.             if (option == JFileChooser.APPROVE_OPTION) {
  68.                 this.textArea.setText(""); //membersihkan text area
  69.                 try {
  70.                     //scanner untuk read filenya
  71.                     Scanner scan = new Scanner(new FileReader(open.getSelectedFile().getPath()));
  72.                     while (scan.hasNext()) // while masih ada yg diread
  73.                         this.textArea.append(scan.nextLine() + "\n"); //tampilkan di text area
  74.                 } catch (Exception ex) {
  75.                     System.out.println(ex.getMessage());
  76.                 }
  77.             }
  78.         }
  79.  
  80.         //opsi save
  81.         else if (e.getSource() == this.saveFile) {
  82.             JFileChooser save = new JFileChooser(); //pilih file
  83.             int option = save.showSaveDialog(this); //verifikasi
  84.             if (option == JFileChooser.APPROVE_OPTION) {
  85.                 try {
  86.                     //buffered writer
  87.                     BufferedWriter out = new BufferedWriter(new FileWriter(save.getSelectedFile().getPath()));
  88.                     out.write(this.textArea.getText()); //save
  89.                     out.close();
  90.                 } catch (Exception ex) {
  91.                     System.out.println(ex.getMessage());
  92.                 }
  93.             }
  94.         }
  95.  
  96.         else if (e.getSource() == this.newfile) {
  97.             textArea.setText(" ");
  98.         }
  99.     }
  100.  
  101.    // membuat notepad dan menampilkan program
  102.     public static void main(String args[]) {
  103.         Notepad app = new Notepad();
  104.         app.setVisible(true);  
  105.     }
  106. }
Advertisement
Add Comment
Please, Sign In to add comment