Menerapkan notepad sederhana dengan menggunakan ayunan dan IO Java, mengimplementasikan pembukaan file teks teks yang ditentukan, kemudian memuat konten file teks ke dalam komponen ayunan, dan kemudian mengedit konten notepad dalam komponen ayunan, dan kemudian menyimpan opsi di dalam Menu yang sama menyimpan konten yang diedit ke file teks. Kodenya adalah sebagai berikut:
Salinan kode adalah sebagai berikut:
impor java.io.*;
impor java.nio.*;
impor java.nio.channels.*;
impor java.nio.charset.*;
impor java.awt.*;
impor java.awt.event.*;
impor javax.swing.joptionpane;
/**
*Fungsi: notebook sederhana
*2011-12-25
*/
kelas publik Simplenotepad mengimplementasikan ActionListener {
bingkai bingkai pribadi;
FileDialog Private FD_LOAD;
FileDialog Private FD_SAVE;
TEPTAREA TA Pribadi;
private string file = "";
save menuitem pribadi;
RAF Private RandomAccessFile;
FCI FileChannel Pribadi;
kawanan filelock pribadi;
Encoder Charsetencoder Pribadi;
Decoder CharsetDecoder Pribadi;
public static void main (string args []) {
new SimpleNotepad (). init ();
}
public void init () {
bingkai = bingkai baru ("notepad saya");
Menubar MB = Menubar baru ();
File menu = menu baru ("file");
Menu help = menu baru ("bantuan");
Menuitem Open = New Menuitem ("Open");
save = new Menuitem ("save");
save.setenabled (false);
file.add (buka);
file.add (simpan);
mb.add (file);
mb.add (bantuan);
frame.setMenubar (MB);
ta = TexTarea baru ();
frame.add (ta, "center");
open.addactionListener (ini);
save.addactionListener (ini);
frame.addwindowlistener (windowadapter baru () {
public void windowclosing (windowevent e) {
System.exit (0);
}
});
frame.setsize (600.400);
frame.setlocation (300.100);
frame.setVisible (true);
fd_load = new filealog (bingkai, "file buka", filedialog.load);
fd_save = new filedialog (bingkai, "simpan file", filedialog.save);
Charset charset = charset.forname (System.getProperty ("File.encoding"));
encoder = charset.newencoder ();
decoder = charset.newDecoder ();
}
public void actionPerformed (actionEvent e) {
String s = e.getactionCommand ();
if (s.equals ("open")) {
fd_load.setVisible (true);
String d = fd_load.getDirectory ();
String f = fd_load.getFile ();
if ((d! = null) && (f! = null)) {
String destfile = d + f;
if (destfile.equals (file)) {
kembali;
}kalau tidak{
this.closefile ();
file = destfile;
this.loadFile ();
}
}
} lain jika (s.equals ("save")) {
this.savefile ();
}
}
public void loadFile () {
mencoba{
RAF = RandomAccessFile baru (file, "rw");
fci = raf.getchannel ();
flock = fci.trylock ();
if (flock == null) {
ta.setText ("");
JOPTIONPANE.SHOWMESSAGEIALOG (NULL,
"File sedang digunakan dan tidak dapat dibuka secara eksklusif!",
"ERROR FRECTE", JOPTIONPANE.Error_Message);
file = "";
raf.close ();
raf = null;
}kalau tidak{
int panjang = (int) fci.size ();
ByteBuffer bb = bytebuffer.allocate (panjang);
fci.read (bb);
bb.flip ();
Charbuffer cb = decoder.decode (bb);
ta.setText (cb.tostring ());
frame.settitle ("My Notepad -" + File);
save.setenabled (true);
}
} catch (ioException e) {
e.printstacktrace ();
}
}
public void saveFile () {
String content = ta.getText ();
mencoba{
Charbuffer cb = charbuffer.wrap (content.tochararray ());
Bytebuffer bb = encoder.encode (cb);
raf.setlength (0);
FCI.Write (BB);
fci.force (true);
} catch (ioException e) {
e.printstacktrace ();
}
}
public void closeFile () {
mencoba{
if (flock! = null) {
flock.release ();
}
if (raf! = null) {
raf.close ();
}
file = "";
frame.settitle ("My Notepad");
save.setenabled (false);
} catch (ioException e) {
e.printstacktrace ();
}
}
}
Gambar reproduksi:
Di atas adalah semua tentang artikel ini, saya harap Anda menyukainya.