• 第十四周课程总结


    文本编译器

    package caidan;
    import java.awt.event.*;
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.FileNotFoundException;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.io.PrintStream;
    
    import javax.swing.*;
    
    
    public   class JMenuDemo1 implements ActionListener {
    
        JFrame frame;
        JMenuBar bar;
        JMenu fileMenu,editMenu;
        JMenuItem newItem,openItem,saveItem,closeItem;
        ImageIcon newIcon,openIcon,saveIcon,closeIcon;
        JScrollPane scorll;
        JTextArea area;
        File file;
        private JFileChooser chooser;
        
        public JMenuDemo1(){
            frame = new JFrame("LXZ的小本本");
            bar = new JMenuBar();
            fileMenu = new JMenu("文件");
            editMenu = new JMenu("编辑");
            newItem = new JMenuItem("新建(N)",newIcon);
            openItem = new JMenuItem("打开(O)",openIcon);
            saveItem = new JMenuItem("另存为(A)",saveIcon);
            closeItem = new JMenuItem("关闭(X)",closeIcon);
            
            area = new JTextArea();
            scorll = new JScrollPane(area,JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
            newIcon = new ImageIcon("D:"+File.separator+"images"+File.separator+"3.png");
            openIcon = new ImageIcon("D:"+File.separator+"images"+File.separator+"1.png");
            saveIcon = new ImageIcon("D:"+File.separator+"images"+File.separator+"2.png");
            closeIcon = new ImageIcon("D:"+File.separator+"images"+File.separator+"4.png");
            area = new JTextArea();
            scorll = new JScrollPane(area);
            
            newItem.setMnemonic(KeyEvent.VK_N);
            openItem.setMnemonic(KeyEvent.VK_O);
            saveItem.setMnemonic(KeyEvent.VK_A);
            closeItem.setMnemonic(KeyEvent.VK_X);
            
             newItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_N,InputEvent.CTRL_DOWN_MASK));
             openItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_O,InputEvent.CTRL_DOWN_MASK));
             saveItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_A,InputEvent.CTRL_DOWN_MASK));
             saveItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_B,InputEvent.CTRL_DOWN_MASK));
             closeItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_X,InputEvent.CTRL_DOWN_MASK));
    
            fileMenu.add(newItem);
            fileMenu.add(openItem);
            fileMenu.add(saveItem);
            fileMenu.addSeparator();
            fileMenu.add(closeItem);
            
            newItem.addActionListener(this);
            openItem.addActionListener(this);
            saveItem.addActionListener(this);
            closeItem.addActionListener(this);
            
            bar.add(fileMenu);
            bar.add(editMenu);
            
            frame.setJMenuBar(bar);
            frame.add(scorll);
            frame.setSize(400, 300);
            frame.setVisible(true);
        }
        
        @Override
        public void actionPerformed(ActionEvent e) {
            Object obj = e.getSource();
            if (obj instanceof JMenuItem) {
                JMenuItem item = (JMenuItem) obj;
                if (item == newItem) {
                    new JMenuDemo1();           
                }else if (item == openItem) {
                    chooser = new JFileChooser();
                    chooser.showOpenDialog(null);
                    file = chooser.getSelectedFile();
                try {
                    FileInputStream fis= new FileInputStream(file);
                        byte[] b = new byte[fis.available()];
                        fis.read(b);
                        area.append(new String(b));
                        fis.close();
                    
                } catch (FileNotFoundException e1) {
                    
                    e1.printStackTrace();
                } catch (IOException e1) {
                    
                    e1.printStackTrace();
                }
            }else if(item == saveItem) {
                if(file!=null) {
                    try {
                        PrintStream out = new PrintStream(new FileOutputStream(file));
                        out.print(this.area.getText());
                        out.close();
                    } catch (FileNotFoundException e1) {
                        
                        e1.printStackTrace();
                    }
                }
                
            }else if(item == saveItem) {
                chooser.showSaveDialog(null);
                file = chooser.getSelectedFile();
                if(!file.exists()) {
                    try {
                        if(!file.exists()) {
                        file.createNewFile();
                        }
                        FileOutputStream fos=new FileOutputStream(file);
                        byte[] b = area.getText().getBytes();
                        fos.write(b);
                        fos.close();
                    } catch (IOException e1) {
                        
                        e1.printStackTrace();
                    
                }
            }
        }else if(item == closeItem) {
            System.exit(1);
        }
    }
    
    
    
    }
    }
    
    
            
    
    Main方法
    package caidan;
    
    
    
    public class JMenuDemo {
    
        public static void main(String[] args) {
            // TODO Auto-generated method stub
            new JMenuDemo1();
    
        }
    
    }
    

    截图:

  • 相关阅读:
    Silverlight“.NET研究” 2.5D RPG游戏技巧与特效处理:(七)动画特写 狼人:
    Silverlight 2.5D RPG游戏“.NET研究”技巧与特效处理:(六)流光追影 狼人:
    WPF中使用amCh“.NET研究”arts绘制股票K线图 狼人:
    验证.N“.NET研究”ET强命称的思路和实例 狼人:
    ASP.NET“.NET研究”下用URLRewriter重写二级域名 狼人:
    spring初始化在ServletContextListener实现类中获取spring注入对象
    执行原因【菜鸟笔记】Ubuntu系统shellscript中 关于for循环以及declare出错的原因
    jqueryfunctionchrome浏览器不支持 onmouseleave事件的解决
    异步线程C#异步委托的使用
    android删除android拦截短信并删除该条短信
  • 原文地址:https://www.cnblogs.com/wmf0616/p/11960569.html
Copyright © 2020-2023  润新知