Jumat, 01 Agustus 2025

JAVA - MEMBUAT NOTEPAD

 


/*

 * JFrameLatihan2.java

 *

 * Created on 18 Agustus 2008, 12:03

 */




/**

 *

 * @author  Administrator

 */


import javax.swing.*;

import java.awt.*;

import java.io.*;

import java.awt.event.*;

import java.awt.datatransfer.*;

public class JFrameLatihan2 extends javax.swing.JFrame {

    boolean simpan=false;

    UIManager windows_GUI=new UIManager();

        /** Creates new form JFrameLatihan2 */

    public JFrameLatihan2() {

        super("Latihan Bab II");

        try {

            windows_GUI.setLookAndFeel(UIManager.

            getSystemLookAndFeelClassName());

        } 

        catch(Exception e) {

            e.printStackTrace();

        }

        initComponents();

        setResizable(false);

        file_open.addActionListener(new open_txt());

        file_save.addActionListener(new simpan());

        format_font.addActionListener(new gantiFont());

        format_back.addActionListener(new gantiBackground());

    }

   class open_txt implements ActionListener {

        public void actionPerformed(ActionEvent e) {

             File open_teks=new File("");

             pilih.setFileFilter(new ekstensi());

             int status=pilih.showOpenDialog(JFrameLatihan2.this);

             if(status==pilih.APPROVE_OPTION) {

                open_teks=pilih.getSelectedFile();

                 int end=(open_teks.getName().length())-4;

                 setTitle(open_teks.getName().substring(0,end));

                simpan=false;

                try {

                     tampil.setPage(open_teks.toURL());

                }

                 catch(IOException exc) {

                    throw new RuntimeException();

                }

           }


        }

    }

   class simpan implements ActionListener {

        public void actionPerformed(ActionEvent e) {

            BufferedWriter bufer;

            File simpan_teks=new File("");

            pilih.setFileFilter(new ekstensi());

            int s=pilih.showSaveDialog(JFrameLatihan2.this);

            if(s==pilih.APPROVE_OPTION) {

                try {

                      simpan_teks=pilih.getSelectedFile();

                      bufer=new BufferedWriter(new FileWriter((simpan_teks.toString()+".txt"),true));

                      tampil.write(bufer);

                      simpan=true;

                       setTitle(simpan_teks.getName());

                }

                catch (IOException x) {

                    JOptionPane.showMessageDialog(null,"Error");

                }

            }

        }

    }

   class gantiFont implements ActionListener {

        public void actionPerformed(ActionEvent e) {

            Color warna=pilih_warna.showDialog(null,"Font Color",Color.BLACK);

            tampil.setForeground(warna);

        }

    }

    class gantiBackground implements ActionListener {

        public void actionPerformed(ActionEvent e) {

            Color warna=pilih_warna.showDialog(null,"Backround Color",Color.BLACK);

            tampil.setBackground(warna);

        }

    }

    /** 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.

     */

    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents

    private void initComponents() {


        pilih = new javax.swing.JFileChooser();

        pilih_warna = new javax.swing.JColorChooser();

        jScrollPane1 = new javax.swing.JScrollPane();

        tampil = new javax.swing.JTextPane();

        jMenuBar1 = new javax.swing.JMenuBar();

        menu_file = new javax.swing.JMenu();

        file_open = new javax.swing.JMenuItem();

        file_save = new javax.swing.JMenuItem();

        file_exit = new javax.swing.JMenuItem();

        menu_format = new javax.swing.JMenu();

        format_font = new javax.swing.JMenuItem();

        format_back = new javax.swing.JMenuItem();

        menu_window = new javax.swing.JMenu();

        resize = new javax.swing.JCheckBoxMenuItem();

        top = new javax.swing.JCheckBoxMenuItem();


        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);


        jScrollPane1.setViewportView(tampil);


        menu_file.setMnemonic('F');

        menu_file.setText("File"); // NOI18N


