zoukankan      html  css  js  c++  java
  • Java高级应用(四)J2SE记事本

    全代码直接来吧

    import java.awt.BorderLayout;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.io.BufferedInputStream;
    import java.io.BufferedOutputStream;
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import javax.swing.JFileChooser;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    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 javax.swing.UIManager;
    
    
    public class NotePad extends JFrame implements ActionListener{
    
            private JMenuItem jopen,jsave,jexit,jabout;
            private JTextArea jta=new JTextArea();
            private JLabel jl=new JLabel();
            private JFileChooser jfc=new JFileChooser();
            public NotePad(){
                setTitle("NotePad");
                JMenuBar mb=new JMenuBar();
                setJMenuBar(mb);
                JMenu fm=new JMenu("File");
                JMenu hm=new JMenu("Help");
                mb.add(fm);
                mb.add(hm);
                fm.add(jopen=new JMenuItem("Open"));
                fm.add(jsave=new JMenuItem("Save"));
                fm.addSeparator();    
                fm.add(jexit=new JMenuItem("Exit"));
                hm.add(jabout=new JMenuItem("About"));
                jfc.setCurrentDirectory(new File("."));
                getContentPane().add(new JScrollPane(jta),BorderLayout.CENTER);
                getContentPane().add(jl,BorderLayout.SOUTH);
                jopen.addActionListener(this);
                jsave.addActionListener(this);
                jexit.addActionListener(this);
                jabout.addActionListener(this);
                setSize(800,600);
                setLocation(300, 80);
                setVisible(true);
                setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                                
            }
            
            
        public static void main(String[] args) {
            try{
                UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
            }
            catch(Exception e){}
                JFrame frame=new NotePad();        
            
    
        }
    
        @Override
        public void actionPerformed(ActionEvent e) {
            String string=e.getActionCommand();
            if(e.getSource() instanceof JMenuItem){
                if(string.equals("Open"))
                    open();        
            
                if(string.equals("Save"))
                    save();        
                else if(string.equals("Exit")){
                    System.exit(0);
                }
                    
    
                if(string.equals("About"))
                    JOptionPane.showMessageDialog(this, "Designed By BaiQiang","About the NotePad",JOptionPane.INFORMATION_MESSAGE);
                
                    }
    
    }
    private void open(){
        if(jfc.showOpenDialog(this)==JFileChooser.APPROVE_OPTION){
            
            File file=jfc.getSelectedFile();
            try{
                BufferedInputStream inputStream=new BufferedInputStream(new FileInputStream(file));
                byte[] b=new byte[inputStream.available()];
                inputStream.read(b,0,b.length);
                jta.append(new String(b,0,b.length));
                inputStream.close();
                jl.setText(file.getName()+"Opened");
                
            }
            catch(IOException e){
                jl.setText("Error OPening!");
                
            }
            
        }
        
        
    }
    private void save(){
     if(jfc.showOpenDialog(this)==JFileChooser.APPROVE_OPTION){
            
            File file=jfc.getSelectedFile();
            try{
                BufferedOutputStream out=new BufferedOutputStream(new FileOutputStream(file));
                byte [] b=(jta.getText()).getBytes();
                out.write(b,0,b.length);
                out.close();
                jl.setText(file.getName()+"Saved");
            }
            catch(IOException e){
                jl.setText("Error OPening!");
                
            }
                
         }
    }    
    }
  • 相关阅读:
    NLTK学习笔记(三):NLTK的一些工具
    NLTK学习笔记(四):自然语言处理的一些算法研究
    NLTK学习笔记(五):分类和标注词汇
    深入C++的运算符重载
    python3实现TCP协议的简单服务器和客户端
    python3实现UDP协议的简单服务器和客户端
    Windows平台下python2和3的兼容问题解决
    异常处理
    内置方法
    反射:hasattr、getattr、setattr、delattr
  • 原文地址:https://www.cnblogs.com/bq12345/p/3011217.html
Copyright © 2011-2022 走看看