简易记事本java源码

来源:互联网 发布:淘宝产品视频拍摄教程 编辑:程序博客网 时间:2024/05/22 06:21

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.io.*;
import java.net.*;
import java.util.*;

//class
class TextEditorFrame extends JFrame
{
    File file = null;
    Color color = Color.black;
    GraphicsEnvironment getFont = GraphicsEnvironment.getLocalGraphicsEnvironment();
    Font []fonts = getFont.getAllFonts();
    //1.2
    JTextPane text = new JTextPane();
    JFileChooser filechooser = new JFileChooser();
    JColorChooser colorchooser = new JColorChooser();
    JDialog about = new JDialog(this);
    JMenuBar menubar = new JMenuBar();


    //主窗体初始化
    TextEditorFrame()
    {
        initTextPane();//面板
        initMenu();//菜单
        initAboutDialog();//关于对话框
        initToolBar();//工具栏

    }
    //面板初始化
    void initTextPane()
    {
      setFont(new Font("Times New Roman",Font.PLAIN,12));
      getContentPane().add( new JScrollPane(text));
    }

    //---------------------------------------------------------------------------
    //菜单的定义
    //super Menue
    JMenu [] menus= new JMenu[]
    {
        new JMenu("文件"),
        new JMenu("编辑"),
  new JMenu("工具"),
        new JMenu("帮助"),
    };

    //sub Menue
    JMenuItem menuitems [][] =new JMenuItem[][]
    {
        {
            new JMenuItem("新建"),
            new JMenuItem("打开..."),
            new JMenuItem("保存..."),
            new JMenuItem("Exit")
        },
        {
            new JMenuItem("Copy"),
            new JMenuItem("Cut"),
            new JMenuItem("Paste"),
            new JMenuItem("选择全部"),
            new JMenuItem("Color...")
        },
  {
    new JMenuItem("MS 记事本"),
    new JMenuItem("MS 计算器")

     },
        {
            new JMenuItem("About")
        }
    };
   //-----------------------------------------------------------------------------
   //菜单的初始化
    void initMenu()
    {
        for(int i=0;i<menus.length;i++)
        {
            menubar.add(menus[i]);
            for(int j=0;j<menuitems[i].length;j++)
            {
                menus[i].add(menuitems[i][j]);

                menuitems[i][j].addActionListener(action);
            }
        }

        this.setJMenuBar(menubar);
    }
   //-----------------------------------------------------------------------------

