Chinaunix首页 | 论坛 | 博客
  • 博客访问: 30023
  • 博文数量: 5
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 70
  • 用 户 组: 普通用户
  • 注册时间: 2014-09-26 13:40
个人简介

学会谦卑和虔诚。

文章分类
文章存档

2016年(2)

2015年(2)

2014年(1)

分类: Java

2015-04-08 21:23:33

   记事本算是一个小项目,对于新人来说,是适合不过的,既用不到网络编程,也永不到数据库的编程,也暂时不管国际化等!
   项目分析:主类继承了JFrame类,是程序的入口点,这个类放入了菜单栏,状态栏和文本的编辑区域,构造方法里面初始化了菜单的各各按钮。并且定义了事件的抽象数组,这些抽象的数组分别各自定义,这些类都继承了AbstratAction,实现具体的事件处理!
   需要注意的地方:由于我们需要在各个类里面处理事件,用到MyPad里面的属性,或者说是用到里面的变量!因此,我们的每个事件处理类都把MyPad这个类做为其属性,在构造方法时候作为参数传过去!

点击(

点击(此处)折叠或打开

  1. package cn.com.action;

  2. import java.awt.event.*;
  3. import javax.swing.*;
  4. import javax.swing.text.DefaultStyledDocument;

  5. import cn.com.shizongger.MyPad;

  6. public class NewAction extends AbstractAction {
  7.     private MyPad myPad = null;
  8.     
  9.     public NewAction(MyPad myPad) {
  10.         super("新建");
  11.         this.myPad = myPad;
  12.     }
  13.     
  14.     @Override
  15.     public void actionPerformed(ActionEvent arg0) {
  16.         //清空文本内容
  17.         myPad.textPane.setDocument(new DefaultStyledDocument());
  18.     }
  19. }


点击(此处)折叠或打开

  1. package cn.com.action;

  2. import java.awt.event.*;
  3. import java.io.File;
  4. import java.io.FileInputStream;
  5. import java.io.FileNotFoundException;
  6. import java.io.IOException;
  7. import java.io.InputStream;

  8. import javax.swing.*;
  9. import cn.com.shizongger.MyPad;

  10. public class OpenAction extends AbstractAction {

  11.     private MyPad myPad = null;
  12.     
  13.     public OpenAction(MyPad myPad) {
  14.         super("打开");
  15.         this.myPad = myPad;
  16.     }
  17.     
  18.     @Override
  19.     public void actionPerformed(ActionEvent e) {
  20.         int i = myPad.fileChooser.showSaveDialog(myPad); //显示打开文件对话框
  21.         if(i == JFileChooser.APPROVE_OPTION) {        //点击对话框中打开的选项
  22.             File f = myPad.fileChooser.getSelectedFile();    //得到选择的文件
  23.             
  24.             try {
  25.                 InputStream is = new FileInputStream(f);
  26.                 try {
  27.                     myPad.textPane.read(is, "d");
  28.                 } catch (IOException e1) {
  29.                     e1.printStackTrace();
  30.                 }
  31.             } catch (FileNotFoundException e1) {
  32.                 e1.printStackTrace();
  33.             }
  34.         }
  35.     }
  36. }


点击(此处)折叠或打开

  1. package cn.com.action;

  2. import java.awt.event.*;
  3. import java.io.File;
  4. import java.io.FileNotFoundException;
  5. import java.io.FileOutputStream;
  6. import java.io.IOException;

  7. import javax.swing.*;

  8. import cn.com.shizongger.MyPad;

  9. public class SavaAction extends AbstractAction {

  10.     MyPad myPad = null;
  11.     
  12.     public SavaAction(MyPad myPad) {
  13.         super("保存");
  14.         this.myPad = myPad;
  15.     }
  16.     
  17.     @Override
  18.     public void actionPerformed(ActionEvent e) {
  19.         
  20.         int i = myPad.fileChooser.showSaveDialog(myPad);    //显示保存文件对话框
  21.         if(i == JFileChooser.APPROVE_OPTION) {
  22.             File f = myPad.fileChooser.getSelectedFile();
  23.             
  24.             try {
  25.                 FileOutputStream os = new FileOutputStream(f);
  26.                 try {
  27.                     os.write(myPad.textPane.getText().getBytes());
  28.                 } catch (IOException e1) {
  29.                     e1.printStackTrace();
  30.                 }
  31.             } catch (FileNotFoundException e1) {
  32.                 e1.printStackTrace();
  33.             }
  34.         }
  35.     }
  36. }

处)折叠或打开

  1. package cn.com.shizongger;

  2. import java.awt.BorderLayout;
  3. import java.awt.Color;
  4. import java.awt.Container;
  5. import java.awt.Font;
  6. import java.awt.event.WindowEvent;
  7. import java.awt.event.WindowListener;

  8. import javax.swing.*;
  9. import cn.com.action.*;

  10. public class MyPad extends JFrame {
  11.     
  12.     public JTextPane textPane = new JTextPane();    //文本窗格,编辑窗口
  13.     
  14.     public JLabel statusBar = new JLabel();    //状态栏
  15.     
  16.     public JFileChooser fileChooser = new JFileChooser();    //文件选择器
  17.     
  18.     Action[] actions = null;
  19.     
  20.     //构造方法
  21.     public MyPad() {
  22.         super("记事本1.0版本");
  23.         
  24.         //设置文本的字体和文字大小
  25.         textPane.setBackground(Color.LIGHT_GRAY);
  26.         textPane.setForeground(Color.ORANGE);
  27.         textPane.setFont(new Font("TimesRoman", Font.PLAIN, 18));
  28.         
  29.         Action[] actions = {    //Action数组,各种操作的命令
  30.                 new NewAction(this),
  31.                 new OpenAction(this),
  32.                 new SavaAction(this),
  33.                 new CutAction(this),
  34.                 new CopyAction(this),
  35.                 new PasteAction(this),
  36.                 new AboutAction(this),
  37.                 new ExitAction(this)
  38.         };
  39.         this.actions = actions;        //将构造方法的actions赋值给类的属性actions
  40.         
  41.         this.setJMenuBar(createJMenuBar(actions));        //设置菜单栏
  42.         Container container = this.getContentPane();    //获取容器
  43.         container.add(createJToolBar(actions), BorderLayout.NORTH);        //增加工具栏
  44.         container.add(textPane, BorderLayout.CENTER);
  45.         container.add(statusBar, BorderLayout.SOUTH);
  46.         
  47.         this.addWindowListener(new WindowListener() {

  48.             @Override
  49.             public void windowActivated(WindowEvent arg0) {    
  50.             }

  51.             @Override
  52.             public void windowClosed(WindowEvent arg0) {    
  53.             }

  54.             @Override
  55.             public void windowClosing(WindowEvent arg0) {
  56.                 System.exit(0);
  57.             }

  58.             @Override
  59.             public void windowDeactivated(WindowEvent arg0) {
  60.             }

  61.             @Override
  62.             public void windowDeiconified(WindowEvent arg0) {
  63.             }

  64.             @Override
  65.             public void windowIconified(WindowEvent arg0) {    
  66.             }

  67.             @Override
  68.             public void windowOpened(WindowEvent arg0) {        
  69.             }
  70.             
  71.         });
  72.     }
  73.     
  74.     //创建菜单栏,参数是各个事件,返回菜单栏JMenuBar
  75.     private JMenuBar createJMenuBar(Action[] actions) {
  76.         JMenuBar menuBar = new JMenuBar();        //实例化菜单栏
  77.         JMenu menuFile = new JMenu("文件");
  78.         JMenu menuEdit = new JMenu("编辑");
  79.         JMenu menuAbout = new JMenu("帮助");
  80.         
  81.         //添加新菜单项
  82.         menuFile.add(new JMenuItem(actions[0]));
  83.         menuFile.add(new JMenuItem(actions[1]));
  84.         menuFile.add(new JMenuItem(actions[2]));
  85.         menuFile.add(new JMenuItem(actions[3]));
  86.         menuFile.add(new JMenuItem(actions[4]));
  87.         menuFile.add(new JMenuItem(actions[5]));
  88.         menuFile.add(new JMenuItem(actions[6]));
  89.         menuFile.add(new JMenuItem(actions[7]));
  90.         
  91.         menuBar.add(menuFile);
  92.         menuBar.add(menuEdit);
  93.         menuBar.add(menuAbout);
  94.         
  95.         return menuBar;
  96.     }
  97.     
  98.     //创建工具条,
  99.     private JToolBar createJToolBar(Action[] actions) {
  100.         JToolBar toolBar = new JToolBar();    //实例化工具条
  101.         
  102.         for(int i = 0; i < actions.length; i++) {
  103.             JButton button = new JButton(actions[i]);    //实例化新按钮
  104.             button.setRequestFocusEnabled(false);        //设置不需要焦点
  105.             toolBar.add(button);        //增加按钮到工具栏
  106.         }
  107.         
  108.         return toolBar;
  109.     }
  110.     
  111.     public static void main(String[] args) {
  112.         MyPad frame = new MyPad();
  113.         frame.setSize(500, 500);
  114.         frame.setVisible(true);
  115.         frame.setLocationRelativeTo(null);
  116.     }
  117. }

