membuat notepad dengan java

waduh masa belajar java cuma buat hello word trus nih,,
ga ada contoh yang lain apa??? lagi mikir yang kaya gituan eh, kepikiran ,masa ga ada contoh lain di internet,,
hahaha,, mulai deh jari jari lentik nan indah serta sedikit nakal saya mengetikkan berbagai kata kunci nan ajaib bagaikan mantra..
tapi ga jauh jauh dari java,,hehehe,,
tapi inget program hello word  adalah program dasar dan hampir di semua bahasa program ada (kata dosen OOP),tiba tiba,,,,,,zzzzzzzzzzzzzzzzzzzzz,,,,,,,,,  nemu deh "sekrip" menarik nan anggun,, apaan si itu??
kalo mau tau ini dia script itu,, eng ing eng...
// File   : editor/NutPad.java -- A very simple text editor
// Purpose: Illustrates use of AbstractActions for menus.
//          It only uses a few Action features.  Many more are available.
//          This program uses the obscure "read" and "write"
//               text component methods.
// Author : Fred Swartz - 2006-12-14 - Placed in public domain.

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

///////////////////////////////////////////////////////////////////////// godaipad
public class godaipad extends JFrame {
    //... Components
    private JTextArea    _editArea;
    private JFileChooser _fileChooser = new JFileChooser();
   
    //... Create actions for menu items, buttons, ...
    private Action _openAction = new OpenAction();
    private Action _saveAction = new SaveAction();
    private Action _exitAction = new ExitAction();
   
    //===================================================================== main
    public static void main(String[] args) {
        new godaipad();
    }
   
    //============================================================== constructor
    public godaipad() {
        //... Create scrollable text area.
        _editArea = new JTextArea(15, 80);
        _editArea.setBorder(BorderFactory.createEmptyBorder(2,2,2,2));
        _editArea.setFont(new Font("monospaced", Font.PLAIN, 14));
        JScrollPane scrollingText = new JScrollPane(_editArea);
       
        //-- Create a content pane, set layout, add component.
        JPanel content = new JPanel();
        content.setLayout(new BorderLayout());
        content.add(scrollingText, BorderLayout.CENTER);
       
        //... Create menubar
        JMenuBar menuBar = new JMenuBar();
        JMenu fileMenu = menuBar.add(new JMenu("File"));
        fileMenu.setMnemonic('F');
        fileMenu.add(_openAction);       // Note use of actions, not text.
        fileMenu.add(_saveAction);
        fileMenu.addSeparator();
        fileMenu.add(_exitAction);
       
        //... Set window content and menu.
        setContentPane(content);
        setJMenuBar(menuBar);
       
        //... Set other window characteristics.
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setTitle("godaipad");
        pack();
        setLocationRelativeTo(null);
        setVisible(true);
    }
   
    ////////////////////////////////////////////////// inner class OpenAction
    class OpenAction extends AbstractAction {
        //============================================= constructor
        public OpenAction() {
            super("Open...");
            putValue(MNEMONIC_KEY, new Integer('O'));
        }
       
        //========================================= actionPerformed
        public void actionPerformed(ActionEvent e) {
            int retval = _fileChooser.showOpenDialog(godaipad.this);
            if (retval == JFileChooser.APPROVE_OPTION) {
                File f = _fileChooser.getSelectedFile();
                try {
                    FileReader reader = new FileReader(f);
                    _editArea.read(reader, "");  // Use TextComponent read
                } catch (IOException ioex) {
                    System.out.println(e);
                    System.exit(1);
                }
            }
        }
    }
   
    //////////////////////////////////////////////////// inner class SaveAction
    class SaveAction extends AbstractAction {
        //============================================= constructor
        SaveAction() {
            super("Save...");
            putValue(MNEMONIC_KEY, new Integer('S'));
        }
       
        //========================================= actionPerformed
        public void actionPerformed(ActionEvent e) {
            int retval = _fileChooser.showSaveDialog(godaipad.this);
            if (retval == JFileChooser.APPROVE_OPTION) {
                File f = _fileChooser.getSelectedFile();
                try {
                    FileWriter writer = new FileWriter(f);
                    _editArea.write(writer);  // Use TextComponent write
                } catch (IOException ioex) {
                    JOptionPane.showMessageDialog(godaipad.this, ioex);
                    System.exit(1);
                }
            }
        }
    }
   
    ///////////////////////////////////////////////////// inner class ExitAction
    class ExitAction extends AbstractAction {
       
        //============================================= constructor
        public ExitAction() {
            super("Exit");
            putValue(MNEMONIC_KEY, new Integer('X'));
        }
       
        //========================================= actionPerformed
        public void actionPerformed(ActionEvent e) {
            System.exit(0);
        }
    }
}



perlu di perhatikan,, ini bukan murni buatan saya lho,, yang berwarna merah itu adalah nama pembuatnya mungkin yah,, saya cuma share aja supaya bagi rekan rekan yang bosan dengan hello word dan ingin mencari tahu yang lain bisa memanfaatkannya juga,, dan satu hal yang harus di perhatikan lagi,jika ingin mengubah kelas dan nama notepad ini, coba lihat bagian yang berwarna orange sebelumnya bernama nutpad, jangan sampai salah,,
dan godaipad ini saya buat dengan menggunakan blueJ,,
semoga bermanfaat..
pada posting berikutnya ada skrip untuk membuat jam digital java,, hehehe...
Previous
Next Post »

1 komentar:

Write komentar