Java完整实现记事本代码

 更新时间:2022年06月16日 10:46:07   作者:工藤学编程  
这篇文章主要介绍了Java实现的简易记事本,较为详细的分析了基于java实现记事本程序的完整过程,具有一定参考借鉴价值,需要的朋友可以参考下

进入今天的正题:

1.整体设计思路如下:

(1)使用顶层容器JFrame。

(2)设置功能菜单并通过BorderLayout进行边框布局管理。

(3)设置相应按钮与文件编辑区。

(4)进行相应事件处理。

2.各功能菜单设计思路:

(1)打开功能:

用户点击打开后,可以选择文件中对应的txt或dat文件,用户确定选择后即可打开改文件并展示文件中的内容,并在程序正上方展示当前文件路径。

(2)新建功能: 用户点击新建功能后,将展示一个空白的记事本,用户可进行相应编辑。

(3)保存功能: 用户点击保存后,如果保存的文件已经存在路径,则直接进行覆盖,若不存在,则需用户自己选择保存的路径,并对保存的文件进行命名。

(4)设定循环加解密规则如下:按照ASCII字符编码(0-255),加密时对每一字符+10,(若超过255,减去255),解密时作对应反变换。我们可以在文件I/O时进行相应操作。 再也不用担心妈妈偷看你的笔记本啦✌️✌️✌️

简单的运行示例如下,其他的大家可以自行测试:

保存后的txt文件是这样滴:

注意:用程序打开时是会正常显示哦!因为在读取的时候也做了相应解密。

例如,这是打开的,所以有了他,是不是在也不用怕小秘密被别人知道啦!!!😉😉😉

