Java实现一个简单的文本编辑器(简易版)

(用Java做了一个简单的文本编辑器,其中看了很多博主的教学和代码,在这里感谢:@Mark7758、@Kingsly_Liang、@佐敦不下雨。再次感谢!)

1.功能说明:

文件菜单:打开、保存、新建。
编辑菜单:复制、粘贴、删除、剪切。
帮助菜单:关于、操作说明。
格式菜单:换行切换。

2.代码实现:

①首先启动类:
package source;

import javax.swing.*;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
public class TestTextEditor {
	public static void main(String[] args) {
		NewTextFrame a=new NewTextFrame();
	}
}
②构造文本类:
package source;

import java.awt.event.*;
import javax.swing.*;
import java.awt.*;
import java.io.File;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.io.BufferedWriter;
import java.io.FileWriter;


class NewTextFrame extends JFrame{
	//构造要使用的变量
	private JMenuBar menuBar1;
	private JTextArea workArea;
	private JScrollPane scrollPane;
	private JMenu menu_file,menu_edit,menu_help,menu_format; //构造菜单栏
	private JMenuItem item_new,item_open,item_save,item_exit;//构造文件项
	private JMenuItem item_cut,item_copy,item_stick,item_delete,item_sum;//构造编辑项
	private JMenuItem item_about,item_help;//构造帮助项
	private JRadioButtonMenuItem item_format;//构造格式项
	FileDialog openDialog; //新建文件窗口变量
	FileDialog saveDialog;//保存文件窗口变量
	
	
	
	public NewTextFrame() {
		super("panbk的文本编辑器");  //相当于JFrame("panbk的文本编辑器")
		this.initJMenuBar();   //新建菜单栏
		
		workArea=new JTextArea();  //文本编辑区域
		scrollPane=new JScrollPane(workArea);  //滚动条
		this.add(scrollPane,BorderLayout.CENTER); 
		
		//分别在JFrame增加四个菜单选项,调用下面的方法
		this.initJMenuFile();
		this.initJMenuEdit();
		this.initJMenuHelp();
		this.initJMenuFormat();
		
		this.setSize(600,600);
		this.setVisible(true);
		this.setLocationRelativeTo(null);
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//关闭时也会释放程序资源
	}
	
	public void initJMenuBar() {
		menuBar1=new JMenuBar();
		this.setJMenuBar(menuBar1);
		menu_file=new JMenu("文件");
		menu_edit=new JMenu("编辑");
		menu_help=new JMenu("帮助");
		menu_format=new JMenu("格式");
		
		menuBar1.add(menu_file);
		menuBar1.add(menu_edit);
		menuBar1.add(menu_help);
		menuBar1.add(menu_format);
		
	}
	
