Swing--编辑器1

Swing--编辑器1

import  java.awt.BorderLayout;
import  java.awt.Container;
import  java.awt.Dimension;
import  java.awt.Font;
import  java.awt.event.ActionEvent;
import  java.io.File;
import  java.io.FileReader;
import  java.io.FileWriter;
import  java.io.IOException;
import  java.util.Hashtable;
import  java.util.Map;

import  javax.swing.AbstractAction;
import  javax.swing.Action;
import  javax.swing.ImageIcon;
import  javax.swing.JButton;
import  javax.swing.JFileChooser;
import  javax.swing.JFrame;
import  javax.swing.JLabel;
import  javax.swing.JMenu;
import  javax.swing.JMenuBar;
import  javax.swing.JOptionPane;
import  javax.swing.JPanel;
import  javax.swing.JScrollPane;
import  javax.swing.JTextArea;
import  javax.swing.JToolBar;
import  javax.swing.text.DefaultEditorKit;
import  javax.swing.text.JTextComponent;


public   class  EditPlus  extends  JFrame {
    
private  Action openAction;
    
private  Action saveAction;
    
private  Action closeAction;
    
    
private  JTextComponent textComp;
    
private  Map actionMap  =   new  Hashtable();
    
    
public  EditPlus() {
        
super ( " EditPlus学习 " );
        textComp 
=  createTextCompoent();
        
this .setSize( 400 300 );
        
this .setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        
        initActionProperty();
        Container c 
=   this .getContentPane();
        c.add(
new  JScrollPane(textComp), BorderLayout.CENTER);
        c.add(createToolBar(), BorderLayout.NORTH);
        
this .setJMenuBar(createMenuBar());        
        
    }
    
    
protected  JTextComponent createTextCompoent() {
        JTextArea a 
=   new  JTextArea();
        a.setLineWrap(
true );
        
return  a;
    }
    
    
//  初始化Action的一些信息,如图标等
     protected   void  initActionProperty() {
        Action a 
=   null ;
        a 
=  textComp.getActionMap().get(DefaultEditorKit.cutAction);
        a.putValue(Action.SMALL_ICON, 
new  ImageIcon( " icons/cut.gif " ));
        a.putValue(Action.NAME, 
" 剪切 " );
        
        a 
=  textComp.getActionMap().get(DefaultEditorKit.copyAction);
        a.putValue(Action.SMALL_ICON, 
new  ImageIcon( " icons/copy.gif " ));
        a.putValue(Action.NAME, 
" 复制 " );

        a 
=  textComp.getActionMap().get(DefaultEditorKit.pasteAction);
        a.putValue(Action.SMALL_ICON, 
new  ImageIcon( " icons/paste.gif " ));
        a.putValue(Action.NAME, 
" 粘贴 " );
        
        a 
=  textComp.getActionMap().get(DefaultEditorKit.selectAllAction);
        a.putValue(Action.NAME, 
" 全部选定 " );
    }
    
    
//  创建ToolBar
     protected  JToolBar createToolBar() {
        JToolBar bar 
=   new  JToolBar();
        bar.add(getOpenAction()).setText(
"" );
        bar.add(getSaveAction()).setText(
"" );
        bar.addSeparator();

        JButton cutBtn 
=  bar.add(textComp.getActionMap().get(DefaultEditorKit.cutAction));
        JButton copyBtn 
=  bar.add(textComp.getActionMap().get(DefaultEditorKit.copyAction));
        JButton pasterBtn 
=  bar.add(textComp.getActionMap().get(DefaultEditorKit.pasteAction));
        cutBtn.setText(
"" );
        copyBtn.setText(
"" );
        pasterBtn.setText(
"" );
        
return  bar;
    }
    
    
//  创建MenuBar
     protected  JMenuBar createMenuBar() {
        JMenuBar bar 
=   new  JMenuBar();
        JMenu file 
=   new  JMenu( " 文件 " );
        JMenu edit 
=   new  JMenu( " 编辑 " );
        bar.add(file);
        bar.add(edit);
        
        file.add(getOpenAction());
        file.add(getSaveAction());
        file.add(getCloseAction());
        
        edit.add(textComp.getActionMap().get(DefaultEditorKit.cutAction));
        edit.add(textComp.getActionMap().get(DefaultEditorKit.copyAction));
        edit.add(textComp.getActionMap().get(DefaultEditorKit.pasteAction));
        edit.add(textComp.getActionMap().get(DefaultEditorKit.selectAllAction));        
        
return  bar;
    }
    
    
protected  Action getOpenAction() {
        
if  (openAction  ==   null )
            openAction 
=   new  OpenAction();
        
return  openAction;
    }
    
    
protected  Action getSaveAction() {
        
if  (saveAction  ==   null )
            saveAction 
=   new  SaveAction();
        
return  saveAction;
    }
    
    
protected  Action getCloseAction() {
        
if  (closeAction  ==   null )
            closeAction 
=   new  CloseAction();
        
return  closeAction;
    }
    
    
protected  JTextComponent getTextComponent() {
        
return  textComp;
    }
    
    
public   class  OpenAction  extends  AbstractAction {
        
public  OpenAction() {
            
super ( " 打开 " new  ImageIcon( " icons/open.gif " ));
        }
        
public   void  actionPerformed(ActionEvent e) {
            JFileChooser fc 
=   new  JFileChooser();
            
if  (fc.showOpenDialog(EditPlus. this !=  JFileChooser.APPROVE_OPTION) 
                
return ;
            File f 
=  fc.getSelectedFile();
            
if  (f  ==   null
                
return ;
            FileReader fr 
=   null ;
            
try  {
                fr 
=   new  FileReader(f);
                textComp.read(fr, 
null );
            } 
catch  (Exception ee) {
                showWarnDialog(
" 读文件异常! " ,ee.toString());
            } 
finally  {
                
if  (fr  !=   null ) {
                    
try  {
                        fr.close();
                    } 
catch  (IOException e1) {
                        e1.printStackTrace();
                    }
                }
            }
        }
    }
    
    
public   class  SaveAction  extends  AbstractAction {
        
public  SaveAction() {
            
super ( " 保存 " new  ImageIcon( " icons/save.gif " ));
        }
        
        
public   void  actionPerformed(ActionEvent e) {
            JFileChooser fc 
=   new  JFileChooser();
            
if  (fc.showSaveDialog(EditPlus. this !=  JFileChooser.APPROVE_OPTION)
                
return ;
            File f 
=  fc.getSelectedFile();
            
if  (f  ==   null )
                
return ;
            
            FileWriter fw 
=   null ;
            
try  {
                fw 
=   new  FileWriter(f);
                textComp.write(fw);
            } 
catch  (Exception ee) {
                showWarnDialog(
" 保存文件异常! " ,ee.toString());
            } 
finally  {
                
try  {
                    
if  (fw  !=   null )
                        fw.close();
                } 
catch  (IOException eee) {
                    eee.printStackTrace();
                }
            }
        }
    }
    
    
public   class  CloseAction  extends  AbstractAction {
        
public  CloseAction() {
            
super ( " 关闭 " );
        }
        @Override
        
public   void  actionPerformed(ActionEvent e) {
            System.exit(
0 );
        }        
    }
    
    
public   void  showWarnDialog(String msg, String warn) {
        JPanel p 
=   new  JPanel();
        JLabel label 
=   new  JLabel(msg);
        label.setFont(label.getFont().deriveFont(Font.BOLD));
        JTextArea area 
=   new  JTextArea(warn);
        area.setOpaque(
false );
        area.setLineWrap(
true );
        area.setPreferredSize(
new  Dimension( 280 100 ));
        p.setLayout(
new  BorderLayout());
        p.add(
new  JLabel( "   " ), BorderLayout.CENTER);
        p.add(label, BorderLayout.NORTH);
        p.add(
new  JScrollPane(area), BorderLayout.SOUTH);
        JOptionPane.showMessageDialog(EditPlus.
this , p,  " 异常信息 "
                JOptionPane.WARNING_MESSAGE);

    }
    
    
public   static   void  main(String args[]) {
        Utils.setLookAndFeel();
        EditPlus ep 
=   new  EditPlus();
        ep.setVisible(
true );
    }
}


</script>

你可能感兴趣的:(Swing--编辑器1)