JAVA 学习之路二 自己制作一个记事本

一、创建项目

以 FileIO为项目名称 ,以com.xiaojun.fileIO为包名, 以 FileIO为类名 创建项目

二、做一个记事本界面

在这里插入图片描述

public class FileIO extends WindowAdapter implements ActionListener {
	JFrame jf; // 窗体

	JButton jb, jb1;

	JTextArea ta; // 文本域

	String filename, copy, paste, cut;

	JPanel jp; // 面板组件

	JMenu jmb, jmb2; // 菜单

	JMenuItem _fm, _fm1, _fm2, _fm3, _fm4, _fe1, _fe2, _fe3, _fe4; // 菜单项

	JMenuBar JMENU; // 菜单栏

	JScrollPane jsp; // 滚动面板

	JLabel jl; // 标签组件

	public FileIO() {
		// TODO 自动生成的构造函数存根
		jp = new JPanel();
		jl = new JLabel("小俊记事本");
		JMENU = new JMenuBar();
		ta = new JTextArea();
		jf = new JFrame();
		jsp = new JScrollPane(ta);
		jf.addWindowListener(this);
		jmb = new JMenu("文件");
		jmb2 = new JMenu("编辑");

		_fm1 = new JMenuItem("打开");
		_fm1.addActionListener(this);
		_fm2 = new JMenuItem("存储");
		_fm2.addActionListener(this);
		_fm4 = new JMenuItem("另存为");
		_fm4.addActionListener(this);
		_fm3 = new JMenuItem("关闭");
		_fm3.addActionListener(this);
		_fm = new JMenuItem("新建");
		_fm.addActionListener(this);

		_fe1 = new JMenuItem("复制");
		_fe1.addActionListener(this);
		_fe2 = new JMenuItem("粘贴");
		_fe2.addActionListener(this);
		_fe3 = new JMenuItem("剪切");
		_fe3.addActionListener(this);
		_fe4 = new JMenuItem("作者");
		_fe4.addActionListener(this);
		jf.setJMenuBar(JMENU);
		jf.setTitle("记事本");

		jmb.add(_fm);
		jmb.addSeparator();// 菜单中横线分隔符号
		jmb.add(_fm1);
		jmb.addSeparator();
		jmb.add(_fm2);
		jmb.addSeparator();
		jmb.add(_fm4);
		jmb.addSeparator();
		jmb.add(_fm3);

		jmb2.add(_fe1);
		jmb2.addSeparator();
		jmb2.add(_fe2);
		jmb2.addSeparator();
		jmb2.add(_fe3);
		jmb2.addSeparator();
		jmb2.add(_fe4);

		JMENU.add(jmb);
		JMENU.add(jmb2);

		jb = new JButton("保存");
		/**
		 * Listener是监听者,比如你定义了一个按钮,用addActionListener就可以给这个按钮添加一
		 * 个监听者,一旦有事件发生(比如鼠标点击),监听者都可以得到这个事件,并可以把事件信息
		 * (例如事件源) 传给事件处理函数.this指本身这个对象,这个类会实现监听器这个接口。给某
		 * 个实例(按钮等)添加事件监听接口,this表示当前类的对象,在一个类里,你不需要new他的
		 * 实例就直接可以用this调用它的方法和属性
		 */
		jb.addActionListener(this);
		jb1 = new JButton("关闭");
		jb1.addActionListener(this);

		jp.add(jb);
		jp.add(jb1);
		jp.add(jl);

		jf.add(jp, "South");
		ta.setWrapStyleWord(true); // 设置在单词过长的时候是否要把长单词移到下一行。
		jf.add(jsp);

		jf.setSize(600, 400);
		jf.setVisible(true);
		// 获取当前屏幕分辨率
		int W = (int) Toolkit.getDefaultToolkit().getScreenSize().getWidth();
		int H = (int) Toolkit.getDefaultToolkit().getScreenSize().getHeight();
		// 将组件移到新位置。通过此组件父级坐标空间中的 x 和 y 参数来指定新位置的左上角。
		jf.setLocation((W - jf.getWidth()) / 2, (H - jf.getHeight()) / 2);

	}
	@Override
	public void actionPerformed(ActionEvent e) {
		// TODO 自动生成的方法存根
	}
	
