Implement a simple Notepad by using the Java Swing, Io, to open the specified text file, then load the contents of the text file into the swing component, and then edit the Notepad contents in the Swing component. You then save the edited content to the text file with the Save option for the menu. The code is as follows:
code as follows:
Import java.io.*;
Import java.nio.*;
Import java.nio.channels.*;
Import java.nio.charset.*;
Import java.awt.*;
Import java.awt.event.*;
Import Javax.swing.JOptionPane;
/**
* Function: Simple Notepad
*2011-12-25
*/
public class Simplenotepad implements actionlistener{
private frame frame;
Private FileDialog fd_load;
Private FileDialog Fd_save;
Private TextArea ta;
Private String file = "";
Private MenuItem Save;
Private Randomaccessfile RAF;
Private FileChannel FCI;
Private Filelock flock;
Private Charsetencoder encoder;
Private Charsetdecoder decoder;
public static void Main (String args[]) {
New Simplenotepad (). Init ();
}
public void init () {
frame = new Frame ("My Notepad");
MenuBar MB = new MenuBar ();
Menu file = new Menu ("file");
Menu aid = new menu (Help);
MenuItem open = new MenuItem ("open");
Save = new MenuItem ("Save");
Save.setenabled (FALSE);
File.add (open);
File.add (save);
Mb.add (file);
Mb.add (Help);
Frame.setmenubar (MB);
Ta = new TextArea ();
Frame.add (TA, "Center");
Open.addactionlistener (this);
Save.addactionlistener (this);
Frame.addwindowlistener (New Windowadapter () {
public void windowclosing (WindowEvent e) {
System.exit (0);
}
});
Frame.setsize (600,400);
Frame.setlocation (300,100);
Frame.setvisible (TRUE);
Fd_load = new FileDialog (frame, "Open file", filedialog.load);
Fd_save = new FileDialog (frame, "Save 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)) {
Return
}else{
This.closefile ();
File = DestFile;
This.loadfile ();
}
}
}else if (s.equals ("Save")) {
This.savefile ();
}
}
public void LoadFile () {
try{
RAF = new Randomaccessfile (file, "RW");
FCI = Raf.getchannel ();
Flock = Fci.trylock ();
if (flock = = null) {
Ta.settext ("");
Joptionpane.showmessagedialog (NULL,
"The file is in use and cannot be opened in an exclusive way!" ",
"Error hint", joptionpane.error_message);
File = "";
Raf.close ();
RAF = NULL;
}else{
int length = (int) fci.size ();
Bytebuffer BB = bytebuffer.allocate (length);
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 ();
try{
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 () {
try{
if (flock!= null) {
Flock.release ();
}
if (RAF!= null) {
Raf.close ();
}
File = "";
Frame.settitle ("My Notepad");
Save.setenabled (FALSE);
}catch (IOException e) {
E.printstacktrace ();
}
}
}
Effect Chart:
The above is the entire content of this article, I hope you can enjoy.