Java - Notepad Sederhana

Untuk menu strip edit dan about belum  saya kasi coding, jadi sementara cuma file saja sahabat.
untuk membuat seperti diatas buatlah designnya dulu seperti gambar dibawa
Gunakan Palette atau swing untuk membuat design diatas

Masukan Code seperti dibawa ini dengan asumsi bahwa nama2 swing yang sahabat buat seperti yang saya buat jika tidak maka editlah


Codenya


/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package notepad;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;

/**
 *
 * @author Moch. Afif
 */
public class home extends javax.swing.JFrame {
    String Tampil, Lokasi;
    private File File;
    private boolean Saved = true;
    /**
     * Creates new form home
     */
    public home() {
        initComponents();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">                        
    private void initComponents() {

        jDialog1 = new javax.swing.JDialog();
        jDialog2 = new javax.swing.JDialog();
        jDialog3 = new javax.swing.JDialog();
        jMenuItem5 = new javax.swing.JMenuItem();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTextArea1 = new javax.swing.JTextArea();
        jMenuBar1 = new javax.swing.JMenuBar();
        jMenu1 = new javax.swing.JMenu();
        jMenuItem4 = new javax.swing.JMenuItem();
        jMenuItem1 = new javax.swing.JMenuItem();
        jMenuItem2 = new javax.swing.JMenuItem();
        jMenuItem6 = new javax.swing.JMenuItem();
        jSeparator1 = new javax.swing.JPopupMenu.Separator();
        jMenuItem3 = new javax.swing.JMenuItem();
        jMenu2 = new javax.swing.JMenu();
        jMenu3 = new javax.swing.JMenu();

        javax.swing.GroupLayout jDialog1Layout = new javax.swing.GroupLayout(jDialog1.getContentPane());
        jDialog1.getContentPane().setLayout(jDialog1Layout);
        jDialog1Layout.setHorizontalGroup(
            jDialog1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 400, Short.MAX_VALUE)
        );
        jDialog1Layout.setVerticalGroup(
            jDialog1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 300, Short.MAX_VALUE)
        );

