Simply open and save the file notepad
import java.awt. *;
import java.awt.event. *;
import java.io. *;
class MenuDemo
{
private Frame f;
// Menu components can add menu items and menu components
private Menu m, m1, m2; //Menu Bar
private MenuBar mb; //Menu Item
private MenuItem closeMenu, mi2, mi3, openItem, saveItem;
private FileDialog openDia, saveDia;
private TextArea ta;
File file = null; // File object
MenuDemo ()
{
init ();
}
public void init ()
{
f = new Frame ("Menu Practice");
// Form settings
f.setBounds (200,100,600,600);
//f.setLayout(new FlowLayout ()); // Frame default border layout
mb = new MenuBar ();
m = new Menu ("File");
m1 = new Menu ("first level menu");
m2 = new Menu ("second level menu");
closeMenu = new MenuItem ("Exit");
mi2 = new MenuItem ("second-level menu item");
mi3 = new MenuItem ("three-level exit");
openItem = new MenuItem ("Open");
saveItem = new MenuItem ("Save");
saveItem.setShortcut (new MenuShortcut (KeyEvent.VK_S)); // Add shortcut command
// Open the save dialog
openDia = new FileDialog (f, "Open", FileDialog.LOAD);
saveDia = new FileDialog (f, "Save", FileDialog.SAVE);
// text box
ta = new TextArea ();
// Add menu related components to the form
f.setMenuBar (mb); // The form adds menu bar setMenuBar ()
mb.add (m); // MenuBar menu bar can only add Menu can not add the parent MenuItem Menu
m.add (openItem);
m.add (saveItem);
//m.addSeparator();//Add a dividing line
m.add (m1);
m1.add (m2);
m1.add (mi2);
//m2.add(closeMenu);
m2.add (mi3);
m.add (closeMenu);
f.add (ta);
/ *
The order of code addition is the order of the graphics. A menu item has only one graphic display.
The statement before adding this menu item is overwritten or invalid
* /
myEvent ();
f.setVisible (true);
}
public void myEvent ()
{
// Add a listener for the form
f.addWindowListener (new WindowAdapter () {
public void windowClosing (WindowEvent e)
{
System.exit (0);
}
});
// Add a listener for the menu item
closeMenu.addActionListener (new ActionListener () {
public void actionPerformed (ActionEvent e)
{
System.exit (0);
}
});
mi3.addActionListener (new ActionListener () {
public void actionPerformed (ActionEvent e)
{
System.exit (0);
}
});
openItem.addActionListener (new ActionListener () {
public void actionPerformed (ActionEvent e)
{
openDia.setVisible (true);
// Open the text file and display the content in the text box
String dirPath = openDia.getDirectory ();
String fileName = openDia.getFile ();
System.out.println (dirPath + "...." + fileName);
file = new File (dirPath, fileName);
if (dirPath == null || fileName == null)
return;
ta.setText ("");
BufferedReader bfr = null; // Local variables must be assigned initial values
try
{
bfr = new BufferedReader (new FileReader (file));
String line = null;
while ((line = bfr.readLine ())! = null)
{
//System.out.println(line);
ta.append (line + "\ r \ n"); // Do n’t forget to add line breaks
}
}
catch (IOException io)
{
throw new RuntimeException ("Read failed!");
}
finally
{
try
{
if (bfr! = null)
bfr.close ();
}
catch (IOException ec)
{
throw new RuntimeException ("guan failed!");
}
}
}
});
// Add monitor for save button Perform action by monitoring event: save text to file
saveItem.addActionListener (new ActionListener () {
public void actionPerformed (ActionEvent e)
{
if (file == null)
{
saveDia.setVisible (true);
String dirPath = saveDia.getDirectory ();
String fileName = saveDia.getFile ();
file = new File (dirPath, fileName);
}
BufferedWriter bfw = null;
try
{
bfw = new BufferedWriter (new FileWriter (file));
String text = ta.getText (); // Return the text in the text component
bfw.write (text);
bfw.flush ();
}
catch (IOException ec)
{
throw new RuntimeException ();
}
finally
{
try
{
if (bfw! = null)
bfw.close ();
}
catch (IOException ec)
{
throw new RuntimeException ("guan failed!");
}
}
The
}
});
}
public static void main (String [] args)
{
new MenuDemo ();
}
}
This article comes from "either desperate or rolling back !" Blog, please be sure to keep this source http://jiangzuun2014.blog.51cto.com/8732469/1440430
Gui exercise-Notepad