JAVA编写实现记事本保存、另存为、查找、替换等功能

直接上图
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
通过学习JAVA基础知识就可以慢慢练习着做这类有趣的事情
自己可以选择写一些小游戏

通过Swing框架,实现图形界面,在图形界面中添加文本框架实现文本编辑,通过鼠标菜单栏添加事件监听实现保存、另存为、查找、替换等功能

部分源码展示

public  static void main(String[] arg) {
 Inter inter=new Inter();
 }
 	class Inter implements ActionListener{        //记事本界面设计   菜单栏的实现
		JMenu menu1,menu2,menu3,menu4,menu5;
	JMenuItem 
  • 15
    点赞
  • 115
    收藏
    觉得还不错? 一键收藏
  • 17
    评论
很高兴回答您的问题。首先,我需要说明的是,这里提供的是一个基本的记事本程序,您可以根据自己的需求进行修改和优化。 Java提供了一些类来读写文件,比如File、FileReader、FileWriter、BufferedReader、BufferedWriter等。这里我们使用FileReader和FileWriter类来读写文件。 以下是一个简单的Java记事本程序的实现,包括编辑、保存另存为查找替换功能。 ```java import java.io.*; public class Notepad { private static File file; private static boolean isSaved = false; private static BufferedReader reader; private static BufferedWriter writer; private static String content; public static void main(String[] args) { BufferedReader input = new BufferedReader(new InputStreamReader(System.in)); String command; System.out.println("Welcome to Notepad!"); while (true) { System.out.print("Enter a command (open, edit, save, saveas, find, replace, quit): "); try { command = input.readLine(); } catch (IOException e) { System.out.println("Error reading command."); continue; } if (command.equalsIgnoreCase("open")) { open(); } else if (command.equalsIgnoreCase("edit")) { edit(); } else if (command.equalsIgnoreCase("save")) { save(); } else if (command.equalsIgnoreCase("saveas")) { saveAs(); } else if (command.equalsIgnoreCase("find")) { find(); } else if (command.equalsIgnoreCase("replace")) { replace(); } else if (command.equalsIgnoreCase("quit")) { if (!isSaved) { System.out.print("Do you want to save changes? (y/n): "); try { command = input.readLine(); } catch (IOException e) { System.out.println("Error reading command."); continue; } if (command.equalsIgnoreCase("y")) { save(); } } System.out.println("Goodbye!"); break; } else { System.out.println("Invalid command."); } } } private static void open() { BufferedReader input = new BufferedReader(new InputStreamReader(System.in)); System.out.print("Enter filename: "); try { String filename = input.readLine(); file = new File(filename); reader = new BufferedReader(new FileReader(file)); String line; StringBuilder sb = new StringBuilder(); while ((line = reader.readLine()) != null) { sb.append(line).append(System.lineSeparator()); } content = sb.toString(); System.out.println("File opened successfully."); isSaved = true; } catch (IOException e) { System.out.println("Error opening file."); } } private static void edit() { BufferedReader input = new BufferedReader(new InputStreamReader(System.in)); System.out.println("Enter text (type 'exit' to stop editing): "); StringBuilder sb = new StringBuilder(); try { String line; while (!(line = input.readLine()).equalsIgnoreCase("exit")) { sb.append(line).append(System.lineSeparator()); } content = sb.toString(); isSaved = false; } catch (IOException e) { System.out.println("Error editing text."); } } private static void save() { if (file == null) { saveAs(); } else { try { writer = new BufferedWriter(new FileWriter(file)); writer.write(content); writer.close(); System.out.println("File saved successfully."); isSaved = true; } catch (IOException e) { System.out.println("Error saving file."); } } } private static void saveAs() { BufferedReader input = new BufferedReader(new InputStreamReader(System.in)); System.out.print("Enter filename: "); try { String filename = input.readLine(); file = new File(filename); writer = new BufferedWriter(new FileWriter(file)); writer.write(content); writer.close(); System.out.println("File saved successfully."); isSaved = true; } catch (IOException e) { System.out.println("Error saving file."); } } private static void find() { BufferedReader input = new BufferedReader(new InputStreamReader(System.in)); System.out.print("Enter search string: "); try { String search = input.readLine(); int index = content.indexOf(search); if (index == -1) { System.out.println("String not found."); } else { System.out.println("String found at position " + index + "."); } } catch (IOException e) { System.out.println("Error finding string."); } } private static void replace() { BufferedReader input = new BufferedReader(new InputStreamReader(System.in)); System.out.print("Enter search string: "); try { String search = input.readLine(); System.out.print("Enter replacement string: "); String replace = input.readLine(); content = content.replace(search, replace); isSaved = false; } catch (IOException e) { System.out.println("Error replacing string."); } } } ``` 希望能帮到您,如有疑问请随时提出。
评论 17
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值