点击(此处)折叠或打开

  1. package cn.com.action;

  2. import java.awt.event.*;
  3. import javax.swing.*;

  4. import cn.com.shizongger.MyPad;

  5. public class CutAction extends AbstractAction {
  6.     MyPad myPad = null;
  7.     
  8.     public CutAction(MyPad myPad) {
  9.         super("剪切");
  10.         this.myPad = myPad;
  11.     }
  12.     @Override
  13.     public void actionPerformed(ActionEvent e) {
  14.         myPad.textPane.cut();
  15.     }
  16. }


点击(此处)折叠或打开

  1. package cn.com.action;

  2. import java.awt.event.*;
  3. import javax.swing.*;

  4. import cn.com.shizongger.MyPad;

  5. public class CopyAction extends AbstractAction {
  6.     MyPad myPad = null;
  7.     
  8.     public CopyAction(MyPad myPad) {
  9.         super("复制");
  10.         this.myPad = myPad;
  11.     }
  12.     
  13.     @Override
  14.     public void actionPerformed(ActionEvent e) {
  15.         myPad.textPane.copy();
  16.     }
  17. }


点击(此处)折叠或打开

  1. package cn.com.action;

  2. import java.awt.event.ActionEvent;

  3. import javax.swing.AbstractAction;

  4. import cn.com.shizongger.MyPad;

  5. public class PasteAction extends AbstractAction {

  6.     private MyPad myPad = null;
  7.     
  8.     public PasteAction(MyPad myPad) {
  9.         super("粘贴");
  10.         this.myPad = myPad;
  11.     }
  12.     
  13.     @Override
  14.     public void actionPerformed(ActionEvent e) {
  15.         myPad.textPane.paste();
  16.     }
  17. }


