Import java.awt.event. *;
Import java.io. *;
The public class Notepad/* implements ActionListener, MouseListener, MouseMotionListener, WindowListener, ItemListener, KeyListener, TextListener */
{
//member variable
Private frame host; //main frame
Private menu bar MB; //menu bar
Private Menus mFile, mEdit, mFormat, mHelp// Menus: File, Edit, Format, Help.
Private MenuItem miNew, miOpen, miSave, miSaveAs, miExit// file menu items: new, open, save, save as, exit.
Private menu miCut, miCopy, micast, miDelete///Edit menu items: cut, copy, paste and delete.
Private MenuItem miFont, miLowtoCapital, miCapitaltoLow, miEncrypt, miDisencrypt///Format menu item: font.
Private menuitem miaoutNotepad//Help menu item: About Notepad
Private TextArea ta// text area
Private string tempString// temporary string, used to store the string to be copied and pasted.
private boolean textvalue changed = false;
Private int id _ font// font
String file name = ""; //File name and address after the last save.
//constructor
Public Notepad () {
//framework
Mainframe = new framework ("Notepad v0.99 passes startup");
MB = new MenuBar();
ta = new TextArea (30,60);
Ta.setFont ("Times New Rome", font. Plain,15));
Ta.setBackground (new color (0,250,200));
//menu bar
MFile = new menu ("file");
MEdit = new menu ("Edit");
MFormat = new menu ("format");
MHelp = new menu ("help");
//"file"
miNew = New MenuItem(" New ");
miOpen = new MenuItem(" Open ");
miSave = new MenuItem(" Save ");
MiSaveAs = new MenuItem ("save as");
mi Exit = new MenuItem(" Exit ");
//"Edit"
miCut = new MenuItem(" Cut ");
mi Copy = new MenuItem(" Copy ");
mi Paste = new MenuItem(" Paste ");
miDelete = new MenuItem(" Delete ");
//"format"
mi Font = new MenuItem(" Font ");
miLowtoCapital = new MenuItem(" Low to Capital ");
miCapitaltoLow = new MenuItem(" Capital to Low ");
miEncrypt = new MenuItem(" Encrypt ");
midi sencrypt = new MenuItem(" discrypt ");
//"Help"
MiAboutNotepad = new MenuItem ("About Notepad");
//Add File Menu Item
mfile . add(miNew);
mfile . add(miOpen);
mfile . add(miSave);
mfile . add(miSaveAs);
mfile . add(miExit);
//Add edit menu item
medit . add(miCut);
medit . add(miCopy);
medit . add(mi paste);
medit . add(miDelete);
//Add Format Menu Item
m format . add(mi font);
m format . add(miLowtoCapital);
m format . add(miCapitaltoLow);
m format . add(miEncrypt);
m format . add(midi sencrypt);
//Add help menu item
mhelp . add(miAboutNotepad);
//Menu Bar Add Menu
MB . add(mFile);
MB . add(mEdit);
MB . add(m format);
MB . add(mHelp);
//Frame Add Menu Bar
mainframe . setmenubar(MB);
//The initial string is empty.
tempString =
//Add a text area
mainFrame.add(ta,BorderLayout。 Center);
mainFrame.setSize(800,500);
mainframe . set location( 100, 100); //Starting position
mainframe . setresizable(true); //Cannot change the size.
mainframe . set visible(true);
//mainframe. pack ();
//////////////////////////////////////////////////////////////////////////////////
///////////Add a monitor.
//////////////////////////////////////////////////////////////////////////////////
//main frame
Mainframe.addwindowlistener (newwindowadapter ()) {//Close the window.
public void window closing(window event e){
system . exit(0);
}
});
//Text area
ta . addkey listener(new key adapter(){
KeyEvent e){ typed by public void (
TextValueChanged = true// Pressing the keyboard key will cause the text to be modified.
}
});
/////////File menu://///////////////////////////
//New
minew . addactionlistener(new action listener(){
public void action performed(action event e){
ta.replaceRange(",0,ta.getText()。 length()); //Clear the contents of the text area.
File name = ""; //The file name is empty
}
});
//open
MiOpen.addActionListener (new ActionListener(){
public void action performed(action event e){
FileDialog d=new FileDialog (mainframe, "open file", file dialog. Load); //Open the file dialog box
D.addwindowlistener (newwindowadapter () {//Close the file dialog window.
public void window closing(window event ee){
system . exit(0);
}
});
d . set visible(true);
File f = new File(d . get directory()+d . getfile()); //Create a new file
fileName = d . get directory()+d . getfile(); //Get the file name
char ch[]= new char[(int)f . length()]; ///Create a character array with the length of this file.
Attempt//exception handling
{
//Read data and store it in the character array ch.
buffered reader bw = new buffered reader(new file reader(f));
bw . read(ch);
bw . close();
}
catch(file not found exception Fe){
System.out.println ("File not found");
system . exit(0);
}
catch( IOException ie){
System.out.println("IO error ");
system . exit(0);
}
String s = new string (ch);
Ta.setText// sets the text area to the contents of the open file.
}
});
//Save
MiSave.addActionListener (new ActionListener(){
public void action performed(action event e){
If(filename. equals(" "){// If the file has not been saved, the file name is empty.
FileDialog d=new FileDialog (mainframe, "save file", file dialog. Save); //Save File Dialog Box
D.addwindowlistener (newwindowadapter () {//Close the file dialog window.
public void window closing(window event ee){
system . exit(0);
}
});
d . set visible(true);
string s = ta . gettext(); //Get the input text content
Attempt//exception handling
{
File f = new File(d . get directory()+d . getfile()); //Create a new file
fileName = d . get directory()+d . getfile(); //Get the file name
buffered writer bw = new buffered writer(new file writer(f)); //Enter the file
bw.write(s,0,s . length());
bw . close();
}
catch(file not found exception Fe _){
System.out.println ("File not found");
system . exit(0);
}
Capture (IOException ie_)
{
System.out.println(" IO error ");
system . exit(0);
}
}
Otherwise//If the file has been saved.
{
string s = ta . gettext(); //Get the input text content
Attempt//exception handling
{
File f = new file (file name); //Create a new file
buffered writer bw = new buffered writer(new file writer(f)); //Enter the file
bw.write(s,0,s . length());
bw . close();
}
catch(file not found exception Fe _){
System.out.println ("File not found");
system . exit(0);
}
Capture (IOException ie_)
{
System.out.println(" IO error ");
system . exit(0);
}
}
}
});
//Save as
misaveas . addactionlistener(new action listener(){
public void action performed(action event e){
FileDialog d=new FileDialog (mainframe, "save file", file dialog. Save); //Save File Dialog Box
D.addwindowlistener (newwindowadapter () {//Close the file dialog window.
public void window closing(window event ee){
system . exit(0);
}
});
d . set visible(true);
string s = ta . gettext(); //Get the input text content
Attempt//exception handling
{
File f = new File(d . get directory()+d . getfile()); //Create a new file
buffered writer bw = new buffered writer(new file writer(f)); //Enter the file
bw.write(s,0,s . length());
bw . close();
}
catch(file not found exception Fe _){
System.out.println ("File not found");
system . exit(0);
}
Capture (IOException ie_)
{
System.out.println(" IO error ");
system . exit(0);
}
}
});
//Exit
Miexit.addactionlistener (new actionlistener () {///exits the program.
public void action performed(action event e){
system . exit(0);
}
});
/////////Edit menu:////////////////////////////
//cut
micut . addactionlistener(new action listener(){
public void action performed(action event e){
tempString = ta . getselectedtext(); ///Get the content to be copied and temporarily store it in tempString.
string buffer tmp = new string buffer(ta . gettext()); //Temporarily store text
int start = ta . getselectionstart(); //Get the starting position of the string to be deleted.
int len = ta.getSelectedText()。 Length (); //Gets the length of the string to be deleted.
tmp.delete( start,start+len); ///Delete the selected string
ta . settext(tmp . tostring()); //Set the original text with the new text
}
});
//copy
MiCopy.addActionListener (new ActionListener(){
public void action performed(action event e){
tempString = ta . getselectedtext(); ///Get the content to be copied and temporarily store it in tempString.
}
});
//Paste
MiPaste.addActionListener (new ActionListener(){
public void action performed(action event e){
string buffer tmp = new string buffer(ta . gettext()); //Temporarily store text
int start = ta . getselectionstart(); //Get the location to paste.
tmp.insert(start,tempString); //Check in the contents to be pasted.
ta . settext(tmp . tostring()); //Set the original text with the new text
}
});
//Delete
MiDelete.addActionListener (new ActionListener(){
public void action performed(action event e){
string buffer tmp = new string buffer(ta . gettext()); //Temporarily store text
int start = ta . getselectionstart(); //Get the starting position of the string to be deleted.
int len = ta.getSelectedText()。 Length (); //Gets the length of the string to be deleted.
tmp.delete( start,start+len); ///Delete the selected string
ta . settext(tmp . tostring()); //Set the original text with the new text
}
});
/////////Format menu:////////////////////////////////////////////////////////////////////
//font
MiFont.addActionListener (new ActionListener(){
public void action performed(action event e){
Final dialog d = new dialog (host, "font"); //New dialog box
d.setLocation( 250,250); //Starting position
d . set layout(new BorderLayout()); //Table layout
////////Upper panel.
Label l _ font = new Label(" font "); //Font label
Panel p _ 1 = new Panel();
p _ 1 . add(l _ font);
p _ 1 . set visible(true);
///////Some panels in////.
List font_list = new List (6,false); //Font list
//Add font item
font _ list . add(" Plain "); ///Normal font
font _ list . add(" Bold "); ///Bold
font _ list . add(" Italic "); //Italic
font _ list . additem listener(new myitem listener _ font()); //Font Increase Monitor
Panel p2 = new Panel();
p _ 2 . add(font _ list);
p _ 2 . set visible(true);
////////Lower panel.
Button ok = New button ("OK");
Ok.addActionListener (new ActionListener(){
public void action performed(action event e){
d . dispose();
}
});
Ok.setSize (new dimension (20,5));
Panel P3 = new Panel(); //Lower panel
p _ 3 . add(ok);
p _ 3 . set visible(true);
//Add three panels
d.add(p_ 1,BorderLayout。 North);
d.add(p_2,BorderLayout。 Center);
d.add(p_3,BorderLayout。 South);
d . pack();
D.addwindowlistener (newwindowadapter () {//Close the dialog window.
public void window closing(window event ee){
d . dispose();
}
});
d . set visible(true);
}
});
//Convert lowercase letters to uppercase letters
Milowtocapital. addactionlistener (new ActionListener(){
public void action performed(action event e){
string s = ta . gettext(); //Get the input text content
string buffer temp = new string buffer(" ");
for(int I = 0; I & lts.length (). i++){
if((int)s.charAt(i)>= 97 & amp& amp(int)s . charat(I)& lt; = 122 ){
temp . append((char)((int)s . charat(I)-32));
}
other
temp.append(s.charAt(i))。
}
S = new string (temp);
ta.setText
}
});
//Convert uppercase letters to lowercase letters
micapitaltolow . addactionlistener(new action listener(){
public void action performed(action event e){
string s = ta . gettext(); //Get the input text content
string buffer temp = new string buffer(" ");
for(int I = 0; I & lts.length (). i++){
if((int)s.charAt(i)>= 65 & amp& amp(int)s . charat(I)& lt; =90 ){
temp . append((char)((int)s . charat(I)+32));
}
other
temp.append(s.charAt(i))。
}
S = new string (temp);
ta.setText
}
});
//encryption
miencrypt . addactionlistener(new action listener(){
public void action performed(action event e){
string s = ta . gettext(); //Get the input text content
string buffer temp = new string buffer(" ");
for(int I = 0; I & lts.length (). i++){
if(s.charAt(i)>= 40 & amp& amps . charat(I)& lt; = 125){
if(i%2==0){
temp . append((char)(s . charat(I)+ 1));
}
other
temp . append((char)(s . charat(I)- 1));
}
other
temp.append(s.charAt(i))。
}
S = new string (temp);
ta.setText
}
});
//decryption
MiDisencrypt.addActionListener (new ActionListener(){
public void action performed(action event e){
string s = ta . gettext(); //Get the input text content
string buffer temp = new string buffer(" ");
for(int I = 0; I & lts.length (). i++){
if(s.charAt(i)>= 40 & amp& amps . charat(I)& lt; = 125){
if(i%2==0){
temp . append((char)(s . charat(I)- 1));
}
other
temp . append((char)(s . charat(I)+ 1));
}
other
temp.append(s.charAt(i))。
}
S = new string (temp);
ta.setText
}
});
/////////Help menu://///////////////////////////////////////////
//About Notepad
Miaboutnotepad.addactionlistener (new ActionListener(){
public void action performed(action event e){
Final dialog d = new dialog (host, "About Notepad"); //New dialog box
Textarea t = new textarea ("\ nWelcome to use Notepad"+"\ n \ n"+"copyright @ launching"+"\ n \ n"+"free software"+"\ n \ n"+"v0.99"); //Add a label
T.setSize (new dimension (5,5));
t . set editable(false);
d . setresizable(false); //Not resizable
D. add (t);
d . pack();
D.addwindowlistener (newwindowadapter () {//Close the dialog window.
public void window closing(window event ee){
d . dispose();
}
});
d.setLocation( 100,250); //Starting position
d . set visible(true);
}
});
}
Classmyitemlistener _ implementing itemlistener {//font listener
public void itemStateChanged(item event e){
id _ font =((Java . awt . list)e . getsource())。 getSelectedIndex();
Switch (id_font){
Case 0:{
Ta.setFont ("Times New Roman", font. PLAIN,ta.getFont()。 getSize())); //Plain text
Break;
}
Case 1:{
Ta.setFont ("Times New Roman", font. BOLD,ta.getFont()。 getSize())); //Bold text
Break;
}
Case 2:{
Ta.setFont ("Times New Roman", font. ITALIC,ta.getFont()。 getSize())); //Italic text
Break;
}
}
}
}
////////////////////////////////////////////////////////.
Public static void main (strinarg []) {
Notepad test = new Notepad (); ///Create Notepad
}
//////////////////////////////////////////////////////////////////////////////////////////////////
}