	public static void main(String[] args) {
		new FileIO();
	}

三、方法实现

@Override
	public void actionPerformed(ActionEvent e) {
		// TODO 自动生成的方法存根
		if (e.getSource() == jb || e.getSource() == _fm2) {
			try {
				if (filename == null) {
					// 在自定义对话框窗口时提示用户输入
					filename = JOptionPane.showInputDialog("请输入文件名", "新建文本文档");
					// 点击取消或者右上角的x时传回来的值都是null
					if (filename == null) {
						return;
					}
					/**
					 * 创建文件输出流,以使用指定的名称写入文件。将创建一个新的FileDescriptor对
					 * 象来表示此文件连接。
					 */
					// 
					FileOutputStream fout = new FileOutputStream(filename + ".txt");

					System.out.print(fout + "  +  " + filename);
					// 使用平台的默认字符集将此字符串编码为字节序列,并将结果存储到新的字节数组中
					byte bb[] = ta.getText().getBytes();
					fout.write(bb);// 将指定字节数组中的b.length字节写入此文件输出流
					fout.close(); // 关闭此文件输出流并释放与此流关联的所有系统资源
					// 显示一个带有OK 按钮的模态对话框。
					JOptionPane.showMessageDialog(null, "已保存"); 
				} else {
					FileOutputStream fout = new FileOutputStream(filename + ".txt");
					System.out.println(fout);
					byte bb[] = ta.getText().getBytes();
					fout.write(bb);
					fout.close();
					JOptionPane.showMessageDialog(null, "已保存");

				}
			} catch (IOException ioe) {
				// TODO: handle exception
				System.err.println(e);
			}

		}
		if (e.getSource() == _fm) {
			if (!(ta.getText().equals("")))// 比较字符串中所包含的内容是否相同
			{
				Object[] options = { "确定", "取消" };
				int response = JOptionPane.showOptionDialog(null, "你是否保存", "提示", JOptionPane.YES_OPTION,
						JOptionPane.QUESTION_MESSAGE, null, options, options[0]);
				if (response == 0) {
					try {
						// SAVE(保存)
						FileDialog d = new FileDialog(jf, "保存文件", FileDialog.SAVE); 
						d.setVisible(true);
						// getFile()函数返回URL的路径和查询。
						filename = d.getDirectory() + d.getFile();
						FileOutputStream fout = new FileOutputStream(filename + ".tex");
						byte bb[] = ta.getText().getBytes();
						fout.write(bb);
						fout.close();
						JOptionPane.showMessageDialog(null, "已保存");
						ta.setText("");
					} catch (Exception e2) {
						// TODO: handle exception
						System.err.println(e);
					}
				}
				if (response == 1) {
					return;
				}
			}
			filename = "";
		}
		// 打开
		if (e.getSource() == _fm1) {
			// 创建具有指定标题的文件对话框窗口
			FileDialog d = new FileDialog(jf, "打开文件", FileDialog.LOAD); // LOAD(读写)
			// 打开弹窗
			d.setVisible(true);
			// 获取选定文件
			File f = null;
			f = new File(d.getDirectory() + d.getFile());
			// ...
			for (int i = 0; i <= f.length(); i++) {
				char[] ch = new char[i];
				System.out.println(Arrays.toString(ch));
				try {
					// 参数是文件的路径及文件名(默认是当前执行文件的路径)
					FileReader fr = new FileReader(f); 
					fr.read(ch);// 一次请读取数组长度的字符值(这里不是读取的数字)
					String str = new String(ch); // 复制字符数组中的内容
					System.out.println(str);
					ta.setText(str);// 显示在文本框中

				} catch (Exception e2) {
					// TODO: handle exception
//					System.out.println(e);
				}
			}
		}
		// 另存为
		if (e.getSource() == _fm4) {
			FileDialog d = new FileDialog(jf, "另存为", FileDialog.SAVE);
			d.setVisible(true);
			try {
				/**
				 * d.getDirectory() 获取此文件对话框的目录。 d.getFile() 获取此文件
				 * 对话框的选定文件。如果用户选择取消,则返回的文件为空
				 */
				filename = d.getDirectory() + d.getFile();
				FileOutputStream fout = new FileOutputStream(filename + ".txt");
				byte bb[] = ta.getText().getBytes();
				fout.write(bb);
				fout.close();
			} catch (Exception e2) {
				// TODO: handle exception
				System.err.println(e);
			}
		}
		// 关闭记事本
		if (e.getSource() == _fm3 || e.getSource() == jb1) {
			System.exit(0); // 用来结束当前正在运行中的java虚拟机
		}
		// 复制
		if (e.getSource() == _fe1) {
			copy = ta.getSelectedText();
		}
		// 粘贴
		if (e.getSource() == _fe2) {
			ta.setText(copy);
		}
		// 剪切
		if (e.getSource() == _fe3) {
			copy = ta.getSelectedText();
			ta.setText("");
		}
		// 作者
		if (e.getSource() == _fe4) {
			JOptionPane.showMessageDialog(jf, "作者: 小俊 \n ! 提醒   好好学习,天天向上 \\n学习JAVA很快乐!");
		}
	}

	public void windowClosing(WindowEvent e)

	{

		System.exit(0);// 将整个虚拟机里的内容都关闭

	}

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
java写的记事本,1000行代码,基本上所有的功能都全了(和微软系统自带的记事本的相似度>70%) 其中菜单里新建模块的代码如下: // 菜单 // 新建(N)按钮事件监听 newTextItem.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent event) { if (selectedFile == null && textArea.getText().equals("")) { return; } else { int btn = JOptionPane.showConfirmDialog(container, "是否保存到" + selectedFile + "?", "是否保存", JOptionPane.YES_NO_CANCEL_OPTION); if (btn == JOptionPane.CANCEL_OPTION) { return; } else if (btn == JOptionPane.YES_OPTION) { if (selectedFile == null && !textArea.getText().equals("")) { choose = new JFileChooser(); int state = choose.showSaveDialog(container); if (state == JFileChooser.APPROVE_OPTION) { try { File file = choose.getSelectedFile(); BufferedWriter bw = new BufferedWriter( new FileWriter(file)); String str = textArea.getText(); String[] lines = str.split("\n"); for (String line : lines) { bw.write(line + "\r\n"); } bw.flush(); file.createNewFile(); bw.close(); } catch (IOException e) { JOptionPane.showConfirmDialog(container, "保存文件失败!", "ERROR", JOptionPane.ERROR_MESSAGE); } } } else if (selectedFile != null) { try { BufferedWriter bw = new BufferedWriter( new FileWriter(selectedFile)); String str = textArea.getText(); String[] lines = str.split("\n"); for (String line : lines) { bw.write(line + "\r\n"); } bw.flush(); bw.close(); } catch (IOException e) { // JOptionPane.showConfirmDialog(container, // "保存文件失败!", // "ERROR", JOptionPane.ERROR_MESSAGE); } } } } textArea.setText(""); newPage = true; selectedFile = null; textField.setText(""); } }); 如果你初学或也在写记事本,这个绝对对你有帮助,且最适合你1

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值