Java GUI (Instance project-Notepad)

Source: Internet
Author: User

Package mymenu;
Java Notepad
Import java.awt.*;
Import java.awt.event.*;
Import java.io.*;
public class Menulianxi {
Defining forms
Private Frame F;
Defining the menu bar
Private MenuBar MenuBar;
Define text Area
Private TextArea TextArea;
Defining menus
Private Menu File_menu;
Defining menu Items
Private MenuItem Close_item,open_item,save_item;


Define Display dialog box
Private FileDialog Opendia,savedia;

Defines a file that is used to save
private file file;
Defining constructors
Menulianxi () {
Add the init () method to the constructor.
Init ();
}
Defines the method that sets the interface of the UI.
public void Init ()
{
Set up basic information for a form
F=new Frame ("My Notepad");
F.setbounds (300,100,650,600);
F.setlayout (New FlowLayout ());

Initializing the menu bar
Menubar=new menubar ();

Initialize text area
Textarea=new textarea ();

Initialize Menu
File_menu=new Menu ("file");

Initializing menu items
Open_item=new MenuItem ("open");
Close_item=new MenuItem ("exit");
Save_item=new MenuItem ("Save");

Add a menu item or submenu to a menu
File_menu.add (Open_item);
File_menu.add (Save_item);
File_menu.add (Close_item);

Add a menu to the menu bar
Menubar.add (File_menu);

Add a menu bar to a form
F.setmenubar (menubar);

Initialize dialog box
Opendia=new FileDialog (F, "My Open", filedialog.load);
Savedia=new FileDialog (F, "My Save", Filedialog.save);

Add a text area to a form
F.add (textarea);

Add Listener
MyEvent ();

Set form visible
F.setvisible (TRUE);

}
Defining the MyEvent method to add listeners
public void MyEvent ()
{

To add an active listener for a Save menu item
Save_item.addactionlistener (new ActionListener () {
Replication Actionperformed method
public void actionperformed (ActionEvent e)
{
If the file does not exist, initialize it to the file
if (file==null)
{
The Settings window is visible
Savedia.setvisible (TRUE);
Gets the file directory, with the file
String dirpath=savedia.getdirectory ();
String Filename=savedia.getfile ();
Do not manipulate any files, cancel operation
if (dirpath==null| | Filename==null)
return;
File =new file (dirpath,filename);
}

try{
Defines a write stream that writes text to a file
BufferedWriter bufw=new BufferedWriter (new FileWriter (file));
Defines the text where the string receives the text.
String Text=textarea.gettext ();
Write text to the text information.
Bufw.write (text);
Closes the write stream.
Bufw.close ();
}
catch (IOException E4)
{
throw new RuntimeException ();
}
}
});
To add an active listener to an open menu item
Open_item.addactionlistener (new ActionListener () {
public void actionperformed (ActionEvent e)
{
Opendia.setvisible (TRUE);
Get Directory in dialog box
String dirpath=opendia.getdirectory ();
Get the file name in the dialog box
String Filename=opendia.getfile ();
System.out.println (dirpath+ "--" +filename);
Avoid NULL pointer exceptions
if (dirpath==null| | Filename==null)
return;
Clear Text Area
Textarea.settext ("");
Defining file and File directory objects
File=new File (dirpath,filename);
try{
Call BufferedReader Read Stream read file
BufferedReader bufr=new BufferedReader (new FileReader (file));
String Line=null;
Gets the text information in the BUFR
while ((Line=bufr.readline ())!=null)
{
Adds text information to the text area.
Textarea.append (line+ "\ r \ n");
}
Closes the read stream.
Bufr.close ();
}
catch (IOException E1)
{
throw new RuntimeException ("read failed");
}
}
});
Add an activity listener for the menu item Closeitem
Close_item.addactionlistener (new ActionListener () {
@Override
The Actionperformed abstract method adds a processing action.
public void actionperformed (ActionEvent e) {
TODO auto-generated method stubs
System.exit (0);
}
});
The form calls the Addwindowlistener method, passing an interface Class (Listener adapter) Windowadapter.
F.addwindowlistener (New Windowadapter () {
Replication Windowclosing method.
public void windowclosing (WindowEvent e) {
System.exit (0);
}
});
}
public static void Main (string[] args) {
New Menulianxi ();
}
}

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Java GUI (Instance project-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.