Senin, 30 Juli 2012

Aplikasi Notepad dalam Java

import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.io.*;
import java.util.Scanner;

public class Notepads extends JFrame
{
    Container konten = getContentPane();
    private JMenuBar menuBaris = new JMenuBar();
    private JMenu menuFile = new JMenu("File");
    private JMenu menuAbout = new JMenu("About");
    private JMenuItem miApplication = new JMenuItem("Application");
    private JMenuItem miSave = new JMenuItem("Save");
    private JMenuItem miOpen = new JMenuItem("Open");
    private JMenuItem miClose = new JMenuItem("Close");
    private JTextArea textArea = new JTextArea("",0,0);
    private JFileChooser fc = new JFileChooser();
   
     //Konstuktor
    public Notepads()
    {
        setTitle("Java Notepad");
        setSize(700,600);
        setVisible(true);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setLocationRelativeTo(null);
       
         setJMenuBar(menuBaris);
            menuBaris.add(menuFile);
                menuFile.add(miSave);
                    miSave.setMnemonic(KeyEvent.VK_S); //Membuat nama shortcut
                    miSave.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_S,ActionEvent.CTRL_MASK)); //Membuat shorcut Keyboard
                menuFile.add(miOpen);
                    menuFile.addSeparator(); //Menambahkan garis separator
                    miOpen.setMnemonic(KeyEvent.VK_O);
                    miOpen.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_O,ActionEvent.CTRL_MASK));
                menuFile.add(miClose);
                    miClose.setMnemonic(KeyEvent.VK_C);
                    miClose.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_C,ActionEvent.CTRL_MASK));
            menuBaris.add(menuAbout);
                menuAbout.add(miApplication);
                    miApplication.setMnemonic(KeyEvent.VK_A);
                    miApplication.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_A,ActionEvent.CTRL_MASK));
      
      konten.add(textArea);
      textArea.setFont(new Font("Arial",Font.BOLD,14)); //Mengatur Fonts
      textArea.setLineWrap(true);
     
      miOpen.addActionListener(new ActionListener()
      {
         public void actionPerformed(ActionEvent act)
         {
             int open = fc.showOpenDialog(textArea);
             if(open == JFileChooser.APPROVE_OPTION)
             {
                 textArea.setText("");
                 try
                 {
                   Scanner scan = new Scanner(new FileReader(fc.getSelectedFile().getPath())); //Membuat scanner membaca file yang dibuka
                   while(scan.hasNext())
                   {
                       textArea.append(scan.nextLine());
                   }
                 }
                 catch(Exception ex)
                 {
                     System.out.println(ex);
                 }
             }
         }
      });
     
      miSave.addActionListener(new ActionListener()
      {
         public void actionPerformed(ActionEvent act)
         {
             int save = fc.showOpenDialog(textArea);
             if(save == JFileChooser.APPROVE_OPTION)
             {
                 try
                 {
                     BufferedWriter bw = new BufferedWriter(new FileWriter(fc.getSelectedFile().getPath())); //Menulis File yang kita buka di TextArea
                     bw.write(textArea.getText());
                     bw.close();
                 }
                 catch(Exception ex)
                 {
                     System.out.println(ex);
                 }
             }
         }
      });
     
      miClose.addActionListener(new ActionListener()
      {
          public void actionPerformed(ActionEvent act)
          {
           dispose(); //Keluar dari aplikasi
          }
      });
     
      miApplication.addActionListener(new ActionListener()
      {
          public void actionPerformed(ActionEvent act)
          {
            JOptionPane.showMessageDialog(null,"Java Notepad","Message",JOptionPane.INFORMATION_MESSAGE);
          }
      });
      
       
    }//Akhir Konstruktor
   
    public static void main(String[] ar)
    {
        new Notepads();
    }
}

0 komentar:

Posting Komentar

◄ Newer Post Older Post ►
 

Copyright 2011 Something is proudly powered by blogger.com | Design by Tutorial Blogspot Published by Template Blogger