Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import javax.swing.*;
- import java.awt.*;
- import java.awt.event.*;
- import java.util.Scanner;
- import java.io.*;
- public class Notepad extends JFrame implements ActionListener {
- private TextArea textArea = new TextArea("", 0,0, TextArea.SCROLLBARS_VERTICAL_ONLY);
- // membuat menu bar
- private MenuBar menuBar = new MenuBar();
- private Menu file = new Menu(); // menubar file
- private MenuItem newfile = new MenuItem(); //program new
- private MenuItem openFile = new MenuItem(); // program open
- private MenuItem saveFile = new MenuItem(); // program save
- private MenuItem close = new MenuItem(); // program close
- public Notepad() {
- this.setSize(500, 300); //setting ukuran dari windownya
- this.setTitle("Java Notepad Sederhana"); //nama program text editor
- setDefaultCloseOperation(EXIT_ON_CLOSE); //mengatur pengaturan default close untuk exit
- this.textArea.setFont(new Font("Century Gothic", Font.BOLD, 12)); //penentuan font
- this.getContentPane().setLayout(new BorderLayout());
- this.getContentPane().add(textArea);
- //menampulkan menubar pada GUI
- this.setMenuBar(this.menuBar);
- this.menuBar.add(this.file);
- this.file.setLabel("File");
- this.newfile.setLabel("New");
- this.newfile.addActionListener(this);
- this.newfile.setShortcut(new MenuShortcut(KeyEvent.VK_N, false));
- this.file.add(this.newfile);
- this.openFile.setLabel("Open"); //menentukan tablenya
- this.openFile.addActionListener(this); //untuk responsif
- this.openFile.setShortcut(new MenuShortcut(KeyEvent.VK_O, false)); //setting keyboard shortcut
- this.file.add(this.openFile); // tambahkan ke menu "File"
- this.saveFile.setLabel("Save");
- this.saveFile.addActionListener(this);
- this.saveFile.setShortcut(new MenuShortcut(KeyEvent.VK_S, false));
- this.file.add(this.saveFile);
- this.close.setLabel("Close");
- this.close.setShortcut(new MenuShortcut(KeyEvent.VK_F4, false));
- this.close.addActionListener(this);
- this.file.add(this.close);
- }
- public void actionPerformed (ActionEvent e) {
- //opsi close
- if (e.getSource() == this.close)
- this.dispose(); //menutup program
- //opsi open
- else if (e.getSource() == this.openFile) {
- JFileChooser open = new JFileChooser(); //membuka file dari browser file
- int option = open.showOpenDialog(this); //verifikasi
- // open dialog untuk memverifikasi kemuan user
- if (option == JFileChooser.APPROVE_OPTION) {
- this.textArea.setText(""); //membersihkan text area
- try {
- //scanner untuk read filenya
- Scanner scan = new Scanner(new FileReader(open.getSelectedFile().getPath()));
- while (scan.hasNext()) // while masih ada yg diread
- this.textArea.append(scan.nextLine() + "\n"); //tampilkan di text area
- } catch (Exception ex) {
- System.out.println(ex.getMessage());
- }
- }
- }
- //opsi save
- else if (e.getSource() == this.saveFile) {
- JFileChooser save = new JFileChooser(); //pilih file
- int option = save.showSaveDialog(this); //verifikasi
- if (option == JFileChooser.APPROVE_OPTION) {
- try {
- //buffered writer
- BufferedWriter out = new BufferedWriter(new FileWriter(save.getSelectedFile().getPath()));
- out.write(this.textArea.getText()); //save
- out.close();
- } catch (Exception ex) {
- System.out.println(ex.getMessage());
- }
- }
- }
- else if (e.getSource() == this.newfile) {
- textArea.setText(" ");
- }
- }
- // membuat notepad dan menampilkan program
- public static void main(String args[]) {
- Notepad app = new Notepad();
- app.setVisible(true);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment