Notepad written in Java

Source: Internet
Author: User
Tags exit copy final functions interface save file stringbuffer

Studied Java for one months. wrote a notepad. Due to the time relationship. To practice, many functions are not added, only to achieve a simple interface and the most basic functions.
Later have time to perfect it.

=================================================================================

* * cats and kittens. First Java program
*
*
*copyright Cat
*/
Import java.awt.*;
Import java.awt.event.*;
Import javax.swing.*;
Import javax.swing.event.*;
Import java.io.*;

public class Notepad extends JFrame
{
String Openfilepath;
String OpenFileName;
String title= "ERROR message";
int type=joptionpane.error_message;

Public Notepad ()
{

Super ("Notepad");
Final JTextArea Text = new JTextArea ();
Text.settooltiptext ("Please type content");
Interface


Exit events
This.addwindowlistener (New Windowadapter ()
{
public void windowclosing (WindowEvent e)
{
System.exit (0);
}
});
A simple layout
Final JPanel panel=new JPanel ();
Panel.setlayout (New GridLayout (1,1));
Panel.add (new JScrollPane (text));
This.getcontentpane (). Add (panel);

Menu items
JMenuBar Mbar = new JMenuBar ();
This.setjmenubar (Mbar);
JMenu file = new JMenu ("file");
JMenu edit = New JMenu ("edit");
JMenu helps = new JMenu ("help");
Mbar.add (file);
Mbar.add (edit);
Mbar.add (Help);
JMenuItem newFile = new JMenuItem ("new");


Newfile.addactionlistener (New ActionListener ()
{
public void actionperformed (ActionEvent e)
{
Text.settext ("");
}
});

Layout end
New file
Newfile.setmnemonic (' N ');
Newfile.setaccelerator (Keystroke.getkeystroke (' N ', java.awt.event.ctrl_mask,true));
Open File
JMenuItem open = new JMenuItem ("open");
Open.setmnemonic (' O ');
Open.setaccelerator (Keystroke.getkeystroke (' O ', java.awt.event.ctrl_mask,true));
Open.addactionlistener (new ActionListener () {
public void actionperformed (ActionEvent e) {
JFileChooser OpenFile = new JFileChooser ();
Openfile.setdialogtitle ("Open file");
Openfile.setapprovebuttontext ("open");
Openfile.showopendialog (panel);
File filename = Openfile.getselectedfile ();
StringBuffer STRBF = new StringBuffer ();
String error_message = "error";
FileInputStream inputfile = null;
try{
Char buffer[] = new char[1024];
Inputfile = new FileInputStream (filename);
int len = 0;
FileReader in = new FileReader (Filename.getabsolutefile ());
while (len = in.read (buffer))!=-1)
{
Strbf.append (buffer, 0, Len);
}
Inputfile.close ();
Text.settext (Strbf.tostring ());
String openfilename = Filename.getname ();
Settitle (OPENFILENAME);

}
catch (IOException Ioex)
{
Joptionpane.showmessagedialog (Panel,error_message,title,type);
}

}});
Save File

JMenuItem save = new JMenuItem ("Save");
Save.setmnemonic (' S ');
Save.setaccelerator (Keystroke.getkeystroke (' S ', java.awt.event.ctrl_mask,true));
Save.addactionlistener (New ActionListener ()
{
public void actionperformed (ActionEvent e)
{
JFileChooser savefile=new JFileChooser ();
Savefile.setapprovebuttontext ("Save");
Savefile.setdialogtitle ("Save File");
Savefile.showsavedialog (panel);
File Filesa=savefile.getselectedfile ();
String file_notfound_message= "File not found";
FileOutputStream Outputfile=null;
Start Handling Exceptions
Try
{
outputfile = new FileOutputStream (FILESA);
}
catch (FileNotFoundException Fe)
{
Joptionpane.showmessagedialog (Panel,file_notfound_message,title,type);
}

String Filecontent=text.gettext ();
String write_error_message= "Write file Error";
Try
{
Outputfile.write (Filecontent.getbytes ());
}
catch (IOException Ioex)
{
Joptionpane.showmessagedialog (Panel,write_error_message,title,type);
}
String cmessage= "Shutdown error";

Try
{
Outputfile.close ();
}
catch (IOException Ioex)
{
Joptionpane.showmessagedialog (Panel,cmessage,title,type);
}
}
}
);
Exit

JMenuItem exit = new JMenuItem ("Exit");
Exit.addactionlistener (New ActionListener ()
{
public void actionperformed (ActionEvent e)
{
System.exit (0);
}
});
Exit.setmnemonic (' Q ');
Exit.setaccelerator (Keystroke.getkeystroke (' Q ', java.awt.event.ctrl_mask,true));
Find
JMenuItem find = new JMenuItem ("lookup");
Find.addactionlistener (New ActionListener ()
{
public void actionperformed (ActionEvent e)
{
Because the curriculum is too tight. So the search function doesn't have time to add up. ^_^
}
});
Find.setmnemonic (' F ');
Find.setaccelerator (Keystroke.getkeystroke (' F ', java.awt.event.ctrl_mask,true));
Shear
JMenuItem cut = new JMenuItem ("shear");
Cut.addactionlistener (New ActionListener ()
{
public void actionperformed (ActionEvent e)
{
Text.cut ();
}
});
Cut.setmnemonic (' C ');
Cut.setaccelerator (Keystroke.getkeystroke (' C ', java.awt.event.ctrl_mask,true));
Copy
JMenuItem copy = new JMenuItem ("copy");
Copy.addactionlistener (New ActionListener ()
{
public void actionperformed (ActionEvent e)
{
Text.copy ();
}
});
Copy.setmnemonic (' O ');
Copy.setaccelerator (Keystroke.getkeystroke (' O ', java.awt.event.ctrl_mask,true));
Paste
JMenuItem paste = new JMenuItem ("paste");
Paste.addactionlistener (new ActionListener () {
public void actionperformed (ActionEvent e) {
Text.paste ();
}});
Paste.setmnemonic (' P ');
Paste.setaccelerator (Keystroke.getkeystroke (' P ', java.awt.event.ctrl_mask,true));

JMenuItem about = new JMenuItem ("about");
About.addactionlistener (new ActionListener () {
public void actionperformed (ActionEvent e) {
int type=joptionpane.information_message;
String title= "about";
String message= "Make by Cat Lee";
Joptionpane.showmessagedialog (Panel,message,title,type);
}});
File.add (NewFile);
File.add (open);
File.add (save);
File.addseparator ();
File.add (exit);
Edit.add (cut);
Edit.add (copy);
Edit.add (paste);
Edit.add (find);
Help.add (about);

}
public static void Main (string[] args) {
Notepad Notepad = new Notepad ();
Notepad.setsize (640, 480);
Notepad.setvisible (TRUE);
Notepad.setdefaultcloseoperation (Jframe.exit_on_close);
}

}

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.