Administrator
Notepad Merupakan aplikasi kecil dari Windows yang dapat digunakan sebagai text editor sederhana. Aplikasi ini mempunyai fungsi fungsi dasar seperti membuat teks baru, menyimpan, memanggil file teks yang telah ada, dan melakukan percetakan.
Tip kali ini adalah membuat notepad dengan 3 fungsi utama, yaitu membuat teks baru, menyimpan teks, dan memanggil teks yang sudah ada. Beberapa komponen yang dibutuhkan untuk aplikasi ini , antara lain JtextArea, JScrollPane dan JMenuBar.
Langkah2 untuk membuat menu bar dan pop-up menu adalah sebagai berikut.
1. Library yang perlu di impor adalah java.awt dan java.awt.evet. Library awt digunakan untuk membuat frame, textarea, dan menu, sedangkan java.awt.event digunakan menangani event event yang terjdadi
import java.awt.*;import java.awt.event.*;import java.io.*;
public class SimplePad extends Frame { private TextArea tx; private MenuBar mb; private String fn;
public SimplePad() { setLayout(new GridLayout(1,1)); tx = new TextArea(); add(tx); mb = new MenuBar(); Menu F = new Menu("File"); MenuItem n = new MenuItem("New"); MenuItem o = new MenuItem("Open"); MenuItem s = new MenuItem("Save"); MenuItem e = new MenuItem("Exit"); n.addActionListener(new New()); F.add(n); o.addActionListener(new Open());F.add(o); s.addActionListener(new Save());F.add(s); e.addActionListener(new Exit());F.add(e); mb.add(F); setMenuBar(mb); }
class New implements ActionListener { public void actionPerformed(ActionEvent e) { tx.setText(" "); setTitle(fn); } }
class Open implements ActionListener { public void actionPerformed(ActionEvent e) { FileDialog fd = new FileDialog(SimplePad.this, "select File",FileDialog.LOAD); fd.show(); if (fd.getFile()!=null) { fn = fd.getDirectory() + fd.getFile(); setTitle(fn); ReadFile(); } tx.requestFocus(); } } void ReadFile() { BufferedReader d; StringBuffer sb = new StringBuffer(); try { d = new BufferedReader(new FileReader(fn)); String line; while((line=d.readLine())!=null) sb.append(line + "\n"); tx.setText(sb.toString()); d.close(); } catch(FileNotFoundException fe) { System.out.println("File not Found"); } catch(IOException ioe){} }
6. Implementasi dari menu Save adalah menyimpan text yang ada di textarea kedalam suatu file:
class Save implements ActionListener { public void actionPerformed(ActionEvent e) { FileDialog fd = new FileDialog(SimplePad.this,"Save File",FileDialog.SAVE); fd.show(); if (fd.getFile()!=null) { fn = fd.getDirectory() + fd.getFile(); setTitle(fn); try { DataOutputStream d = new DataOutputStream(new FileOutputStream(fn)); String line = tx.getText(); BufferedReader br = new BufferedReader(new StringReader(line)); while((line = br.readLine())!=null) { d.writeBytes(line + "\r\n"); d.close(); } } catch(Exception ex) { System.out.println("Error occurer"); } tx.requestFocus(); } } }
7. Terakhir adalah implementasi dari menu Exit, untuk mengakhiri program:
class Exit implements ActionListener { public void actionPerformed(ActionEvent e) { System.exit(0); } }
8. Untuj program utama, kita deklarasikan objek pad menggunaakan simple pad.
public static void main(String args[]) { SimplePad pad = new SimplePad(); pad.setSize(300,300); pad.setVisible(true); } }9. Jika dijalankan...wow.. nicee
0 komentar:
Posting Komentar
Dimohon Komentarnya...
1 Komentar Anda Sangat Berarti Untuk Blog Saya...