点击(此处)折叠或打开

  1. package cn.com.action;

  2. import java.awt.event.ActionEvent;

  3. import javax.swing.AbstractAction;
  4. import javax.swing.JOptionPane;

  5. import cn.com.shizongger.MyPad;

  6. public class AboutAction extends AbstractAction {
  7.     
  8.     MyPad myPad = null;
  9.     
  10.     public AboutAction(MyPad myPad) {
  11.         super("关于");
  12.         this.myPad = myPad;
  13.     }
  14.     
  15.     @Override
  16.     public void actionPerformed(ActionEvent e) {
  17.         String output = "文本编辑器1.0版本\n" +
  18.                 "作者:张仕宗\n" +
  19.                 "日期:2015.5,8\n" +
  20.                 "这是学习java以来的第一个比较综合的项目\n" +
  21.                 "你的支持是我前进的动力";
  22.         
  23.         JOptionPane.showMessageDialog(myPad, output);
  24.     }
  25. }


点击(此处)折叠或打开

  1. package cn.com.action;

  2. import java.awt.event.*;
  3. import java.io.File;
  4. import java.io.FileNotFoundException;
  5. import java.io.FileOutputStream;
  6. import java.io.IOException;
  7. import java.io.OutputStream;

  8. import javax.swing.*;

  9. import cn.com.shizongger.MyPad;

  10. public class ExitAction extends AbstractAction {

  11.     private MyPad myPad = null;
  12.     
  13.     public ExitAction(MyPad myPad) {
  14.         super("退出");
  15.         this.myPad = myPad;
  16.     }
  17.     
  18.     @Override
  19.     public void actionPerformed(ActionEvent e) {
  20.         File f = myPad.fileChooser.getSelectedFile();
  21.         
  22.         //如果文件不存在,那么直接推出
  23.         if(f != null) {
  24.             int option = JOptionPane.showConfirmDialog(null, "要保存文本么?");
  25.             if(JOptionPane.YES_OPTION == option){
  26.                 try {
  27.                     OutputStream os = new FileOutputStream(f);
  28.                     try {
  29.                         os.write(myPad.textPane.getText().getBytes());
  30.                     } catch (IOException e1) {
  31.                         e1.printStackTrace();
  32.                     }
  33.                 } catch (FileNotFoundException e1) {
  34.                     e1.printStackTrace();
  35.                 }
  36.                 System.exit(0);
  37.             }else if(JOptionPane.CANCEL_OPTION == option) {    
  38.                 return;
  39.             }
  40.         }
  41.         System.exit(0);
  42.     }
  43. }



阅读(1096) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~