JAVA Case Tutorial (1)

Source: Internet
Author: User
Tags exit event listener getmessage window
Tutorials//************************************


//A simple Java program


//function is to demonstrate a small window with the ability to read and write files.


//@author Gaogao


//@date 2004-11-04


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





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


//introduction of GUI and GUI event handling classes, and I/O managed classes


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


import java.awt.*;


import java.awt.event.*;


import java.io.*;





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


//Testone, a Demo menu and button application


//program.


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


public class Testone extends Frame implements ActionListener {


//inherit from frame, implement ActionListener interface


//frame is a form of frame class, ActionListener is an event interface that implements his


//Can do event handling.





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


//declaring variables


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


//Menu control


Private MenuBar MB;


private Menu Mfile;


private MenuItem miload;


private MenuItem Misave;


private MenuItem Miexit;


//Button


private TextArea TextArea;


//Disk Management


private file file;


//FileName


public static final String filename_str = "Love.TXT";


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


//Construction


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


public Testone () {


init ();





Putobjectstomyframe ();


addlistenerstoobjects ();


Setthewindow ();


}


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


//Allocating object memory


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


private void init () {


MB = new MenuBar ();


mfile = new Menu ("File");


miload = new MenuItem ("Load");


misave = new MenuItem ("Save");


miexit = new MenuItem ("Exit");





TextArea = new TextArea ("");


}





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


//Install parts to window


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


private void Putobjectstomyframe () {


Setmenubar (MB);


Mb.add (Mfile);


Mfile.add (miload);


Mfile.add (Misave);


Mfile.add (miexit);


Add (TextArea);


}





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


//Settings window


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


private void Setthewindow () {


this.setsize (400,300);


this.show ();


Addwindowlistener (


New Windowadapter () {


public void windowclosing (WindowEvent we)


{


system.exit (0);


}


}


);


}


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


//Set Event listener


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


private void Addlistenerstoobjects () {


Miload.addactionlistener (this)//the event listener miload This menu is placed in the instance of this window


Misave.addactionlistener (this);


Miexit.addactionlistener (this);





}


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


//Event listeners (this is the case of this class) after hearing the event


//Call. This method is an abstract
in the ActionListener interface

//Method implementation.


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


public void actionperformed (ActionEvent ae) {


if (ae.getsource () = = Miload)


{





//


System.out.println ("Loading");


DataInputStream Dis;





try {


file = new file (FILENAME_STR);


dis = new DataInputStream (new FileInputStream (file));


Textarea.settext ("");


////No Java doc documents are not written. To be perfected.


Dis.close ();


}


catch (Exception ex)


{


System.out.println (Ex.getmessage ());


}














}


Else if (ae.getsource () = = Misave)


{


//


System.out.println ("Saving");


DataOutputStream dos;


try {


file = new file (FILENAME_STR);


dos = new DataOutputStream (new FileOutputStream (file));


int i = 0;


String temp = Textarea.gettext ();


while (I < temp.length ())


{


Dos.writechar (Temp.charat (i++));


}





Dos.close ();


}


catch (Exception ex)


{


System.out.println (Ex.getmessage ());


}








}


Else if (ae.getsource () = = Miexit)


{


//Exit Application


System.out.println ("Exit");


system.exit (0);


}





}


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


//Program entry.


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


public static void Main (string[] args) {


new Testone ();


}


}

















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.