NotePad version 1.0

NotePad version 1.0

自己写的一个非常简陋的类似于记事本的程序,只有简单的打开、保存功能。

/** 
 * @author nlee
 * @version 5:54:57 PM Jan 8, 2015
 */
package java_notepad;

import java.awt.Color;

import javax.swing.JFrame;

public class TxtFrame extends JFrame
{
	public static void main(String[] args)
	{
		// 设置TxtFrame
		TxtFrame tf = new TxtFrame();
		tf.setBackground(Color.darkGray);
		tf.setBounds(100, 100, 800, 600);
		tf.setLayout(null);
		tf.setVisible(true);

		// 添加菜单栏
		TxtMenuBar tmb = new TxtMenuBar();
		tmb.setFile();
		tmb.setEdit();
		tmb.setTool();
		tf.setJMenuBar(tmb);

		// 添加TxtPanel
		TxtPanel tp = new TxtPanel();
		// 添加快捷按钮
		tp.addButton();
		// 添加TextArea
		tp.addTextArea();

		tf.setContentPane(tp);
	}
}

/** 
 * @author nlee
 * @version 6:11:07 PM Jan 8, 2015
 */
package java_notepad;

import java.awt.Color;

import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;

public class TxtMenuBar extends JMenuBar
{
	public TxtMenuBar()
	{
		super();
		this.setBackground(Color.white);
		this.setBounds(0, 0, 40, 30);
		this.setVisible(true);
	}

	// 添加File菜单栏
	public void setFile()
	{
		JMenu mu_file = new JMenu("File");
		mu_file.setBackground(Color.white);

		String[] mi_name = { "New", "Open", "Save", "Save as", "Exit" };
		for (int i = 0; i < mi_name.length; i++)
		{
			JMenuItem mi = new JMenuItem("   " + mi_name[i]);
			mu_file.add(mi);
		}

		this.add(mu_file);
	}
	
	// 添加Edit菜单栏
	public void setEdit()
	{
		JMenu mu_edit = new JMenu("Edit");
		mu_edit.setBackground(Color.white);

		String[] mi_name = { "Undo", "Redo", "Find", "Replace" };
		for (int i = 0; i < mi_name.length; i++)
		{
			JMenuItem mi = new JMenuItem("   " + mi_name[i]);
			mu_edit.add(mi);
		}
		this.add(mu_edit);
	}

	// 添加Tool菜单栏
	public void setTool()
	{
		JMenu mu_tool = new JMenu("Tool");
		mu_tool.setBackground(Color.white);

		this.add(mu_tool);
	}
}

/** 
 * @author nlee
 * @version 6:15:53 PM Jan 8, 2015
 */
package java_notepad;

import java.awt.Button;
import java.awt.Color;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;

import javax.swing.JPanel;
import javax.swing.JTextArea;

public class TxtPanel extends JPanel
{
	public JTextArea ta;
	
	public TxtPanel()
	{
		super();
		this.setLayout(null);
		this.setBackground(Color.gray);
		this.setBounds(0, 30, 800, 600);
		this.setVisible(true);
	}
	
	// 添加按钮
	public void addButton()
	{
		String[] bt_name = {"new", "open", "save", "undo", "redo", "find" };
		for (int i = 0; i < bt_name.length; i++)
		{
			Button bt = new Button(bt_name[i]);
			bt.setBackground(Color.white);
			bt.setBounds(40 * i, 0, 40, 30);
			
			if (bt_name[i] == "open")
			{
				// 添加事件监听器
				bt.addMouseListener(new MouseAdapter()
				{
					// 事件处理方法
					@Override
					public void mouseClicked(MouseEvent e)
					{
						// 将文件中的内容输出到JTextArea中
						System.out.println("open button has been clicked!");
						openText();
					}
				});
			}
			
			if (bt_name[i] == "save")
			{
				// 添加事件监听器
				bt.addMouseListener(new MouseAdapter()
				{
					// 事件处理方法
					@Override
					public void mouseClicked(MouseEvent e)
					{
						// 将JTextArea中的内容保存到文件中
						System.out.println("save button has been clicked!");
						saveText();
					}
				});
			}
			
			this.add(bt);
		}
	}
	
	// 添加文本
	public void addTextArea()
	{
		ta = new JTextArea();
		ta.setBackground(Color.LIGHT_GRAY);
		ta.setVisible(true);
		ta.setBounds(0, 30, 800, 570);
		
		this.add(ta);
	}
	
	// 将JTextArea中的内容保存到文件中
	public void saveText()
	{
		String tmp = this.ta.getText();
		System.out.println(tmp);
		
		try
		{
			FileOutputStream fos = new FileOutputStream("mytxt.txt");
			byte[] b = new byte[tmp.length()];
			b = tmp.getBytes();
			fos.write(b);
		}
		catch (FileNotFoundException e)
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		catch (IOException e)
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	// 将文件中的内容输出到JTextArea中
	public void openText()
	{
		try
		{
			BufferedReader br = new BufferedReader(new InputStreamReader(
					new FileInputStream("mytxt.txt")));
			String line = null;
			while ((line = br.readLine()) != null)
			{
				this.ta.append(line + "\n");
			}
		}
		catch (FileNotFoundException e)
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		catch (IOException e)
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}
posted @ 2015-01-19 21:54  Coder816  阅读(124)  评论(0编辑  收藏  举报