JAVA Case Tutorial (1)
Last Update:2017-02-28
Source: Internet
Author: User
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 ();
}
}