    //事件处理
    ActionListener action = new ActionListener()
    {
        public void actionPerformed(ActionEvent e)
        {
            JMenuItem mi = (JMenuItem)e.getSource();

            String id = mi.getText();
            if(id.equals("新建")){
             Date date=new Date();
                text.setText(date.toString());
               
                file = null;
            }else if(id.equals("打开...")){
                if(file != null)
                  filechooser.setSelectedFile(file);

                int returnVal = filechooser.showOpenDialog(TextEditorFrame.this);

                if(returnVal == JFileChooser.APPROVE_OPTION){
                    file = filechooser.getSelectedFile();
                    openFile();
                }
            }
            else if(id.equals("保存...")){
                if(file != null)
                  filechooser.setSelectedFile(file);

                int returnVal = filechooser.showSaveDialog(TextEditorFrame.this);

                if(returnVal == JFileChooser.APPROVE_OPTION){
                    file = filechooser.getSelectedFile();
                    saveFile();
                }}else if(id.equals("Exit")){
                    System.exit(0);
                }else if(id.equals("Cut")){
                    text.cut();
                }else if(id.equals("Copy")){
                    text.copy();
                }else if(id.equals("Paste")){
                    text.paste();
                }else if(id.equals("选择全部")){
                    text.selectAll();
                }else if(id.equals("Color...")){
                    color = JColorChooser.showDialog(TextEditorFrame.this,"",color);
                    text.setForeground(color);
    }else if(id.equals("MS 记事本")){
     try{
           String command = "notepad.exe";
           Process child = Runtime.getRuntime().exec(command);
          }
          catch (IOException ex)
          {
                         }
                }else if(id.equals("MS 计算器")){
                    try{
           String command = "calc.exe";
           Process child = Runtime.getRuntime().exec(command);
          }
          catch (IOException ex)
          {
                          }
    }else if(id.equals("About")){
                    about.setSize(250,150);
                    about.show();
                }

            }
        };

//-------------------------------------------------------------------------------
//保存文件
    void saveFile()
    {
        try{
            FileWriter fw = new FileWriter(file);
            fw.write(text.getText());
            fw.close();
        }
        catch(Exception e)
        {
            e.printStackTrace();
        }
    }
 //---------------------------------------------------------------
 //打开文件
    void openFile()
    {
        try{
            FileReader fr = new FileReader(file);
            int len = (int) file.length();
            char [] buffer = new char[len];
            fr.read(buffer,0,len);
            fr.close();
            text.setText(new String(buffer));
        }
        catch(Exception e)
        {
            e.printStackTrace();
        }
    }
//--------------------------------------------------------------
//关于对话框
    void initAboutDialog()
    {

        about.setTitle("关于... ");
        about.getContentPane().setBackground(Color.green );
        about.getContentPane().add(new JLabel("   简易文字编辑器!作者:孙志峰"));
        about.setModal(true);

    }
//---------------------------------------------------------------
    //@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
    JToolBar toolbar = new JToolBar();
    JButton [] buttons = new JButton[]
    {
        new JButton("",new ImageIcon("copy.jpg")),

        new JButton("",new ImageIcon("cut.jpg")),

        new JButton("",new ImageIcon("paste.jpg")),

  new JButton("",new ImageIcon("MS1.jpg")),

  new JButton("",new ImageIcon("MS2.jpg")),
    };
    //@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
//-------------------------------------------------------------------------------------------------------------------
//工具栏
    void initToolBar()
    {
        for(int i=0; i<buttons.length;i++)
                    toolbar.add(buttons[i]);
        buttons[0].setToolTipText("copy");
        buttons[0].addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e){
                text.copy();
            }
            });

        buttons[1].setToolTipText("cut");
        buttons[1].addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e){
                text.cut();
            }
           });

        buttons[2].setToolTipText("paste");
        buttons[2].addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e){
                text.paste();
            }
           });

     buttons[3].setToolTipText("MS 记事本");
     buttons[3].addActionListener(new ActionListener(){
         public void actionPerformed(ActionEvent e){
                    try{
                   String command = "notepad.exe";
                   Process child = Runtime.getRuntime().exec(command);
                  }
                  catch (IOException ex)
                  {
                         }
                }
           });

  buttons[4].setToolTipText("MS 计算器");
  buttons[4].addActionListener(new ActionListener(){
      public void actionPerformed(ActionEvent e){
                     try{
                    String command = "calc.exe";
                    Process child = Runtime.getRuntime().exec(command);
                   }
                   catch (IOException ex)
                   {
                          }
                 }
           });

        this.getContentPane().add(toolbar,BorderLayout.NORTH);

       }

  }


//-----------------------------------------------------------------------------------------------------


//入口函数
 public class TextEditorApp
{

  public static void main( String [] args){

    TextEditorFrame f = new TextEditorFrame();
 JSplashWindowEx splash = new JSplashWindowEx();
    splash.start();
 try {
       Thread.sleep(10000);
     }
     catch (Exception ex) {
       ex.printStackTrace();
    }
    f.setTitle("简易文字编辑器 ");
    f.setSize(800,600);
    f.show();

  }
}
//启动屏
class JSplashWindowEx extends JWindow implements Runnable {
  Thread splashThread=null;
  private JProgressBar progress;
  public JSplashWindowEx() {
    setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
    JPanel splash = new JPanel(new BorderLayout());
    URL url = getClass().getResource("/images/winter.jpg");

    if(url != null){
      splash.add(new JButton(new ImageIcon(url)),
      BorderLayout.CENTER);
    }
    progress = new JProgressBar(1,100);
    progress.setStringPainted(true);
    progress.setBorderPainted(false);
    progress.setString("请稍后,程序正在加载...");
    progress.setBackground(Color.white);
    splash.add(progress,BorderLayout.SOUTH);
    setContentPane(splash);

    Dimension screen = getToolkit().getScreenSize();
    pack();
    setLocation((screen.width - getSize().width) / 2,
 (screen.height - getSize().height) / 2);
  }

  public void start(){
    this.toFront();
    splashThread=new Thread(this);
    splashThread.start();
  }

  public void run(){
    show();
    try {
      for (int i=0;i<100;i++){
        Thread.sleep(100);
        progress.setValue(progress.getValue() + 1);
      }
    }
    catch (Exception ex) {
      ex.printStackTrace();
    }
    dispose();
  }
}