mypad小程序

package menupack;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
class MyMenu
{
	private Frame f;
	private MenuBar mb;
	private Menu fileMenu;
	private MenuItem openItem,saveItem,closeItem;

	private TextArea ta;
	private File ff;
	MyMenu()
	{
		init();
	}

	private void init()
	{
		f = new Frame("my frame");
		f.setBounds(300,200,600,500);
		f.setLayout(new FlowLayout());

		mb = new MenuBar();

		fileMenu = new Menu("文件");

		openItem = new MenuItem("打开");
		saveItem = new MenuItem("保存");
		closeItem = new MenuItem("关闭");

		fileMenu.add(openItem);
		fileMenu.add(saveItem);
		fileMenu.add(closeItem);

		mb.add(fileMenu);

		ta = new TextArea(28,82);

		f.setMenuBar(mb);
		f.add(ta);
		myEvent();

		f.setVisible(true);
	}
	private void myEvent()
	{

		openItem.addActionListener(new ActionListener()
		{
			public void actionPerformed(ActionEvent e)
			{
				FileDialog open = new FileDialog(f,"我的打开",FileDialog.LOAD);
				

				open.setVisible(true);
				String dir = open.getDirectory();
				String file = open.getFile();
				System.out.println(dir+".."+file);
				if(dir==null && file==null)
					return;

				ff = new File(dir,file);
				try
				{
					ta.setText("");
					BufferedReader bufr = 
					new BufferedReader(new FileReader(ff));

					String line = null;

					while((line=bufr.readLine())!=null)
					{
						ta.append(line+"\r\n");
					}

					bufr.close();
				}
				catch (IOException ex)
				{
					System.out.println("open :"+ex.toString());
				}
				
	
					
			}
		});

		saveItem.addActionListener(new ActionListener()
		{
			public void actionPerformed(ActionEvent e)
			{
				FileDialog save = new FileDialog(f,"我的保存",FileDialog.SAVE);
				if(ff==null)
				{
					save.setVisible(true);
					String dir = save.getDirectory();
					String file = save.getFile();
					System.out.println(dir+"......."+file);
					if(dir==null && file==null)
						return;
					ff = new File(dir,file);
				}
				try
				{
					BufferedWriter bufw =
						new BufferedWriter(new FileWriter(ff));

					String text = ta.getText();
					bufw.write(text);

					bufw.close();
				}
				catch (IOException ex)
				{
					System.out.println("save:"+ex.toString());
				}
			}
		});

		closeItem.addActionListener(new ActionListener()
		{
			public void actionPerformed(ActionEvent e)
			{
				System.exit(0);
			}
		});
		
		f.addWindowListener(new WindowAdapter()
		{
			public void windowClosing(WindowEvent e)
			{
				System.exit(0);
			}
		});
	}
	
	public static void main(String[] args) 
	{
		new MyMenu();
	}
}


/*
1,定义一个配置文件。
	Main-Class:空格包名.类名回车
2,jar -cfm demo.jar 配置文件名称 要存到jar中的包或者类。

3,验证一下,用winrar打开配置文件看到配置信息是否存在。

4,文件夹选项中设计jar文件的关联程序是bin\javaw.exe -jar
	
*/

 

你可能感兴趣的:(程序)