Use AWT to write your fourth graphical interface in Java--a simple Notepad

Source: Internet
Author: User

Made a simple Notepad, can be simple to open the file, edit the file, save the file, but how can not solve the problem of Chinese garbled, ask everyone advice ...

Package com.xywei.awt;

Import Java.awt.FileDialog;
Import Java.awt.Frame;
Import Java.awt.Menu;
Import Java.awt.MenuBar;
Import Java.awt.MenuItem;
Import Java.awt.TextArea;
Import java.awt.event.ActionEvent;
Import Java.awt.event.ActionListener;
Import Java.awt.event.WindowAdapter;
Import java.awt.event.WindowEvent;
Import Java.io.BufferedReader;
Import Java.io.File;
Import Java.io.FileInputStream;
Import java.io.FileNotFoundException;
Import Java.io.FileOutputStream;
Import java.io.IOException;
Import Java.io.InputStreamReader;
Import Java.io.OutputStreamWriter;
Import java.io.UnsupportedEncodingException;

/**
*
* @author Welins Write a simple notepad of your own,
*
*/
public class Mynotepad {

Private frame FR = new frame ();
Private MenuBar mbar = new MenuBar ();
Private menu mu = new menu ();
Private MenuItem Clsitem = new MenuItem ();
Private MenuItem SaveItem = new MenuItem ();
Private MenuItem Opnitem = new MenuItem ();
Private TextArea txae = new TextArea ();
To open a file that involves IO
private file file;
A saved or opened dialog box
Private FileDialog Opendia,savedia;
Private FileDialog opndial, savedial;

public void Mynotepad () {
Fr.settitle ("My Notepad");
Fr.setbounds (400, 100, 600, 400);
Fr.setvisible (TRUE);
Fr.setmenubar (mbar);

Mu.setlabel ("select");

Mbar.add (MU);
Opnitem.setlabel ("open");
Mu.add (Opnitem);
Saveitem.setlabel ("Save");
Mu.add (SaveItem);
Clsitem.setlabel ("Off");
Mu.add (Clsitem);
Add text to the frame window
Fr.add (txae);

opndial = new FileDialog (FR, "open", filedialog.load);
savedial = new FileDialog (FR, "save", Filedialog.save);

Event ();
}

public void Event () {
Fr.addwindowlistener (New Windowadapter () {
public void windowclosing (WindowEvent e) {
System.out.println ("Ready to close the window = = =");
System.out.println ("window closed, program exited");
System.exit (0);
}
});

Clsitem.addactionlistener (new ActionListener () {
@Override
public void actionperformed (ActionEvent e) {
TODO auto-generated Method Stub
System.exit (0);
}
});

Read File operation
Opnitem.addactionlistener (new ActionListener () {

@Override
public void actionperformed (ActionEvent e) {
//Call system function, implement open file
//TODO auto-generated method stub
Opndial.setvisible (TRUE);
//Get directories and files
String dir = opndial.getdirectory ();
String fileName = Opndial.getfile ();
System.out.println ("The directory of your selected files is:" + dir + "file is:" + fileName);
//Select OK and then appear in the Text field, first do the empty processing
Txae.settext (null);
Obtain the appropriate file by path and file name
File = new file (dir, filename);
The following involves the IO stream operation, open the file, whether to open in bytes or characters or a line of good?
//It is easier to read one line at a time
/**
*//Convert the file into an input stream inputstreamreader instrread = new
* InputStreamReader (New Fileinputstrea m (file), encoding); The
* Next changes the input to a byte stream bufferedreader bufread = new
* BufferedReader (Instrread);//strings string linetxt = null;
* * while ((Linetxt = Bufread.readline ()) = null) {
* SYSTEM.OUT.PRINTLN (Linetxt)}///After reading, you must close the read stream
* bufread.cl OSE (); Instrread.close ();
*/

String encoding = "UTF-8";
InputStreamReader Bufin;
try {
Bufin = new InputStreamReader (new FileInputStream (file),
encoding);
BufferedReader bufread = new BufferedReader (Bufin);
A line of reading must still have a string,
String READF = null;

try {
while ((READF = Bufread.readline ()) = null) {

System.out.println (READF);
Txae.append (READF);
}
You have to close the stream after reading.
Bufread.close ();
Bufin.close ();
} catch (IOException E1) {
TODO auto-generated Catch block
E1.printstacktrace ();
System.out.println ("read failed 1======");

}
} catch (Unsupportedencodingexception E1) {
TODO auto-generated Catch block
E1.printstacktrace ();
System.out.println ("read failed 2======");
} catch (FileNotFoundException E1) {
TODO auto-generated Catch block
E1.printstacktrace ();
System.out.println ("read failed 3======");

}

TODO auto-generated Catch block

}
});

Write file operations
Saveitem.addactionlistener (new ActionListener () {

@Override
public void actionperformed (ActionEvent e) {
TODO auto-generated Method Stub
if (file = = null) {
Savedial.setvisible (TRUE);
String dir = savedial.getdirectory ();
String fileName = Savedial.getfile ();
File = new file (dir, fileName);

}
The following write operation design output stream operation
try {
OutputStreamWriter BUFW = new OutputStreamWriter (
New FileOutputStream (file));
String writestr = Txae.gettext ();
try {
Bufw.write (WRITESTR);
} catch (IOException E1) {
TODO auto-generated Catch block
E1.printstacktrace ();
}
try {
Bufw.close ();
} catch (IOException E1) {
TODO auto-generated Catch block
E1.printstacktrace ();
}

} catch (FileNotFoundException E1) {
TODO auto-generated Catch block
E1.printstacktrace ();
}

}
});

}

/**
* @param args
*/
public static void Main (string[] args) {
TODO auto-generated Method Stub
Mynotepad NotePad = new Mynotepad ();
Notepad.mynotepad ();
}

}

Use AWT to write your fourth graphical interface in Java--a simple Notepad

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.