Java实现简单的文本编辑器

1、首先创建一个编辑器EditorDemo类,继承JFrame类,具体实现编码如下:
在这里插入图片描述

2、创建菜单栏,新增各个菜单项,比如文件、编辑、帮助等功能,新增createJMenuBar方法如下:

/**
     * 创建菜单栏
     * @param actions
     * @return
     */
    private JMenuBar createJMenuBar(Action[] actions) {
        JMenuBar menubar = new JMenuBar(); //实例化菜单栏
        JMenu menuFile = new JMenu("文件"); //实例化菜单
        JMenu menuEdit = new JMenu("编辑");
        JMenu menuAbout = new JMenu("帮助");
        menuFile.add(new JMenuItem(actions[0])); //增加新菜单项
        menuFile.add(new JMenuItem(actions[1]));
        menuFile.add(new JMenuItem(actions[2]));
        menuFile.add(new JMenuItem(actions[7]));
        menuEdit.add(new JMenuItem(actions[3]));
        menuEdit.add(new JMenuItem(actions[4]));
        menuEdit.add(new JMenuItem(actions[5]));
        menuAbout.add(new JMenuItem(actions[6]));
        menubar.add(menuFile); //增加菜单
        menubar.add(menuEdit);
        menubar.add(menuAbout);
        return menubar; //返回菜单栏
    }

3、创建工具条,比如:新建、打开、剪切、保存、拷贝等功能,新增createJToolBar方法

/**
* 创建工具条
* @param actions
* @return
*/
private JToolBar createJToolBar(Action[] actions) {
JToolBar toolBar = new JToolBar(); //实例化工具条
for (int i = 0; i < actions.length; i++) {
JButton bt = new JButton(actions[i]); //实例化新的按钮
bt.setRequestFocusEnabled(false); //设置不需要焦点
toolBar.add(bt); //增加按钮到工具栏
}
return toolBar; //返回工具栏
}
4、具体操作功能实现,包括:新建文件命令、打开文件命令、保存命令、退出命令、剪切命令、拷贝命令、粘贴命令、关于选项命令,具体实现上述功能类编码如下:

/**
     * 新建文件命令
     */
    public class NewAction extends AbstractAction {
        public NewAction() {
            super("新建");
        }
        public void actionPerformed(ActionEvent e) {
            textPane.setDocument(new DefaultStyledDocument()); //清空文档
        }
    }

    /**
     * 打开文件命令
     */
    public class OpenAction extends AbstractAction {
        public OpenAction() {
            super("打开");
        }
        public void actionPerformed(ActionEvent e) {
            int i = filechooser.showOpenDialog(EditorDemo.this); //显示打开文件对话框
            if (i == JFileChooser.APPROVE_OPTION) { //点击对话框中打开选项
                File f = filechooser.getSelectedFile(); //得到选择的文件
                try {
                    InputStream is = new FileInputStream(f); //得到文件输入流
                    textPane.read(is, "d"); //读入文件到文本窗格
                } catch (Exception ex) {
                    ex.printStackTrace();  //输出出错信息
                }
            }
        }
    }

    /**
     * 保存命令
     */
    public class SaveAction extends AbstractAction {
        public SaveAction() {
            super("保存");
        }
        public void actionPerformed(ActionEvent e) {
            int i = filechooser.showSaveDialog(EditorDemo.this); //显示保存文件对话框
            if (i == JFileChooser.APPROVE_OPTION) {  //点击对话框中保存按钮
                File f = filechooser.getSelectedFile(); //得到选择的文件
                try {
                    FileOutputStream out = new FileOutputStream(f);  //得到文件输出流
                    out.write(textPane.getText().getBytes()); //写出文件
                } catch (Exception ex) {
                    ex.printStackTrace(); //输出出错信息
                }
            }
        }
    }

    /**
     * 退出命令
     */
    public class ExitAction extends AbstractAction {
        public ExitAction() {
            super("退出");
        }
        public void actionPerformed(ActionEvent e) {
            System.exit(0);  //退出程序
        }
    }

    /**
     * 剪切命令
     */
    public class CutAction extends AbstractAction {
        public CutAction() {
            super("剪切");
        }
        public void actionPerformed(ActionEvent e) {
            textPane.cut();  //调用文本窗格的剪切命令
        }
    }

    /**
     * 拷贝命令
     */
    public class CopyAction extends AbstractAction {
        public CopyAction() {
            super("拷贝");
        }
        public void actionPerformed(ActionEvent e) {
            textPane.copy();  //调用文本窗格的拷贝命令
        }
    }

    /**
     * 粘贴命令
     */
    public class PasteAction extends AbstractAction {
        public PasteAction() {
            super("粘贴");
        }
        public void actionPerformed(ActionEvent e) {
            //调用文本窗格的粘贴命令
            textPane.paste();
        }
    }

    /**
     * 关于选项命令
     */
    public class AboutAction extends AbstractAction {
        public AboutAction() {
            super("关于");
        }
        public void actionPerformed(ActionEvent e) {
            JOptionPane.showMessageDialog(EditorDemo.this, "简单的文本编辑器演示"); //显示软件信息
        }
    }

5、最后写一个main方法调用EditorDemo类,启动文本剪辑器

/**
     * 启动程序的main方法
     * @param args
     */
    public static void main(String[] args) {
        new EditorDemo();
    }

启动成功,可以尽情的码字了

在这里插入图片描述

参考文章:http://www.ysxbohui.com/article/23

  • 7
    点赞
  • 76
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

YT博烩

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

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

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

打赏作者

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

抵扣说明:

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

余额充值