Programming

Tutorial Membuat Aplikasi Notepad di Java

Hello guys,,,

Kali ini saya akan berbagi tentang bagaimana membuat aplikasi notepad di java.

Notepad
Notepad

Berikut ini source code nya :


/*
 * author Maulana Gempa Putra
 * Teknik Komputer Jaringan dan Media Digital - ITB
 */

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.util.Scanner;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.*;

/**
 *
 * @author gempa
 */
public class AppNotepad extends JFrame implements ActionListener {

    /**
     * @param args the command line arguments
     */
    //deklarasi textarea
    private TextArea textArea = new TextArea("",0,0,TextArea.SCROLLBARS_BOTH);

    //deklarasi menu bar
    private MenuBar menuAtas = new MenuBar();

    //menu header
    private Menu file = new Menu();
    private Menu help = new Menu();

    //menu item
    private MenuItem buka = new MenuItem();
    private MenuItem simpan = new MenuItem();
    private MenuItem tentang = new MenuItem();
    private MenuItem tutup = new MenuItem();

    public AppNotepad()
    {
        this.setSize(640,480);
        this.setTitle("Aplikasi Notepad by Maulana Gempa Putra");
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        this.textArea.setFont(new Font("calibri",Font.BOLD,12));
        this.getContentPane().setLayout(new BorderLayout());
        this.getContentPane().add(textArea);
        this.setMenuBar(this.menuAtas);
        this.menuAtas.add(file);
        this.file.setLabel("File");
        this.buka.setLabel("Open New File");
        this.buka.addActionListener(this);
        this.file.add(this.buka);

        this.simpan.setLabel("Save as..");
        this.simpan.addActionListener(this);
        this.file.add(simpan);

        this.tutup.setLabel("Close");
        this.tutup.addActionListener(this);
        this.file.add(tutup);

        this.menuAtas.add(help);
        this.help.setLabel("Help");
        this.tentang.setLabel("About us");
        this.tentang.addActionListener(this);
        this.help.add(this.tentang);

    }

    @Override
    public void actionPerformed(ActionEvent e) {
        if(e.getSource() == this.tutup)
        {
            this.dispose();
        }
        else if (e.getSource() == this.buka)
        {
            JFileChooser open = new JFileChooser();
            int option = open.showOpenDialog(this);
            if(option == JFileChooser.APPROVE_OPTION)
            {
                this.textArea.setText("");
                try
                {
                    Scanner scan = new Scanner(new FileReader(open.getSelectedFile().getPath()));
                    while (scan.hasNext())
                    {
                        this.textArea.append(scan.nextLine() + "\n");
                    }
                }
                catch (Exception ex)
                        {
                            JOptionPane.showMessageDialog(rootPane, ex,"Error",0);
                        }

            }

        }
        else if(e.getSource() == this.simpan)
        {
            JFileChooser save = new JFileChooser();
            int option = save.showSaveDialog(this);
            if(option == JFileChooser.APPROVE_OPTION)
            {
               try
               {
                   BufferedWriter out = new BufferedWriter(new FileWriter(save.getSelectedFile().getPath()));
                   out.write(this.textArea.getText());
                   out.close();
               }
               catch (Exception ex)
               {
                   JOptionPane.showMessageDialog(rootPane, ex,"Error",0);
               }

            }
        }
        else if (e.getSource() == this.tentang)
        {
            JOptionPane.showMessageDialog(rootPane, "Terima Kasih Telah Menggunakan Aplikasi ini, by Maulana Gempa Putra","Terima Kasih",1);
        }
    }

    public static void main(String[] args) throws ClassNotFoundException, InstantiationException, IllegalAccessException {
        //GUI menjadi look and feel

        try {
            UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
        } catch (UnsupportedLookAndFeelException ex) {
            Logger.getLogger(AppNotepad.class.getName()).log(Level.SEVERE, null, ex);
        }

        AppNotepad an = new AppNotepad();
        an.setVisible(true);
        an.setLocationRelativeTo(null);

    }
}

untuk lebih jelasnya, saya juga melampirkan tutorial dalam bentuk video nya :

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s