mypadウィジェット

3583 ワード


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
"); } 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 */