        file_open.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_O, java.awt.event.InputEvent.CTRL_MASK));

        file_open.setText("Open"); // NOI18N

        file_open.addActionListener(new java.awt.event.ActionListener() {

            public void actionPerformed(java.awt.event.ActionEvent evt) {

                file_openActionPerformed(evt);

            }

        });

        menu_file.add(file_open);


        file_save.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_S, java.awt.event.InputEvent.CTRL_MASK));

        file_save.setText("Save"); // NOI18N

        file_save.addActionListener(new java.awt.event.ActionListener() {

            public void actionPerformed(java.awt.event.ActionEvent evt) {

                file_saveActionPerformed(evt);

            }

        });

        menu_file.add(file_save);


        file_exit.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_X, java.awt.event.InputEvent.CTRL_MASK));

        file_exit.setText("Exit"); // NOI18N

        file_exit.addActionListener(new java.awt.event.ActionListener() {

            public void actionPerformed(java.awt.event.ActionEvent evt) {

                file_exitActionPerformed(evt);

            }

        });

        menu_file.add(file_exit);


        jMenuBar1.add(menu_file);


        menu_format.setMnemonic('F');

        menu_format.setText("Format"); // NOI18N


        format_font.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_T, java.awt.event.InputEvent.CTRL_MASK));

        format_font.setText("Font Color");

        menu_format.add(format_font);


        format_back.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_G, java.awt.event.InputEvent.CTRL_MASK));

        format_back.setText("Background Color");

        menu_format.add(format_back);


        jMenuBar1.add(menu_format);


        menu_window.setMnemonic('W');

        menu_window.setText("Window"); // NOI18N


        resize.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_R, java.awt.event.InputEvent.CTRL_MASK));

        resize.setText("Resizable"); // NOI18N

        resize.addActionListener(new java.awt.event.ActionListener() {

            public void actionPerformed(java.awt.event.ActionEvent evt) {

                resizeActionPerformed(evt);

            }

        });

        menu_window.add(resize);


        top.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_W, java.awt.event.InputEvent.CTRL_MASK));

        top.setText("Always on Top"); // NOI18N

        top.addActionListener(new java.awt.event.ActionListener() {

            public void actionPerformed(java.awt.event.ActionEvent evt) {

                topActionPerformed(evt);

            }

        });

        menu_window.add(top);


        jMenuBar1.add(menu_window);


        setJMenuBar(jMenuBar1);


        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());

        getContentPane().setLayout(layout);

        layout.setHorizontalGroup(

            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)

            .addGroup(layout.createSequentialGroup()

                .addContainerGap()

                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 380, Short.MAX_VALUE)

                .addContainerGap())

        );

        layout.setVerticalGroup(

            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)

            .addGroup(layout.createSequentialGroup()

                .addContainerGap()

                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 257, Short.MAX_VALUE)

                .addContainerGap())

        );


        pack();

    }// </editor-fold>//GEN-END:initComponents


    private void file_openActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_file_openActionPerformed

        // TODO add your handling code here:

               

    }//GEN-LAST:event_file_openActionPerformed


    private void file_exitActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_file_exitActionPerformed

        // TODO add your handling code here:

        System.exit(0);

}//GEN-LAST:event_file_exitActionPerformed


    private void resizeActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_resizeActionPerformed

        // TODO add your handling code here:

        if (resize.isSelected()){

            setResizable(true);

}else

            setResizable(false);

    }//GEN-LAST:event_resizeActionPerformed


    private void topActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_topActionPerformed

        // TODO add your handling code here:

         if (top.isSelected()){

            setAlwaysOnTop(true);

}else

            setAlwaysOnTop(false);

}//GEN-LAST:event_topActionPerformed


    private void file_saveActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_file_saveActionPerformed

        // TODO add your handling code here:

        

}//GEN-LAST:event_file_saveActionPerformed

    

    /**

     * @param args the command line arguments

     */

    public static void main(String args[]) {

        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {

                new JFrameLatihan2().setVisible(true);

            }

        });

    }

    

    // Variables declaration - do not modify//GEN-BEGIN:variables

    private javax.swing.JMenuItem file_exit;

    private javax.swing.JMenuItem file_open;

    private javax.swing.JMenuItem file_save;

    private javax.swing.JMenuItem format_back;

    private javax.swing.JMenuItem format_font;

    private javax.swing.JMenuBar jMenuBar1;

    private javax.swing.JScrollPane jScrollPane1;

    private javax.swing.JMenu menu_file;

    private javax.swing.JMenu menu_format;

    private javax.swing.JMenu menu_window;

    private javax.swing.JFileChooser pilih;

    private javax.swing.JColorChooser pilih_warna;

    private javax.swing.JCheckBoxMenuItem resize;

    private javax.swing.JTextPane tampil;

    private javax.swing.JCheckBoxMenuItem top;

    // End of variables declaration//GEN-END:variables

    

}



======================================================================

/*

 * To change this template, choose Tools | Templates

 * and open the template in the editor.

 */

 

/**

 *

 * @author Administrator

 */

import javax.swing.filechooser.FileFilter;

import java.io.File;

    public class ekstensi extends FileFilter {

        public boolean accept(File file) {

            if(file.getName().endsWith(".txt") || file.isDirectory())

                return true;

            return false;

        }

        public String getDescription() {

            return ".txt";

        }

    }

===========================================

Tidak ada komentar: