RickyS20_

Untitled

Dec 27th, 2020
147
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.01 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.   private MenuBar menuBar = new MenuBar(); // Buat MenuBar item
  10.     private Menu file = new Menu(); // File menu
  11.    
  12.     private MenuItem openFile = new MenuItem();  // an open option
  13.     private MenuItem saveFile = new MenuItem(); // a save option
  14.     private MenuItem close = new MenuItem(); // and a close option!
  15.  
  16.     public Notepad() {
  17.         this.setSize(1500, 1300); // set the initial size of the window
  18.         this.setTitle("Java Notepad"); // set the title of the window
  19.         setDefaultCloseOperation(EXIT_ON_CLOSE); // set the default close operation (exit when it gets closed)
  20.         this.textArea.setFont(new Font("Century Gothic", Font.BOLD, 14)); // set a default font for the TextArea
  21.         // this is why we didn't have to worry about the size of the TextArea!
  22.         this.getContentPane().setLayout(new BorderLayout()); // the BorderLayout bit makes it fill it automatically
  23.         this.getContentPane().add(textArea);
  24.  
  25.         // add our menu bar into the GUI
  26.         this.setMenuBar(this.menuBar);
  27.         this.menuBar.add(this.file); // we'll configure this later
  28.  
  29.         // first off, the design of the menuBar itself. Pretty simple, all we need to do
  30.         // is add a couple of menus, which will be populated later on
  31.         this.file.setLabel("File");
  32.  
  33.         // time for the repetitive stuff. let's add the "Open" option
  34.         this.openFile.setLabel("Open"); // set the label of the menu item
  35.         this.openFile.addActionListener(this); // add an action listener (so we know when it's been clicked
  36.         this.openFile.setShortcut(new MenuShortcut(KeyEvent.VK_O, false)); // set a keyboard shortcut
  37.         this.file.add(this.openFile); // add it to the "File" menu
  38.  
  39.         // and the save...
  40.         this.saveFile.setLabel("Save");
  41.         this.saveFile.addActionListener(this);
  42.         this.saveFile.setShortcut(new MenuShortcut(KeyEvent.VK_S, false));
  43.         this.file.add(this.saveFile);
  44.  
  45.         // and finally, the close option
  46.         this.close.setLabel("Close");
  47.         // along with our "CTRL+F4" shortcut to close the window, we also have
  48.         // the default closer, as stated at the beginning of this tutorial.
  49.         // this means that we actually have TWO shortcuts to close:
  50.         // 1) the default close operation (example, Alt+F4 on Windows)
  51.         // 2) CTRL+F4, which we are about to define now: (this one will appear in the label)
  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.         // if the source of the event was our "close" option
  59.         if (e.getSource() == this.close)
  60.             this.dispose(); // dispose all resources and close the application
  61.  
  62.         // if the source was the "open" option
  63.         else if (e.getSource() == this.openFile) {
  64.             JFileChooser open = new JFileChooser(); // open up a file chooser (a dialog for the user to browse files to open)
  65.             int option = open.showOpenDialog(this); // get the option that the user selected (approve or cancel)
  66.             // NOTE: because we are OPENing a file, we call showOpenDialog~
  67.             // if the user clicked OK, we have "APPROVE_OPTION"
  68.             // so we want to open the file
  69.             if (option == JFileChooser.APPROVE_OPTION) {
  70.                 this.textArea.setText(""); // clear the TextArea before applying the file contents
  71.                 try {
  72.                     // create a scanner to read the file (getSelectedFile().getPath() will get the path to the file)
  73.                     Scanner scan = new Scanner(new FileReader(open.getSelectedFile().getPath()));
  74.                     while (scan.hasNext()) // while there's still something to read
  75.                         this.textArea.append(scan.nextLine() + "\n"); // append the line to the TextArea
  76.                 } catch (Exception ex) { // catch any exceptions, and...
  77.                     // ...write to the debug console
  78.                     System.out.println(ex.getMessage());
  79.                 }
  80.             }
  81.         }
  82.  
  83.         // and lastly, if the source of the event was the "save" option
  84.         else if (e.getSource() == this.saveFile) {
  85.             JFileChooser save = new JFileChooser(); // again, open a file chooser
  86.             int option = save.showSaveDialog(this); // similar to the open file, only this time we call
  87.             // showSaveDialog instead of showOpenDialog
  88.             // if the user clicked OK (and not cancel)
  89.             if (option == JFileChooser.APPROVE_OPTION) {
  90.                 try {
  91.                     // create a buffered writer to write to a file
  92.                     BufferedWriter out = new BufferedWriter(new FileWriter(save.getSelectedFile().getPath()));
  93.                     out.write(this.textArea.getText()); // write the contents of the TextArea to the file
  94.                     out.close(); // close the file stream
  95.                 } catch (Exception ex) { // again, catch any exceptions and...
  96.                     // ...write to the debug console
  97.                     System.out.println(ex.getMessage());
  98.                 }
  99.             }
  100.         }
  101.     }
  102.    // the main method, for actually creating our notepad and setting it to visible.
  103.     public static void main(String args[]) {
  104.         Notepad app = new Notepad();
  105.         app.setVisible(true);
  106.    
  107.    
  108. }
  109. }
  110.  
  111.  
Advertisement
Add Comment
Please, Sign In to add comment