zoukankan      html  css  js  c++  java
  • java记事本1.2版

    功能:实现了新建,打开,保存,退出,复制,剪切,粘贴等功能

    效果图:

    代码:

    /**
     * 
     */
    package com.niit.javagui;
    
    import java.awt.Color;
    import java.awt.MenuBar;
    import java.awt.event.ActionEvent;
    import java.awt.event.InputEvent;
    import java.awt.event.ItemEvent;
    import java.awt.event.ItemListener;
    import java.awt.event.KeyEvent;
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.FileNotFoundException;
    import java.io.FileOutputStream;
    import java.io.InputStream;
    import java.io.OutputStream;
    
    import javax.swing.AbstractAction;
    import javax.swing.JFileChooser;
    import javax.swing.JFrame;
    import javax.swing.JMenu;
    import javax.swing.JMenuBar;
    import javax.swing.JMenuItem;
    import javax.swing.JPanel;
    import javax.swing.JTabbedPane;
    import javax.swing.JTextPane;
    import javax.swing.KeyStroke;
    import javax.swing.text.DefaultStyledDocument;
    
    /**
     * @author: Annie
     * @date:2016年5月30日
     * @description:简单的记事本
     */
    public class Notepad extends JFrame {
        public static void main(String[] args) {
            new Notepad();
        }
    
        JTabbedPane tabbedPane;
        JPanel jp1,jp2,jp3;
        JMenuBar menuBar;
        JMenu filemenu,editMenu,formatMenu,helpmenu,checkMenu;
        JMenuItem itemNew,itemOpen,itemSave,itemSaveAs,itemSet,itemPrint,itemExit,
        itemAutomatic,itemFont,itemaStatusBar,itemaCheckHelp,itemAbout,itemCopy,itemCut,itemPast;
        JTextPane textpane;
        JFileChooser fileChooser; 
        public Notepad(){
            setVisible(true);
            setSize(400,400);
            setTitle("记事本");
            setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            instantiationObject();    
            keyWord();
            addFunction();
    
    
        }
    
        /**
         * 实例化对象方法
         * */
        public void instantiationObject(){
            jp1 = new JPanel();
            jp2 = new JPanel();
            jp3 = new JPanel();
            tabbedPane = new JTabbedPane(JTabbedPane.BOTTOM);
            menuBar = new JMenuBar();
            filemenu = new JMenu("文件(F)");
            editMenu = new JMenu("编辑(E)");
            formatMenu = new JMenu("格式(o)");
            checkMenu = new JMenu("查看(V)");
            helpmenu = new JMenu("帮助(H)");
    
            itemNew = new JMenuItem(new newAction());
            itemOpen = new JMenuItem(new OpenAction());
            itemSave = new JMenuItem(new SaveAction());
            itemSaveAs = new JMenuItem("另存为");
            itemSet = new JMenuItem("页面设置");
            itemPrint = new JMenuItem("打印");
            itemExit = new  JMenuItem(new ExistAction());
    
            itemCopy = new JMenuItem(new CopyAction());
            itemPast = new JMenuItem(new PasteAction());
            itemCut = new JMenuItem(new CutAction());
    
            itemAutomatic = new JMenuItem("自动转行");
            itemFont = new JMenuItem("字体");
    
            itemaStatusBar = new JMenuItem("状态栏");
    
            itemaCheckHelp = new JMenuItem("查看帮助");
            itemAbout = new JMenuItem("关于记事本");
    
            textpane = new JTextPane(); //文本窗格,编辑窗口
    
            fileChooser = new JFileChooser();//文件选择器
    
    
        }
    
        /**
         * 添加组件方法
         * */
        public void addFunction(){
            setJMenuBar(menuBar);
    
            menuBar.add(filemenu);
            menuBar.add(editMenu);
            menuBar.add(formatMenu);
            menuBar.add(checkMenu);
            menuBar.add(helpmenu);
    
            filemenu.add(itemNew);
            filemenu.add(itemOpen);
            filemenu.add(itemSave);
            filemenu.add(itemSaveAs);
            filemenu.addSeparator();
            filemenu.add(itemSet);
            filemenu.add(itemPrint);
            filemenu.add(itemExit);
    
            editMenu.add(itemCopy);
            editMenu.add(itemPast);
            editMenu.add(itemCut);
    
            formatMenu.add(itemAutomatic);
            formatMenu.add(itemFont);
    
            checkMenu.add(itemaStatusBar);
    
            helpmenu.add(itemaCheckHelp);
            helpmenu.add(itemAbout);
    
            add(textpane);
    
        }
    
        /**
         * 实例化对象方法
         * */
        public void keyWord(){
            itemNew.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_N,InputEvent.CTRL_MASK));
            itemOpen.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_O,InputEvent.CTRL_MASK));
            itemSave.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_S,InputEvent.CTRL_MASK));
            itemSaveAs.setAccelerator(KeyStroke.getKeyStroke('A'));
            itemSet.setAccelerator(KeyStroke.getKeyStroke('U'));
            itemPrint.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_P,InputEvent.CTRL_MASK));
            itemExit.setAccelerator(KeyStroke.getKeyStroke('X'));
    
            itemAutomatic.setAccelerator(KeyStroke.getKeyStroke('W'));
            itemFont.setAccelerator(KeyStroke.getKeyStroke('F'));
    
            itemaStatusBar.setAccelerator(KeyStroke.getKeyStroke('S'));
    
            itemaCheckHelp.setAccelerator(KeyStroke.getKeyStroke('H'));
            itemAbout.setAccelerator(KeyStroke.getKeyStroke('A'));
        }
    
    
    
    
        class  newAction extends AbstractAction{
            public newAction(){
                super("新建");
            }        
            @Override
            public void actionPerformed(ActionEvent e) {
                textpane.setDocument(new DefaultStyledDocument());
    
            }
    
        }
    
        class OpenAction extends AbstractAction{
            public OpenAction() {
                super("打开");
            }
            @Override
            public void actionPerformed(ActionEvent e) {
                int i = fileChooser.showOpenDialog(Notepad.this); // 显示打开文件选择对话框
                if(i == JFileChooser.APPROVE_OPTION){//点击对话框中打开选项
                    File f = fileChooser.getSelectedFile();//得到选择的文件
                    try {
                        InputStream is = new FileInputStream(f);
                        textpane.read(is, "HTMLDocument"); //读入文件到文本窗格
    
                    } catch (Exception e1) {
                        e1.printStackTrace();
                    }
                }
    
            }
    
        }
    
        class SaveAction extends AbstractAction{
            public SaveAction() {
                super("保存");
            }
            @Override
            public void actionPerformed(ActionEvent e) {
                int i =fileChooser.showSaveDialog(Notepad.this);
                if(i == JFileChooser.APPROVE_OPTION ){
                    /* 1.得到选择的文件   2.得到输出流  3.写入 */
                    File f =fileChooser.getSelectedFile();
                    try {
                        OutputStream out = new FileOutputStream(f);
                        out.write(textpane.getText().getBytes());
                    } catch (Exception e1) {
                        e1.printStackTrace();
                    }
                }
            }
    
        }
    
        class ExistAction extends AbstractAction{
            public     ExistAction(){
                super("退出");
            }
            @Override
            public void actionPerformed(ActionEvent e) {
                System.exit(0);
    
            }
        }
    
    
        class CutAction extends AbstractAction { //剪切命令
            public CutAction() {
                super("剪切");
            }
            public void actionPerformed(ActionEvent e) {
                textpane.cut(); //调用文本窗格的剪切命令
            } 
        }
    
        class CopyAction extends AbstractAction { //拷贝命令
            public CopyAction() {
                super("复制");
            }
            public void actionPerformed(ActionEvent e) {
                textpane.copy(); //调用文本窗格的拷贝命令
            }
        }
    
        class PasteAction extends AbstractAction { //粘贴命令
            public PasteAction() {
                super("粘贴");
            }
            public void actionPerformed(ActionEvent e) {
                textpane.paste(); //调用文本窗格的粘贴命令
            }
        }
    
    }
  • 相关阅读:
    记一次干扰后处理特效的实现
    Cosine-Weighted采样算法
    PBR技术简介(三):菲涅尔公式
    PBR技术简介(二):Cook-Torrance BRDF
    PBR技术简介(一)
    Python函数的默认参数坑点解析
    屏幕空间环境光屏蔽(SSAO)探秘
    辐射度量学简介(二)
    辐射度量学简介(一)
    解决16bit压缩贴图失真问题
  • 原文地址:https://www.cnblogs.com/wangmingxia/p/5550045.html
Copyright © 2011-2022 走看看