GUI (Practice-open and save files)

Source: Internet
Author: User
Tags stub
* * Program Head NOTE start * Copyright and version Statement of the program (c) 2011, University of Yantai Computer Students * All rights reserved.   
* File name: GUI (Practice-open and save files) * Author: Shigung * Completion Date: September 22, 2011
* Version Number: x1.0 * Description of tasks and solutions * Input Description: * Problem Description: * Program output: * program Head of the comment end * * * package xue;
Import java.awt.*;
Import java.awt.event.*;

Import java.io.*;
	public class Mymenutest {/** * @param args * * private Frame F;
	Private MenuBar Bar;
	Private TextArea ta;
	Private Menu Filemenu;
	
	Private MenuItem Openitem, SaveItem, Closeitem;
	
	Private FileDialog Opendia, Savedia;
	private file file;
	Mymenutest () {init ();
		public void init () {//TODO auto-generated method Stub f = new Frame (' my Window ');
		
		F.setbounds (300, 100, 650, 600);
		
		bar = new MenuBar ();
		
		Ta = new TextArea ();
		
		Filemenu = new Menu ("file");
		Openitem = new MenuItem ("open");
		SaveItem = new MenuItem ("Save");
		
		Closeitem = new MenuItem ("Exit"); FileMenu.add (Openitem);
		Filemenu.add (SaveItem);
		Filemenu.add (Closeitem);
		
		Bar.add (Filemenu);
		
		F.setmenubar (bar);
		Opendia = new FileDialog (f, "I want to open", filedialog.load);
		
		Savedia = new FileDialog (f, "I want to save", Filedialog.save);
		F.add (TA);
		
		MyEvent ();

	F.setvisible (TRUE);
			private void MyEvent () {//TODO auto-generated Method Stub Saveitem.addactionlistener (new ActionListener () {
					
					public void actionperformed (ActionEvent e) {if (file = = null) {savedia.setvisible (true);
					String Dirpath = Savedia.getdirectory ();
					
					String fileName = Savedia.getfile ();
					if (Dirpath = null | | | fileName = NULL) {return;	
				File = new file (Dirpath, fileName);
					try {bufferedwriter BUFW = new BufferedWriter (new FileWriter (file));
					String text = Ta.gettext ();
					Bufw.write (text);
					Bufw.flush ();
				Bufw.close ();
				The catch (IOException ex) {throw new RuntimeException (); }
			}
		}); Openitem.addactionlistener (new ActionListener () {public void actionperformed (ActionEvent e) {Opendia.setvisi
				BLE (true);
				String Dirpath = Opendia.getdirectory ();
				
				String fileName = Opendia.getfile ();
				if (Dirpath = null | | | fileName = NULL) {return;
				} ta.settext ("");
				
				File = new file (Dirpath, fileName);
					try {bufferedreader bufr = new BufferedReader (new FileReader (file));
					
					String line = null;
					while (line = Bufr.readline ())!= null) {Ta.append (line + "\ r \ n");
				} bufr.close ();
				catch (IOException ex) {throw new RuntimeException ("read failed");
		
		}
			}
		}); Closeitem.addactionlistener (new ActionListener () {@Override public void actionperformed (ActionEvent e) {//	
			TODO auto-generated Method Stub system.exit (0);
		
		}
		}); F.addwindowlistener (New Windowadapter () {public void windowclosing (WindowEvent e) {System.Exit (0);
	}
		});

	public static void Main (string[] args) {//TODO auto-generated method Stub new Mymenutest ();
 }

}


Run Result:

On the Machine testimonials: graphical interface Good magic Ah, learned it to know this fun

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.