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!");
            
        }
            
     }
}    
}

 

posted @ 2013-04-09 22:09  强子~Developer  阅读(232)  评论(0编辑  收藏  举报