• Java实现简单记事本


    新建Java源代码格式文件,直接上代码:

    import java.awt.*;
    import java.awt.event.*;
    import java.io.*;
    import javax.swing.*;
    import javax.swing.text.*;
     
    //简单的文本编辑器
     
    public class Editor extends JFrame {
        public JTextPane textPane = new JTextPane(); //文本窗格d,编辑窗口
        public JFileChooser filechooser = new JFileChooser(); //文件选择器
     
        public Editor()
        {
            super("记事本");
            
            Action[] actions=            //菜单项的各种功能
                {
                        new NewAction(),
                        new OpenAction(),
                        new SaveAction(),
                        new CutAction(),
                        new CopyAction(),
                        new PasteAction(),
                        new AboutAction(),
                        new ExitAction(),
                        new HelpAction()
                };
            setJMenuBar(createJMenuBar(actions));        //根据actions创建菜单栏
            Container container=getContentPane();        
            container.add(textPane, BorderLayout.CENTER);
            
            setSize(800, 600);
            setVisible(true);
        //    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        }
     
        private JMenuBar createJMenuBar(Action[] actions)    //创建菜单栏的函数
        {
            JMenuBar menubar=new JMenuBar();
            JMenu menuFile=new JMenu("文件(F)");
            JMenu menuEdit=new JMenu("编辑(E)");
            JMenu menuAbout=new JMenu("帮助(H)");
            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]));
            menuAbout.add(new JMenuItem(actions[8]));
            menubar.add(menuFile);
            menubar.add(menuEdit);
            menubar.add(menuAbout);
            return menubar;
        }
     
        class NewAction extends AbstractAction        //新建
        {
            public NewAction()
            {
                super("新建(N)     Ctrl+N");
            }
            public void actionPerformed(ActionEvent e)
            {
                textPane.setDocument(new DefaultStyledDocument());
            }
        }
     
        class OpenAction extends AbstractAction        //打开
        {
            public OpenAction()
            {
                super("打开(O)     Ctrl+O");
            }
            public void actionPerformed(ActionEvent e)
            {
                int i=filechooser.showOpenDialog(Editor.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();
                    }
                }
            }
        }
     
        class SaveAction extends AbstractAction        //保存
        {
            public SaveAction()
            {
                super("保存(S)     Ctrl+S");
            }
            public void actionPerformed(ActionEvent e)
            {
                int i=filechooser.showSaveDialog(Editor.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();
                    }
                }
            }
        }
     
        class ExitAction extends AbstractAction        //退出
        {
            public ExitAction()
            {
                super("退出(X)");
            }
            public void actionPerformed(ActionEvent e)
            {
                dispose();
            }
        }
     
        class CutAction extends AbstractAction        //剪切
        {
            public CutAction()
            {
                super("剪切(T)     Ctrl+X");
            }
            public void actionPerformed(ActionEvent e)
            {
                textPane.cut();
            }
        }
        
        class CopyAction extends AbstractAction        //复制
        {
            public CopyAction()
            {
                super("复制(C)     Ctrl+C");
            }
            public void actionPerformed(ActionEvent e)
            {
                textPane.copy();
            }
        }
     
        class PasteAction extends AbstractAction        //粘贴
        {
            public PasteAction()
            {
                super("粘贴(P)     Ctrl+V");
            }
            public void actionPerformed(ActionEvent e)
            {
                textPane.paste();
            }
        }
     
        class AboutAction extends AbstractAction
        {
            public AboutAction()
            {
                super("关于记事本(A)");
            }
            public void actionPerformed(ActionEvent e)
            {
                JOptionPane.showMessageDialog(Editor.this,"实现了记事本的一些基本功能","关于",JOptionPane.PLAIN_MESSAGE);
            }
        }
        
        class HelpAction extends AbstractAction
        {
            public HelpAction()
            {
                super("联系开发者");
            }
            public void actionPerformed(ActionEvent e)
            {
                JOptionPane.showMessageDialog(Editor.this,"meteor_disappears@foxmail.com","开发者:王子辰",JOptionPane.PLAIN_MESSAGE);
            }
        }
        
        public static void main(String[] args)
        {
            new Editor();
        }
    }

    保存完后 javac --> java 即可直接使用。

  • 相关阅读:
    Ansible安装配置
    Git 工作流程
    使用 Docker 搭建 Tomcat 运行环境
    Linux的cron与%
    配置sonar和jenkins进行代码审查
    Jenkins配置基于角色的项目权限管理
    Jenkins和maven自动化构建java程序
    Jenkins修改workspace和build目录
    Git 进阶指南
    git代码回滚:Reset、Checkout、Revert的选择
  • 原文地址:https://www.cnblogs.com/meteor-disappears-chen/p/13174086.html
Copyright © 2020-2023  润新知