Simple Notepad Java source instance _java

Source: Internet
Author: User
Tags int size readfile save file

This example describes the simple Notepad Java implementation code. Share to everyone for your reference. Specifically as follows:

The complete code is as follows:

Copy Code code as follows:
Import java.awt.*;
Import java.io.*;
Import java.awt.datatransfer.*;
Import java.awt.event.*;

public class Main extends Frame implements ActionListener {
Private static final long serialversionuid = 1L;
TextArea TextArea = new TextArea ();
MenuBar MenuBar = new MenuBar ();
Menu Filemenu = new Menu ("File");
MenuItem newitem = new MenuItem ("new");
MenuItem Openitem = new MenuItem ("Open");
MenuItem SaveItem = new MenuItem ("Save");
MenuItem Saveasitem = new MenuItem ("Save as");
MenuItem Exititem = new MenuItem ("Exit");
Menu Editmenu = new Menu ("Edit");
MenuItem SelectItem = new MenuItem ("Select All");
MenuItem CopyItem = new MenuItem ("Copy");
MenuItem Cutitem = new MenuItem ("cut");
MenuItem Pasteitem = new MenuItem ("Paste");
String fileName = null;
Toolkit Toolkit=toolkit.getdefaulttoolkit ();
Clipboard Clipboard=toolkit.getsystemclipboard ();

Private FileDialog OpenFileDialog = new FileDialog (this, "Open File", filedialog.load);
Private FileDialog Saveasfiledialog = new FileDialog (this, "Save File as", Filedialog.save);

Public Main () {
Settitle ("notepad program-by jackbase");
SetFont (New Font ("Times New Roman", font.plain,12));
SetBackground (Color.White);
SetSize (400,300);
Filemenu.add (NewItem);
Filemenu.add (Openitem);
Filemenu.addseparator ();
Filemenu.add (SaveItem);
Filemenu.add (Saveasitem);
Filemenu.addseparator ();
Filemenu.add (Exititem);
Editmenu.add (SelectItem);
Editmenu.addseparator ();
Editmenu.add (CopyItem);
Editmenu.add (Cutitem);
Editmenu.add (Pasteitem);
Menubar.add (Filemenu);
Menubar.add (Editmenu);
Setmenubar (MenuBar);
Add (TextArea);
Addwindowlistener (New Windowadapter () {
public void windowclosing (WindowEvent e) {
System.exit (0);
}
});
Newitem.addactionlistener (this);
Openitem.addactionlistener (this);
Saveitem.addactionlistener (this);
Saveasitem.addactionlistener (this);
Exititem.addactionlistener (this);
Selectitem.addactionlistener (this);
Copyitem.addactionlistener (this);
Cutitem.addactionlistener (this);
Pasteitem.addactionlistener (this);
}

public void actionperformed (ActionEvent e) {//Listener event
Object EventSource = E.getsource ();
if (EventSource = = NewItem) {
Textarea.settext ("");
}else if (EventSource = = Openitem) {
Openfiledialog.show ();
FileName = Openfiledialog.getdirectory () +openfiledialog.getfile ();
if (fileName!= null)
ReadFile (FileName);
}else if (EventSource = = SaveItem) {
if (fileName!= null)
WriteFile (FileName);
}else if (EventSource = = Saveasitem) {
Saveasfiledialog.show ();
FileName = Saveasfiledialog.getdirectory () +saveasfiledialog.getfile ();
if (filename!= null)
WriteFile (FileName);
}else if (EventSource = = SelectItem) {
Textarea.selectall ();
}else if (EventSource = = CopyItem) {
String Text=textarea.getselectedtext ();
StringSelection selection=new stringselection (text);
Clipboard.setcontents (Selection,null);
}else if (EventSource = = Cutitem) {
String Text=textarea.getselectedtext ();
StringSelection selection=new stringselection (text);
Clipboard.setcontents (Selection,null);
Textarea.replacerange ("", Textarea.getselectionstart (), Textarea.getselectionend ());
}else if (EventSource = = Pasteitem) {
Transferable contents=clipboard.getcontents (this);
if (contents==null) return;
String text;
Text= "";
try{
text= (String) contents.gettransferdata (Dataflavor.stringflavor);
}catch (Exception Exception) {
}
Textarea.replacerange (Text,textarea.getselectionstart (), Textarea.getselectionend ());
}else if (EventSource = = Exititem) {
System.exit (0);
}
}

public void ReadFile (String fileName) {//Read file processing
try{
File File = new file (fileName);
FileReader Readin = new FileReader (file);
int size = (int) file.length ();
int charsread = 0;
char[] content = new Char[size];
while (Readin.ready ())
Charsread + + readin.read (content, Charsread, size-charsread);
Readin.close ();
Textarea.settext (New String (content, 0, charsread));
}
catch (IOException e) {
System.out.println ("Error opening file");
}
}

public void WriteFile (String fileName) {//write file processing
try{
File File = new file (fileName);
FileWriter writeout = new FileWriter (file);
Writeout.write (Textarea.gettext ());
Writeout.close ();
}
catch (IOException e) {
System.out.println ("Error writing file");
}
}

@SuppressWarnings ("deprecation")
public static void Main (string[] args) {
Frame frame = new Main (); Creating objects
Frame.show (); is the object display
}
}

The results of the operation are shown in the following illustration:

I hope this article will help you with your Java programming.

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.