        javax.swing.GroupLayout jDialog2Layout = new javax.swing.GroupLayout(jDialog2.getContentPane());
        jDialog2.getContentPane().setLayout(jDialog2Layout);
        jDialog2Layout.setHorizontalGroup(
            jDialog2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 400, Short.MAX_VALUE)
        );
        jDialog2Layout.setVerticalGroup(
            jDialog2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 300, Short.MAX_VALUE)
        );

        javax.swing.GroupLayout jDialog3Layout = new javax.swing.GroupLayout(jDialog3.getContentPane());
        jDialog3.getContentPane().setLayout(jDialog3Layout);
        jDialog3Layout.setHorizontalGroup(
            jDialog3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 400, Short.MAX_VALUE)
        );
        jDialog3Layout.setVerticalGroup(
            jDialog3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 300, Short.MAX_VALUE)
        );

        jMenuItem5.setText("jMenuItem5");

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jTextArea1.setColumns(20);
        jTextArea1.setRows(5);
        jScrollPane1.setViewportView(jTextArea1);

        jMenu1.setText("File");

        jMenuItem4.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_N, java.awt.event.InputEvent.CTRL_MASK));
        jMenuItem4.setText("Baru");
        jMenuItem4.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem4ActionPerformed(evt);
            }
        });
        jMenu1.add(jMenuItem4);

        jMenuItem1.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_O, java.awt.event.InputEvent.CTRL_MASK));
        jMenuItem1.setText("Buka");
        jMenuItem1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem1ActionPerformed(evt);
            }
        });
        jMenu1.add(jMenuItem1);

        jMenuItem2.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_S, java.awt.event.InputEvent.CTRL_MASK));
        jMenuItem2.setText("Simpan");
        jMenuItem2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem2ActionPerformed(evt);
            }
        });
        jMenu1.add(jMenuItem2);

        jMenuItem6.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_S, java.awt.event.InputEvent.SHIFT_MASK | java.awt.event.InputEvent.CTRL_MASK));
        jMenuItem6.setText("Simpan sebagai....");
        jMenuItem6.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem6ActionPerformed(evt);
            }
        });
        jMenu1.add(jMenuItem6);
        jMenu1.add(jSeparator1);

        jMenuItem3.setText("Exit");
        jMenuItem3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem3ActionPerformed(evt);
            }
        });
        jMenu1.add(jMenuItem3);

        jMenuBar1.add(jMenu1);

        jMenu2.setText("Edit");
        jMenuBar1.add(jMenu2);

        jMenu3.setText("About");
        jMenuBar1.add(jMenu3);

        setJMenuBar(jMenuBar1);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 400, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 279, Short.MAX_VALUE)
        );

        pack();
    }// </editor-fold>                      

    private void jMenuItem1ActionPerformed(java.awt.event.ActionEvent evt) {                                        
        // TODO add your handling code here:
       buka();
    }                                        

    private void jMenuItem4ActionPerformed(java.awt.event.ActionEvent evt) {                                        
        // TODO add your handling code here:
        baru();
    }                                        

    private void jMenuItem2ActionPerformed(java.awt.event.ActionEvent evt) {                                        
        // TODO add your handling code here:
        if (File == null)
        {
            simas();
        }
        else
        {
            simpan();
        }
    }                                        

    private void jMenuItem6ActionPerformed(java.awt.event.ActionEvent evt) {                                        
        // TODO add your handling code here:
        simas();
    }                                        

    private void jMenuItem3ActionPerformed(java.awt.event.ActionEvent evt) {                                        
        // TODO add your handling code here:
        exit();
    }                                        

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(home.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(home.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(home.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(home.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new home().setVisible(true);
            }
        });
    }
 
    public void baru()
    {
        if (jTextArea1.getText().equals("") || jTextArea1.getText().equals(Tampil))
        {
            jTextArea1.setText("");
            Tampil = "";
            Lokasi = "";
            setTitle("Belum ada judul file - Notepad Saeful");
        }
        else
        {
            int i = JOptionPane.showConfirmDialog(null,"Isi file telah perubah!\nApakah anda ingin menyimpan perubahan?");
            if (i == 0)
                simpan();
            else if (i == 1)
            {
                jTextArea1.setText("");
                Lokasi = "";
                setTitle("Belum ada judul file - Notepad Saeful");
            }
            else if (i == 2)
                return;
        }
    }
    public void buka()
    {
        JFileChooser filechooser = new JFileChooser();
        filechooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
        int i = filechooser.showOpenDialog(this);
        if (i == filechooser.CANCEL_OPTION)
            return;
        File BukaFile = filechooser.getSelectedFile();
        if (BukaFile == null || BukaFile.getName().equals(""))
        {
            JOptionPane.showMessageDialog(this, "Pilih sebuah file!", "Error", JOptionPane.ERROR_MESSAGE);
            return;
        }
        try {
            BufferedReader bufferedreader = new BufferedReader(new FileReader(BukaFile));
            StringBuffer stringbuffer = new StringBuffer();
            String Baris;
            while((Baris = bufferedreader.readLine()) != null)stringbuffer.append(Baris+"\n");
            jTextArea1.setText(stringbuffer.toString());
            Tampil = jTextArea1.getText();
            Lokasi = BukaFile.toString();
            setTitle(BukaFile.getName()+"- Notepad Saeful");
        } catch (FileNotFoundException ex) {
            JOptionPane.showMessageDialog(null, "File tidak ditemukan:"+ ex);
        } catch (IOException ex)
        {
            JOptionPane.showMessageDialog(null, "IO Error:"+ ex);
        }
    }
    public void simpan()
    {
        if (File != null)
        {
            FileWriter filewriter = null;
            try {
                filewriter = new FileWriter(File);
                filewriter.write(jTextArea1.getText());
                filewriter.flush();
                Saved = true;
            } catch (IOException ex) {
                JOptionPane.showMessageDialog(this, "Terjadi kesalahan!\nTidak dapat disimpan");
            } finally
            {
                try {
                    filewriter.close();
                } catch (IOException ex) {
                    ex.printStackTrace();
                }
            }
        }
        else
        {
            simas();
        }
    }
    public void simas()
    {
        JFileChooser filechooser = new JFileChooser(".");
        int i = filechooser.showSaveDialog(this);
        if (i == JFileChooser.APPROVE_OPTION)
        {
            File = new File(filechooser.getSelectedFile().getPath()+".txt");
            setTitle(File.getPath());
            simpan();
        }
    }
    public void exit()
    {
         if (jTextArea1.getText().equals("") || jTextArea1.getText().equals(Tampil))
            System.exit(0);
        else
        {
            int i = JOptionPane.showConfirmDialog(null, "Isi file belum disimpan atau ada perubahan!\nApakah anda ingin menyimpan perubahan?");
            if (i == 0)
                simpan();
            else if (i == 1)
                System.exit(0);
            else if (i == 2)
                return;
        }
    }
    // Variables declaration - do not modify                  
    private javax.swing.JDialog jDialog1;
    private javax.swing.JDialog jDialog2;
    private javax.swing.JDialog jDialog3;
    private javax.swing.JMenu jMenu1;
    private javax.swing.JMenu jMenu2;
    private javax.swing.JMenu jMenu3;
    private javax.swing.JMenuBar jMenuBar1;
    private javax.swing.JMenuItem jMenuItem1;
    private javax.swing.JMenuItem jMenuItem2;
    private javax.swing.JMenuItem jMenuItem3;
    private javax.swing.JMenuItem jMenuItem4;
    private javax.swing.JMenuItem jMenuItem5;
    private javax.swing.JMenuItem jMenuItem6;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JPopupMenu.Separator jSeparator1;
    private javax.swing.JTextArea jTextArea1;
    // End of variables declaration                
}

Jika ingin edit program saya downlod Disini source codenya

Comments

Popular posts from this blog

VB.NET - Membuat Angka Acak

Kenapa dinamai windows?

Konversi Desimal ke Biner Menggunakan Stack - Java