	public void initJMenuFile() {
		
		//1.新建菜单选项
		item_new=new JMenuItem("新建");
		item_new.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				workArea.setText("");
			}
		});
		menu_file.add(item_new);
		
		//2.打开菜单选项
		
		//定义打开和保存对话框
		openDialog=new FileDialog(this,"打开",FileDialog.LOAD);
		saveDialog=new FileDialog(this,"另存为",FileDialog.SAVE);
				
		item_open=new JMenuItem("打开");
		item_open.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				openDialog.setVisible(true);
				String dirPath=openDialog.getDirectory();//获取文件路径 
				String fileName=openDialog.getFile();//获取文件名称 
				//如果打开路径或目录为空 返回空
				if(dirPath==null ||fileName==null){
					return ;
				}
				workArea.setText("");//清空文本
				File file1=new File(dirPath,fileName);
				
				try {
					BufferedReader bf1=new BufferedReader(new FileReader(file1));
					String line1=null;
					while((line1=bf1.readLine())!=null) {
						workArea.append(line1+"\r\n");
					}
					bf1.close();
				}catch(IOException ioex) {
					throw new RuntimeException("文件读取失败!");
				}
			}
		});
		menu_file.add(item_open);
		
		//3.保存菜单选项
		item_save=new JMenuItem("保存");
		item_save.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				File fileS=null;
				if(fileS==null) {
					saveDialog.setVisible(true);
					String dirPath=saveDialog.getDirectory();//获取文件路径 
					String fileName=saveDialog.getFile();//获取文件名称 
					//如果路径或目录为空 返回空
					if(dirPath==null ||fileName==null) {
						return;
					}
					fileS=new File(dirPath,fileName);
				}
				
				try {
					BufferedWriter bfS=new BufferedWriter(new FileWriter(fileS));
					String strWrite=workArea.getText();
					bfS.write(strWrite);
					bfS.close();
				}catch(IOException ioex) {
					throw new RuntimeException("文件保存失败");
				}
			}
		});
		
		menu_file.add(item_save);
		
		
		//4.退出菜单选项
		item_exit=new JMenuItem("退出");
		item_exit.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				System.exit(0);
			}
		});
		
		menu_file.add(item_exit);
	}
	public void initJMenuEdit() {
		//1.复制
		item_copy=new JMenuItem("复制");
		item_copy.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				workArea.copy();
			}
		});
		menu_edit.add(item_copy);
		
		//2.剪切
		item_cut=new JMenuItem("剪切");
		item_cut.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				workArea.cut();
			}
		});
		
		menu_edit.add(item_cut);
		
		//3.粘贴
		item_stick=new JMenuItem("粘贴");
		item_stick.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				workArea.paste();
			}
		});
		
		menu_edit.add(item_stick);
		
		
		//4.删除
		item_delete=new JMenuItem("删除");
		item_delete.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				workArea.replaceSelection("");
			}
		});
		
		menu_edit.add(item_delete);
		
		//5.统计
		item_sum=new JMenuItem("统计");
		item_sum.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				String a=workArea.getSelectedText();
				System.out.println(a.replace(" ","").replace("\n","").length());
			}
		});
		
		menu_edit.add(item_sum);
	}
	
	public void initJMenuHelp() {
		//1.item_help
		item_help=new JMenuItem("查看帮助");
		menu_help.add(item_help);
		item_help.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent ex) {
				
				new LookHelp();	
			}
			
		});
		
		
		//2.item_about
		item_about=new JMenuItem("关于");
		menu_help.add(item_about);
		item_about.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent ex) {
				new LookAbout();	
			}
		});
	}
	
	public void initJMenuFormat() {
		item_format=new JRadioButtonMenuItem("自动换行",false);
		menu_format.add(item_format);
		
		item_format.addActionListener(new ActionListener() {
			int isno=0;
			public void actionPerformed(ActionEvent e) {
				//getSource
				if(e.getSource()==item_format && isno==0) {
					workArea.setLineWrap(true);
					isno=1;
				}
				else {
					workArea.setLineWrap(false);
					isno=0;
				}
			}
		});
	}
}
③“查看帮助”选项“item_help"实现
package source;


import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.*;

public class LookHelp extends JFrame{
	public LookHelp() {
		super("查看帮助");
		JPanel helpPane;
		helpPane=new JPanel();
		this.getContentPane().add(helpPane);
		
		//垂直显示
		BoxLayout layOut1=new BoxLayout(helpPane,BoxLayout.Y_AXIS);
		helpPane.setLayout(layOut1);
		
		JLabel label1 = new JLabel("1、可新建、保存、打开文件使用。");
		JLabel label2 = new JLabel("2、剪切、复制、粘贴可用快捷键Ctrl + x、Ctrl + c、Ctrl + v实现。");
		JLabel label3 = new JLabel("3、“自动换行”选择进行自动换行、取消换行。");
		JLabel label4 = new JLabel("4、“统计”用来统计选中文本的非空字数");
		JLabel label5 = new JLabel("5、祝您使用愉快。");
		
		
		helpPane.add(label1);
		helpPane.add(label2);
		helpPane.add(label3);
		helpPane.add(label4);
		helpPane.add(label5);
		this.add(helpPane);
		this.setSize(300, 300);
		this.setVisible(true);
		this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		
	}
}
④“关于”选项“item_about”实现:
package source;

import javax.swing.*;
import java.awt.*;
public class LookAbout extends JFrame{
	public LookAbout() {
		super("关于记事本");
		JPanel aboutPane;
		aboutPane=new JPanel();
		this.getContentPane().add(aboutPane);
		
		//垂直显示
		BoxLayout layOut1=new BoxLayout(aboutPane,BoxLayout.Y_AXIS);
		aboutPane.setLayout(layOut1);
		
		JLabel label1 = new JLabel("1、这是一个简单的编辑器");
		JLabel label2 = new JLabel("2、是的,就是这样");
		
		aboutPane.add(label1);
		aboutPane.add(label2);
		this.add(aboutPane);
		this.setSize(300, 300);
		this.setVisible(true);
		this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
	}
}
  • 1
    点赞
  • 39
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

_panbk_

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值