话不多说,上源码:

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Font;
import java.awt.GraphicsEnvironment;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JColorChooser;
import javax.swing.JComboBox;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JToolBar;
import javax.swing.filechooser.FileNameExtensionFilter;
import chenhao.io.TextTool;
public class TextPad {
	private JTextArea contentArea;
	private JFrame frame;
	private String fileName;
	public TextPad() {
		frame = new JFrame("记事本");
		frame.setSize(500, 500);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		// 添加菜單
		JMenuBar menuBar = new JMenuBar();
		JMenu menu = new JMenu("文件");
		JMenuItem newItem = new JMenuItem("新建");
		newAction(newItem);
		menu.add(newItem);
		JMenuItem openItem = new JMenuItem("打开");
		openAction(openItem);
		menu.add(openItem);
		JMenuItem saveItem = new JMenuItem("保存");
		saveAction(saveItem);
		menu.add(saveItem);
		menuBar.add(menu);
		frame.setJMenuBar(menuBar);
		// 布局
		frame.setLayout(new BorderLayout());
		JToolBar toolBar = new JToolBar();
		JComboBox<String> fontCom = fontAction();
		toolBar.add(fontCom);
		JComboBox<String> fontSize = fontSizeAction();
		toolBar.add(fontSize);
		fontStyleAction(toolBar);
		JButton colorbtn = fontColorAction();
		toolBar.add(colorbtn);
		frame.add(toolBar, BorderLayout.NORTH);
		// 文件编辑区
		contentArea = new JTextArea();
		JScrollPane pane = new JScrollPane(contentArea);
		frame.add(pane);
		frame.setVisible(true);
	}
	private JButton fontColorAction() {
		JButton colorbtn = new JButton("■");
		colorbtn.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				Color color = colorbtn.getForeground();
				Color co = JColorChooser.showDialog(TextPad.this.frame, "设置字体颜色", color);
                 colorbtn.setForeground(co);
                 contentArea.setForeground(co);
			}
		});
		return colorbtn;
	}
	// 记事本,字体格式
	private void fontStyleAction(JToolBar toolBar) {
		JCheckBox boldBox = new JCheckBox("粗体");
		JCheckBox itBox = new JCheckBox("斜体");
		ActionListener actionListener = new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				boolean bold = boldBox.isSelected();
				boolean it = itBox.isSelected();
				int style = (bold ? Font.BOLD : Font.PLAIN) | (it ? Font.ITALIC : Font.PLAIN);
				Font font = contentArea.getFont();
				contentArea.setFont(new Font(font.getName(), style, font.getSize()));
				//contentArea.setFont(new Font(font.getName(), style, font.getSize()));
			}
		};
		boldBox.addActionListener(actionListener);
		itBox.addActionListener(actionListener);
		toolBar.add(boldBox);
		toolBar.add(itBox);
	}
	// 记事本,设置字体大小
	private JComboBox<String> fontSizeAction() {
		String[] fontSizes = new String[] { "10", "20", "30", "50" };
		JComboBox<String> fontSize = new JComboBox<>(fontSizes);
		fontSize.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				int size = Integer.valueOf((String) fontSize.getSelectedItem());
				Font font = TextPad.this.contentArea.getFont();
				TextPad.this.contentArea.setFont(new Font(font.getName(), font.getStyle(), size));
			}
		});
		return fontSize;
	}
	// 记事本,设置字体
	private JComboBox<String> fontAction() {
		GraphicsEnvironment environment = GraphicsEnvironment.getLocalGraphicsEnvironment();
		String[] fontNames = environment.getAvailableFontFamilyNames();
		JComboBox<String> fontCom = new JComboBox<>(fontNames);
		fontCom.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				String fontName = (String) fontCom.getSelectedItem();
				Font font = TextPad.this.contentArea.getFont();
				TextPad.this.contentArea.setFont(new Font(fontName, font.getStyle(), font.getSize()));
			}
		});
		return fontCom;
	}
	// 记事本新建操作
	private void newAction(JMenuItem newItem) {
		newItem.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				contentArea.setText("");
				frame.setTitle("新建-记事本");
				fileName = null;
			}
		});
	}
	// 记事本打开文件操作
	private void openAction(JMenuItem openItem) {
		openItem.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				JFileChooser chooser = new JFileChooser();
				FileNameExtensionFilter filter = new FileNameExtensionFilter("Text & dat", "txt", "dat");
				chooser.setFileFilter(filter);
				int returnVal = chooser.showOpenDialog(frame);
				if (returnVal == JFileChooser.APPROVE_OPTION) {
					String fileName = chooser.getSelectedFile().getPath();
					TextPad.this.fileName = fileName;
					String content = TextTool.read(fileName);
					contentArea.setText(content);
					TextPad.this.frame.setTitle(fileName + "- 记事本");
				}
			}
		});
	}
	// 菜单 保存操作
	private void saveAction(JMenuItem saveItem) {
		saveItem.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				if (TextPad.this.fileName != null) {
					String content = TextPad.this.contentArea.getText();
					TextTool.write(TextPad.this.fileName, content);
				} else {
					JFileChooser chooser = new JFileChooser();
					FileNameExtensionFilter filter = new FileNameExtensionFilter("Text & dat", "txt", "dat");
					chooser.setFileFilter(filter);
					int returnVal = chooser.showSaveDialog(frame);
					if (returnVal == JFileChooser.APPROVE_OPTION) {
						String fileName = chooser.getSelectedFile().getPath();
						TextPad.this.fileName = fileName;
						String content = TextPad.this.contentArea.getText();
						TextTool.write(TextPad.this.fileName, content);
						TextPad.this.frame.setTitle(fileName + "- 记事本");
					}
				}
			}
		});
	}
	public static void main(String[] args) {
		TextPad pad = new TextPad();
	}
}
import java.awt.image.BufferedImage;
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Reader;
import java.io.Writer;
import javax.swing.JOptionPane;
public class TextTool {
	public static String read(String fileName) {
		try (Reader reader = new FileReader(fileName); BufferedReader buff = new BufferedReader(reader);) {
			String str;
			StringBuilder sb = new StringBuilder();
			while ((str = buff.readLine()) != null) {
				str = decoding(str);
				sb.append(str + "\n");
			}

			return sb.toString();
		} catch (FileNotFoundException e) {
			JOptionPane.showMessageDialog(null, "找不到文件路径" + fileName);
		} catch (IOException e) {
			e.printStackTrace();
		}
		return null;
	}
	public static void write(String fileName, String content) {

		try (Writer writer = new FileWriter(fileName);) {
			content = encoding(content);
			writer.write(content);
			writer.flush();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	public static String encoding(String str) {
		String temp = "";
		for (int i = 0; i < str.length(); i++) {
			if(str.charAt(i)=='\n')
			{
				temp+=str.charAt(i);
			}
			else if (0 <= str.charAt(i) && str.charAt(i) <= 255)
				temp += (char) ((str.charAt(i) - '0' + 10) % 255);
			else
				temp += str.charAt(i);
		}
		return temp;
	}
	public static String decoding(String str) {
		String temp = "";
		for (int i = 0; i < str.length(); i++) {
			if(str.charAt(i)=='\n')
			{
				temp+=str.charAt(i);
			}
			else if (0 <= str.charAt(i) && str.charAt(i) <= 255)
				temp += (char) ((str.charAt(i) + '0' - 10 + 255) % 255);
			else
				temp += str.charAt(i);
		}
		return temp;
	}
}

到此这篇关于Java完整实现记事本代码的文章就介绍到这了,更多相关Java记事本内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

相关文章

  • Java中实现文件上传下载的三种解决方案(推荐)

    Java中实现文件上传下载的三种解决方案(推荐)

    这篇文章主要介绍了Java中实现文件上传下载的三种解决方案的相关资料,非常不错,具有参考借鉴价值,需要的朋友可以参考下
    2016-07-07
  • java实现文件夹上传功能实例代码(SpringBoot框架)

    java实现文件夹上传功能实例代码(SpringBoot框架)

    在web项目中上传文件夹现在已经成为了一个主流的需求,下面这篇文章主要给大家介绍了关于java实现文件夹上传功能(springBoot框架)的相关资料,文中通过实例代码介绍的非常详细,需要的朋友可以参考下
    2023-04-04
  • SpringBoot使用Quartz无法注入Bean的问题及解决

    SpringBoot使用Quartz无法注入Bean的问题及解决

    这篇文章主要介绍了SpringBoot使用Quartz无法注入Bean的问题及解决方案,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教
    2023-11-11
  • 手写Java LockSupport的示例代码

    手写Java LockSupport的示例代码

    LockSupport给我们提供了一个非常强大的功能,它是线程阻塞最基本的元语,他可以将一个线程阻塞也可以将一个线程唤醒,因此经常在并发的场景下进行使用。本文将用60行代码实现手写LockSupport,需要的可以参考一下
    2022-08-08
  • Java MongoDB数据库连接方法梳理

    Java MongoDB数据库连接方法梳理

    MongoDB作为一种介于关系型数据库和非关系型数据库之间的产品,它可以提供可扩展的高性能的数据存储解决方案,近些年来受到了开发者的喜爱
    2022-08-08
  • java信号量控制线程打印顺序的示例分享

    java信号量控制线程打印顺序的示例分享

    这篇文章主要介绍了java信号量控制线程打印顺序的示例,如ABCABC这样输出线程,大家参考使用吧
    2014-01-01
  • JavaSE的类和对象你真的了解吗

    JavaSE的类和对象你真的了解吗

    这篇文章主要为大家详细介绍了JavaSE的类和对象,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下,希望能够给你带来帮助
    2022-03-03
  • Java字符串常量池示例详解

    Java字符串常量池示例详解

    作为最基础的引用数据类型,Java设计者为 String 提供了字符串常量池以提高其性能,下面这篇文章主要给大家介绍了关于Java字符串常量池的相关资料,需要的朋友可以参考下
    2021-08-08
  • 深入了解Spring中Bean的作用域和生命周期

    深入了解Spring中Bean的作用域和生命周期

    这篇文章主要介绍了深入了解Spring中Bean的作用域和生命周期,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2019-10-10
  • JAVA版排序算法之快速排序示例

    JAVA版排序算法之快速排序示例

    这篇文章主要介绍了JAVA版排序算法之快速排序,结合实例形式分析了基于java版的遍历、递归实现快速排序功能的具体步骤与操作技巧,需要的朋友可以参考下
    2017-01-01

最新评论