A simple Notepad code (ii)

Source: Internet
Author: User
Tags current time getmessage gettext save file

/**************************************************/


//Call function


public void file_new_actionperformed (ActionEvent e) {


filenew ();


}


public void file_open_actionperformed (ActionEvent e) {


FileOpen ();


}


public void file_save_actionperformed (ActionEvent e) {


FileSave ();


}


public void file_saveto_actionperformed (ActionEvent e) {


Filesaveto ();


}


public void file_close_actionperformed (ActionEvent e) {


FileClose ();


}


public void mousereleased (MouseEvent e) {


if (E.ispopuptrigger ())


pm.show (This,e.getx (), e.gety ());


}


public void mouseclicked (MouseEvent e) {}


public void mouseentered (MouseEvent e) {}


public void mouseexited (MouseEvent e) {}


public void mousepressed (MouseEvent e) {}


boolean toexit ()


{


if (bsaved)


return true;


int I=joptionpane.showconfirmdialog (this, "Save File", "Jishiben", joptionpane.yes_no_cancel_option);


switch (i)


{


Case Joptionpane.yes_option:


return true;


Case Joptionpane.no_option:


return false;


Case Joptionpane.cancel_option:


return false;


Default:


return false;


}


}


//Call File New


void FileNew ()


{


if (bsaved) {


This.jTextArea1.setText ("");


this.jTextArea1.setFocusable (TRUE);


this.settitle ("unnamed");


This.statubar.setText ("new text");


}


else Filesaveto ();


}


Call File Open


void FileOpen ()


{


String strfileopen= "";


if (bsaved) {


try{


if (This.jfilechooser.approve_option==this.jfilechooser.showopendialog (this)) {


strfileopen=this.jfilechooser.getselectedfile (). GetPath ();


file File=new file (Strfileopen);


int flength= (int) file.length ();


int num=0;


filereader freader=new filereader (file);


char[] Data=new char[flength];


while (Freader.ready ()) {


Num+=freader.read (Data,num,flength-num);


}


Freader.close ();


Jtextarea1.settext (New String (Data,0,num));


Filename=strfileopen;


this.settitle (filename);


Statubar.settext ("Open:" +filename);


Updatefname ();


Bsaved=false;


}


Else


return;


}catch (Exception e) {


statubar.settext ("Error Open:" +e.getmessage ());


}


}


Else


FileSave ();


}


//Invoke Save dialog box


boolean filesave ()


{


if (filename==null) {


return Filesaveto ();


}


if (!bsaved) {


if (filename.length ()!=0) {


try{


file Savefile=new file (filename);


FileWriter fw=new FileWriter (savefile);


Fw.write (Jtextarea1.gettext ());


Fw.close ();


statubar.settext ("Save file:" +filename);


bsaved=true;


Updatefname ();


}catch (Exception e)


{


statubar.settext ("Save Error:" +e.getmessage ());


}


}


else{


return Filesaveto ();


}


}


return true;


}


//Call Save dialog box


boolean Filesaveto ()


{


if (Jfilechooser.approve_option==jfilechooser.showsavedialog (this)) {


filename=jfilechooser.getselectedfile (). GetPath ();


return FileSave ();


}


else{


return false;


}


}


//Change title


void Updatefname () {


String str;


if (filename==null) {


str= "no title";


}


else{


Str=filename;


}


if (!bsaved) {


str= "Notepad" +STR;


}


this.settitle (str);


This.repaint ();


}


//File Close


void FileClose () {


if (!bsaved) {


if (Toexit ()) {


FileSave ();


}


else {


system.exit (0);


}


}


else{


system.exit (0);


}


}


//Call Edit cut


void Filecut (ActionEvent e)


{


try{


String Str=this.jtextarea1.getselectedtext ();


if (str.length ()!=0) {


stringselection s = new stringselection (str);


cb.setcontents (s,s);


This.jTextArea1.replaceRange ("", This.jTextArea1.getSelectionStart (), this.jTextArea1.getSelectionEnd ());


bsaved =false;


}


}catch (Exception ex) {


this.statubar.setText ("Cut Error:" +ex.getmessage ());


}


}


//Invoke Edit replication


void FileCopy (ActionEvent e)


{


try{


String Str=this.jtextarea1.getselectedtext ();


if (str.length ()!=0) {


stringselection s = new stringselection (str);


cb.setcontents (s,s);


}


}catch (Exception ex)


{


This.statubar.setText ("Error copying!") +ex.getmessage ());


}


}


//Invoke Edit paste


void Filepaste (ActionEvent e)


{


Try


{


Transferable tr =cb.getcontents (this);


if (tr!= null)


{


string s = (string) tr.gettransferdata (Dataflavor.stringflavor);


if (s!=null)


Jtextarea1.replacerange (S,jtextarea1.getselectionstart (), Jtextarea1.getselectionend ());


//this.jtextarea1.insert (S,this.jtextarea1.getcaretposition ());


bsaved =false;


}


}


catch (Exception err)


{


Err.printstacktrace ();


}


}


Invoke edit Delete


void Filedel (ActionEvent e)


{


Jtextarea1.replacerange ("", Jtextarea1.getselectionstart (), Jtextarea1.getselectionend ());


bsaved =false;


}


//Call Edit Lookup


void Filefindnext (ActionEvent e)


{


new Frame1 (JTEXTAREA1);


}


//Time


void Filetime (ActionEvent e) {


SimpleDateFormat sdf = new SimpleDateFormat ("Yyyy-mm-dd HH:mm:ss");


jtextarea1.append ("Current Time:" +sdf.format (New Date ());


bsaved =false;


}


//Select all


void Allselect (ActionEvent e) {


Jtextarea1.setselectionstart (0);


Jtextarea1.setselectionend (This.jTextArea1.getText (). Length ());


}


//Line Change


void Fileline (ActionEvent e) {


if (!jtextarea1.getlinewrap ()) {


Jtextarea1.setlinewrap (TRUE);


}


else{


Jtextarea1.setlinewrap (FALSE);


}


}//Problem


//Color


public void Fcolor (ActionEvent e) {


Color Bcolor=jtextarea1.getforeground ();


Jcolor.setcolor (Bcolor);


Jtextarea1.setforeground (


Jcolor.showdialog (jTextArea1, "Select Color", Bcolor));


}


//Call About dialog box


void Helpaboutdialog (ActionEvent e)


{


Joptionpane.showmessagedialog (This, "Copyright: Keel failure ...", "About Notepad", joptionpane.information_message);


}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.