美文网首页hello,world!
(JAVA)记事本

(JAVA)记事本

作者: 霙愔 | 来源:发表于2016-10-14 16:34 被阅读41次

    /*

     * 记事本程序

     * 编写时间:2010.3.12

     */

    import java.awt.BorderLayout;

    import java.awt.Container;

    import java.awt.FlowLayout;

    import java.awt.Font;

    import java.awt.GraphicsEnvironment;

    import java.awt.event.ActionEvent;

    import java.awt.event.ActionListener;

    import java.awt.event.InputEvent;

    import java.awt.event.KeyEvent;

    import java.util.logging.Level;

    import java.util.logging.Logger;

    import javax.swing.JFileChooser;

    import javax.swing.JFrame;

    import javax.swing.JMenu;

    import javax.swing.JMenuBar;

    import javax.swing.JMenuItem;

    import javax.swing.JOptionPane;

    import javax.swing.JScrollPane;

    import javax.swing.JTextArea;

    import java.io.*;

    import javax.swing.JButton;

    import javax.swing.JComboBox;

    import javax.swing.JPanel;

    import javax.swing.KeyStroke;

    public class TextFrame extends JFrame{

        private JFrame frame;

        private JTextArea jta;

        private JScrollPane jsp;

        private JMenu wj;

        private JMenu bj;

        private JMenu gs;

        private JMenu ck;

        private JMenu bz;

        private JMenuBar jmb;

        private JMenuItem xj;

        private JMenuItem dk;

        private JMenuItem bc;

        private JMenuItem lcw;

        private JMenuItem ymsz;

        private JMenuItem dy;

        private JMenuItem tc;

        private JMenuItem cx;

        private JMenuItem jq;

        private JMenuItem fz;

        private JMenuItem zt;

        private JMenuItem sc;

        private JMenuItem cz;

        private JMenuItem czxyg;

        private JMenuItem th;

        private JMenuItem zd;

        private JMenuItem qx;

        private JMenuItem sj;

        private JMenuItem zdhh;

        private JMenuItem zti;

        private JMenuItem ztl;

        private JMenuItem bzzt;

        private JMenuItem gy;

        private String lj = "";

       public TextFrame(){

       frame = new JFrame("Java记事本");

       frame.setLocation(100, 100);

       Container c = frame.getContentPane();

       jta = new JTextArea(30,60);

       jsp = new JScrollPane(jta);

       wj = new JMenu("文件(F)");//工具栏

       bj = new JMenu("编辑(E)");

       gs = new JMenu("格式(O)");

       ck = new JMenu("查看(V)");

       bz = new JMenu("帮助(H)");

       wj.setMnemonic('F');//为工具栏设置快捷键(Alt+'  ')

       gs.setMnemonic('O');//为工具栏设置快捷键(Alt+'  ')

       ck.setMnemonic('V');//为工具栏设置快捷键(Alt+'  ')

       bj.setMnemonic('E');//为工具栏设置快捷键(Alt+'  ')

       bz.setMnemonic('H');//为工具栏设置快捷键(Alt+'  ')

       jmb = new JMenuBar();

       xj = new JMenuItem("新建(N)");

       dk = new JMenuItem("打开(O)…");

       bc = new JMenuItem("保存(S)");

       lcw = new JMenuItem("另存为(A)…");

       ymsz = new JMenuItem("页面设置(U)…");

       dy = new JMenuItem("打印(P)…");

       tc = new JMenuItem("退出(X)");

       xj.setMnemonic('N');//设置快捷键(' ')

       xj.setAccelerator(

               KeyStroke.getKeyStroke(

               KeyEvent.VK_N,InputEvent.CTRL_MASK));//设置快捷键(ctl+" ")

       dk.setMnemonic('O');//设置快捷键(' ')

       dk.setAccelerator(

               KeyStroke.getKeyStroke(

               KeyEvent.VK_O,InputEvent.CTRL_MASK));//设置快捷键(ctl+" ")

       bc.setMnemonic('S');//设置快捷键(' ')

       bc.setAccelerator(

               KeyStroke.getKeyStroke(

               KeyEvent.VK_S,InputEvent.CTRL_MASK));//设置快捷键(ctl+" ")

       lcw.setMnemonic('A');//设置快捷键(' ')

       ymsz.setMnemonic('U');//设置快捷键(' ')

       dy.setMnemonic('P');//设置快捷键(' ')

       dy.setAccelerator(

               KeyStroke.getKeyStroke(

               KeyEvent.VK_P,InputEvent.CTRL_MASK));//设置快捷键(ctl+" ")

       tc.setMnemonic('X');//设置快捷键(' ')

       wj.add(xj);

       wj.add(dk);

       wj.add(bc);

       wj.add(lcw);

       wj.add(ymsz);

       wj.add(dy);

       wj.add(tc);

       cx = new JMenuItem("撤消(U)");

       jq = new JMenuItem("剪切(T)");

       fz = new JMenuItem("复制(C)");

       zt = new JMenuItem("粘贴(P)");

       sc = new JMenuItem("删除(L)");

       cz = new JMenuItem("查找(F)");

       czxyg = new JMenuItem("查找下一个(N)");

       th = new JMenuItem("替换(R)");

       zd = new JMenuItem("转到(G)");

       qx = new JMenuItem("全选(A)");

       sj = new JMenuItem("时间/日期(D)");

       cx.setMnemonic('U');//设置快捷键(' ')

       cx.setAccelerator(

               KeyStroke.getKeyStroke(

               KeyEvent.VK_Z,InputEvent.CTRL_MASK));//设置快捷键(ctl+" ")

       jq.setMnemonic('T');//设置快捷键(' ')

       jq.setAccelerator(

               KeyStroke.getKeyStroke(

               KeyEvent.VK_X,InputEvent.CTRL_MASK));//设置快捷键(ctl+" ")

       fz.setMnemonic('C');//设置快捷键(' ')

       fz.setAccelerator(

               KeyStroke.getKeyStroke(

               KeyEvent.VK_C,InputEvent.CTRL_MASK));//设置快捷键(ctl+" ")

       zt.setMnemonic('P');//设置快捷键(' ')

       zt.setAccelerator(

               KeyStroke.getKeyStroke(

               KeyEvent.VK_V,InputEvent.CTRL_MASK));//设置快捷键(ctl+" ")

       sc.setMnemonic('L');//设置快捷键(' ')

       sc.setAccelerator(

               KeyStroke.getKeyStroke(

               KeyEvent.VK_DELETE,0));//设置快捷键(DELETE)

       cz.setMnemonic('F');//设置快捷键(' ')

       cz.setAccelerator(

               KeyStroke.getKeyStroke(

               KeyEvent.VK_F,InputEvent.CTRL_MASK));//设置快捷键(ctl+" ")

       czxyg.setMnemonic('N');//设置快捷键(' ')

       czxyg.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_F3,0));//设置快捷键(F3)

       th.setMnemonic('R');//设置快捷键(' ')

       th.setAccelerator(

               KeyStroke.getKeyStroke(

               KeyEvent.VK_H,InputEvent.CTRL_MASK));//设置快捷键(ctl+" ")

       zd.setMnemonic('G');//设置快捷键(' ')

       zd.setAccelerator(

               KeyStroke.getKeyStroke(

               KeyEvent.VK_G,InputEvent.CTRL_MASK));//设置快捷键(ctl+" ")

       qx.setMnemonic('A');//设置快捷键(' ')

       qx.setAccelerator(

               KeyStroke.getKeyStroke(

               KeyEvent.VK_A,InputEvent.CTRL_MASK));//设置快捷键(ctl+" ")

       sj.setMnemonic('D');//设置快捷键(' ')

       sj.setAccelerator(

               KeyStroke.getKeyStroke(

               KeyEvent.VK_F5,0));//设置快捷键(F5)

       bj.add(cx);

       bj.add(jq);

       bj.add(fz);

       bj.add(zt);

       bj.add(sc);

       bj.add(cz);

       bj.add(czxyg);

       bj.add(th);

       bj.add(zd);

       bj.add(qx);

       bj.add(sj);

       zdhh = new JMenuItem("自动换行(W)");//设置快捷键(' ')

       zti = new JMenuItem("字体(F)");//设置快捷键(' ')

       zdhh.setMnemonic('W');//设置快捷键(' ')

       zti.setMnemonic('F');//设置快捷键(' ')

       gs.add(zdhh);

       gs.add(zti);

       ztl = new JMenuItem("状态栏(S)");//设置快捷键(' ')

       ztl.setMnemonic('S');//设置快捷键(' ')

       ck.add(ztl);

       bzzt = new JMenuItem("帮助主题(H)");

       gy = new JMenuItem("关于记事本(A)");

       bzzt.setMnemonic('H');//设置快捷键(' ')

       gy.setMnemonic('A');//设置快捷键(' ')

       bz.add(bzzt);

       bz.add(gy);

       jmb.add(wj);

       jmb.add(bj);

       jmb.add(gs);

       jmb.add(ck);

       jmb.add(bz);

       c.add(jmb,BorderLayout.NORTH);

       c.add(jsp,BorderLayout.CENTER);

       frame.setVisible(true);//让窗体可见

       frame.setResizable(false);//禁止窗体放大

       frame.pack();//根据窗口里面的布局及组件的preferedSize来确定frame的最佳大小

       jta.requestFocusInWindow();//输入文本域得到焦点

       /*各菜单项的单击事件*/

       //新建

       xj.addActionListener(new ActionListener(){

           public void actionPerformed(ActionEvent e){

               lj = "";//先让路径为空

               String nr = jta.getText();

               if(!nr.equals("")){//文本域里文本不为空

                   int result = JOptionPane.showConfirmDialog(

                           frame, ("是否要保存?"),("保存文件"),JOptionPane.YES_NO_CANCEL_OPTION);

                   if(result == JOptionPane.NO_OPTION){//不保存

                       jta.setText("");

                   }

                   else if(result == JOptionPane.CANCEL_OPTION){//取消新建

                   }

                   else if(result == JOptionPane.YES_OPTION)//选择保存

                   {

                       JFileChooser jfc = new JFileChooser();//用于选择保存路径的文件名

                       int bcf = jfc.showSaveDialog(frame);

                       if(bcf == JFileChooser.APPROVE_OPTION){

                                try {

                                    //保存文件

                                    BufferedWriter bfw = new BufferedWriter(

                                            new FileWriter(new File(jfc.getSelectedFile().getAbsolutePath()+".txt")));

                                    lj = jfc.getSelectedFile().getAbsolutePath()+".txt";//获取文件保存的路径

                                    bfw.write(nr);//向文件写出数据

                                    bfw.flush();

                                    bfw.close();//关闭输出流

                                } catch (IOException ex) {

                                    Logger.getLogger(TextFrame.class.getName()).log(Level.SEVERE, null, ex);

                                }

                           new TextFrame();//新建文本文件

                       }

                   }

               }

           }

       });

       //打开

       dk.addActionListener(new ActionListener(){

       public void actionPerformed(ActionEvent e){

           JFileChooser jfc = new JFileChooser();//创建一个文件选择类实例

           int dkf = jfc.showOpenDialog(frame);

           if(dkf == JFileChooser.APPROVE_OPTION){//单击了打开文件窗体中的确定

               try {

                   FileInputStream input = new FileInputStream(new File(

                           jfc.getSelectedFile().getAbsolutePath()));

                   int sum = input.available();//得到输入流文件的长度

                   byte texts[] = new byte[sum];//用来保存读取到的数据

                   input.read(texts);//将文件读取到texts字节数组中

    //               BASE64Encoder enc=new BASE64Encoder();

    //               String str = enc.encode(texts);//字节数组转换成String

                   String str = new String(texts,"GB2312");//将字节数组转换成中文字符

                   jta.setText(str);

    //                        FileInputStream is = new FileInputStream(

    //                                jfc.getSelectedFile().getAbsolutePath());

    //                        InputStreamReader streamReader =

    //                                new InputStreamReader(is, "GB2312");

    //                        int in = 0;

    //                        char ch [] = new char[1024];

    //                        String text = "";

    //                        char s = ' ';

    //                        while((in = streamReader.read(ch))!=-1){

    //                            for(int i=0;i<ch.length;i++){

    //                            s = ch[i];

    //                            text += s;

    //                        }

    //                        jta.setText(text);

    //                        }

                            //streamReader.close();

                        } catch (Exception ex) {

                            Logger.getLogger(TextFrame.class.getName()).log(Level.SEVERE, null, ex);

                        }

           }

           else

               lj = "";

       }

       });

       //保存

       bc.addActionListener(new ActionListener(){

           public void actionPerformed(ActionEvent e){

               if(lj.equals("")){//没有路径时,就另存为

                   JFileChooser jfc = new JFileChooser();//用于选择保存路径的文件名

                       int bcf = jfc.showSaveDialog(frame);//弹出保存窗口

                       if(bcf == JFileChooser.APPROVE_OPTION){

                                try {

                                    //保存文件

                                    BufferedWriter bfw = new BufferedWriter(

                                            new FileWriter(new File(jfc.getSelectedFile().getAbsolutePath()+".txt")));

                                    lj = jfc.getSelectedFile().getAbsolutePath();

                                    bfw.write(jta.getText());//向文件写出数据

                                    bfw.flush();

                                    bfw.close();//关闭输出流

                                } catch (IOException ex) {

                                    Logger.getLogger(TextFrame.class.getName()).log(Level.SEVERE, null, ex);

                                }

                       }

               }

               else{//路径不为空时,保存在原来的路径下

                   try {

                       //保存文件

                       BufferedWriter bfw = new BufferedWriter(

                               new FileWriter(

                               new File(lj)));

                       bfw.write(jta.getText());//向文件写出数据

                       bfw.flush();

                       bfw.close();//关闭输出流

                   } catch (IOException ex) {

                       Logger.getLogger(TextFrame.class.getName()).log(Level.SEVERE, null, ex);

                   }

               }

           }

       });

       //另存为

       lcw.addActionListener(new ActionListener(){

           public void actionPerformed(ActionEvent e){

               JFileChooser jfc = new JFileChooser();//用于选择保存路径的文件名

                       int bcf = jfc.showSaveDialog(frame);//弹出保存窗口

                       if(bcf == JFileChooser.APPROVE_OPTION){

                                try {

                                    //保存文件

                                    BufferedWriter bfw = new BufferedWriter(

                                            new FileWriter(new File(jfc.getSelectedFile().getAbsolutePath()+".txt")));

                                    lj = jfc.getSelectedFile().getAbsolutePath();

                                    bfw.write(jta.getText());//向文件写出数据

                                    bfw.flush();

                                    bfw.close();//关闭输出流

                                } catch (IOException ex) {

                                    Logger.getLogger(TextFrame.class.getName()).log(Level.SEVERE, null, ex);

                                }

                       }

           }

       });

       //退出

       tc.addActionListener(new ActionListener(){

           public void actionPerformed(ActionEvent e){

               dispose();

               System.exit(0);

           }

       });

       //打印

       dy.addActionListener(new ActionListener(){

           public void actionPerformed(ActionEvent e){

           }

       });

       //页面设置

       ymsz.addActionListener(new ActionListener(){

           public void actionPerformed(ActionEvent e){

           }

       });

       //自动换行

       zdhh.addActionListener(new ActionListener(){

           public void actionPerformed(ActionEvent e){

              jta.setLineWrap(true);

           }

       });

       //字体(点字体设置窗体的叉能关闭字体设置窗体却不能再编辑)

       zti.addActionListener(new ActionListener(){

           public void actionPerformed(ActionEvent e){

               final JFrame ztsz = new JFrame("字体设置");

               //字体

               ztsz.setLocation(150, 200);

               frame.setEnabled(false);//文本编辑窗体不可用!

               final JComboBox zt = new JComboBox(

                       GraphicsEnvironment.getLocalGraphicsEnvironment()

                       .getAvailableFontFamilyNames());

               zt.setLocation(30, 80);

               Container c = ztsz.getContentPane();

               JPanel jp = new JPanel();

               jp.add(zt,new FlowLayout());

               //字形

               String[]   faceString={"正常","粗体","斜体","粗斜体"};

               String[]   sizeString={"初号","小初","一号","小一","二号","小二",

                      "三号","小三","四号","小四","五号","小五","六号","小六","七号",

                      "八号","5","8","9","10","11","12","14","16","18","20","22","24",

                      "26","28","36","48","72"};

               final JComboBox zx = new JComboBox(faceString);

               final JComboBox dx = new JComboBox(sizeString);

               final JButton sure = new JButton("确定");

               final JButton cancel = new JButton("取消");

               jp.add(zx);

               jp.add(dx);

               jp.add(sure);

               jp.add(cancel);

               c.add(jp);

               //确定

               sure.addActionListener(new ActionListener(){

                   public void actionPerformed(ActionEvent e){

                       if(e.getSource()==sure){//将文本设置成所选的字体

                           if(!jta.getText().equals("")){

                           jta.setFont(new Font(

                                   zt.getActionCommand(),zx.getSelectedIndex(),

                                   dx.getSelectedIndex()));

                           frame.setEnabled(true);//文本编辑窗体可用

                           }

                           else{

                               JOptionPane.showMessageDialog(null,

                                       "你的文本中还没有字符,请输入字符后重新设置");

                               frame.setEnabled(true);

                           }

                       }

                   }

               });

               cancel.addActionListener(new ActionListener(){//取消

                   public void actionPerformed(ActionEvent e){

                       if(e.getSource()==cancel){//关闭字体设置窗体

                           ztsz.setVisible(false);

                           frame.setEnabled(true);//文本编辑窗体可用

                       }

                   }

               });

               ztsz.setSize(360, 100);//设置窗体长度100和宽度360

               ztsz.setVisible(true);//窗体可见

               ztsz.setResizable(false);//禁止放大窗体

           }

       });

       frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

       }

       public static void main(String args []){

       new TextFrame();

       }

    }

    相关文章

      网友评论

        本文标题:(JAVA)记事本

        本文链接:https://www.haomeiwen.com/subject/yktnyttx.html