GUI —— 制作记事本 FileDialog/Frame/MenuBar/Menu/MenuIt

/**
 制作:记事本
 */

//MenuBar 菜单栏/菜单条
//Menu  菜单
//Menu  菜单项
//包含关系图:MenuBar(Menu(Menu / MenuItem))

package day22;

import java.awt.FileDialog;
import java.awt.Frame;
import java.awt.Menu;
import java.awt.MenuBar;
import java.awt.MenuItem;
import java.awt.MenuShortcut;
import java.awt.TextArea;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;

public class MenuDemo {
	Frame frame = null;
	MenuBar menuBar = null;
	Menu fileMenu = null,
			viewMenu = null,
			_1Menu = null;
	MenuItem openItem = null, saveItem = null, closeItem = null,
			_2MenuItem = null;

	FileDialog fileDialog = null;

	TextArea ta = null;

	File file = null;

	MenuDemo() {
		init();
	}

	public void init() {
		frame = new Frame("my Frame showMenu");
		frame.setBounds(200, 100, 600, 400);
		// frame.setLayout(new BorderLayout()); TextArea的默认布局管理器:边界式布局

		ta = new TextArea();
		ta.setBounds(0, 0, 400, 300);

		menuBar = new MenuBar();

		fileMenu = new Menu("文件");
		viewMenu = new Menu("查看");

		_1Menu = new Menu("子菜单");

		openItem = new MenuItem("打开", new MenuShortcut(KeyEvent.VK_CONTROL
				+ KeyEvent.VK_A));
		saveItem = new MenuItem("保存");
		closeItem = new MenuItem("关闭");

		_2MenuItem = new MenuItem("二级菜单项");

		_1Menu.add(_2MenuItem);

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

		menuBar.add(fileMenu);
		menuBar.add(viewMenu);

		frame.setMenuBar(menuBar);
		frame.add(ta);

		// 添加侦听器
		addListener();

		frame.setVisible(true);// ★注意★:此句必须写在其他语句之后
	}

	private void addListener() {
		frame.addWindowListener(new WindowAdapter() {
			public void windowClosing(WindowEvent e) {
				System.exit(0);
			}
		});

		openItem.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				fileDialog = new FileDialog(frame, "openFile", FileDialog.LOAD);
				fileDialog.setVisible(true);// ★注意★:此句必须写在后面语句之前!

				// 获取打开文件的目录及文件名
				String dirName = fileDialog.getDirectory();
				String fileName = fileDialog.getFile();

				// 如果没有选择文件,则返回null,所以加以判断
				if (dirName == null || fileName == null)
					return;

				// 当选择的是有效文件时,清空原TextArea
				ta.setText("");

				file = new File(dirName, fileName);
				BufferedReader bufr = null;
				try {

					bufr = new BufferedReader(new FileReader(file));
					String line = null;
					while ((line = bufr.readLine()) != null) {
						ta.append(line);
						// 跨平台换行符
						ta.append(System.getProperty("line.separator"));
					}

				} catch (IOException elment) {
					throw new RuntimeException("打开文件失败");
				} finally {
					try {
						if (bufr != null)
							bufr.close();
					} catch (IOException elment) {
						throw new RuntimeException("流关闭失败");
					}
				}

			}
		});

		saveItem.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				if (file == null) {
					fileDialog = new FileDialog(frame, "SaveFile",
							FileDialog.SAVE);
					fileDialog.setVisible(true);
					String dirName = fileDialog.getDirectory();
					String fileName = fileDialog.getFile();
					file = new File(dirName, fileName);
				}
				String str =ta.getText();
				BufferedWriter bufw = null;
				try{
					bufw= new BufferedWriter(new FileWriter(file));
					bufw.write(str);
					bufw.flush();
				}catch(IOException elment){
					throw new RuntimeException("保存文件失败");
				}finally{
					try{
						if(bufw !=null)
							bufw.close();
					}catch(IOException elment){
						throw new RuntimeException("写入流关闭失败");
					}
				}
			}
		});

		closeItem.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				System.exit(0);
			}
		});
	}

	public static void main(String[] args) {
		new MenuDemo();
	}
}


你可能感兴趣的:(GUI,记事本)