Java se项目:记事本
来源:互联网 发布:mac dare you 不好看 编辑:程序博客网 时间:2024/05/16 04:46
记事本算是一个小项目,对于学习JAVA SE新人来说,是适合不过的,既用不到网络编程,也永不到数据库的编程,也暂时不管国际化等!
项目分析:主类继承了JFrame类,是程序的入口点,这个类放入了菜单栏,状态栏和文本的编辑区域,构造方法里面初始化了菜单的各各按钮。并且定义了事件的抽象数组,这些抽象的数组分别各自定义,这些类都继承了AbstratAction,实现具体的事件处理! 需要注意的地方:由于我们需要在各个类里面处理事件,用到MyPad里面的属性,或者说是用到里面的变量!因此,我们的每个事件处理类都把MyPad这个类做为其属性,在构造方法时候作为参数传过去!
package cn.com.action; import java.awt.event.*; import javax.swing.*; import javax.swing.text.DefaultStyledDocument; import cn.com.shizongger.MyPad; public class NewActionextendsAbstractAction{ private MyPad myPad = null; public NewAction(MyPad myPad){ super("新建"); this.myPad= myPad; } @Override public voidactionPerformed(ActionEvent arg0){ //清空文本内容 myPad.textPane.setDocument(newDefaultStyledDocument()); } }
package cn.com.action; import java.awt.event.*; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.io.InputStream; import javax.swing.*; import cn.com.shizongger.MyPad; public class OpenActionextendsAbstractAction{ private MyPad myPad = null; public OpenAction(MyPad myPad){ super("打开"); this.myPad= myPad; } @Override public voidactionPerformed(ActionEvent e){ int i = myPad.fileChooser.showSaveDialog(myPad);//显示打开文件对话框 if(i==JFileChooser.APPROVE_OPTION){ //点击对话框中打开的选项 File f = myPad.fileChooser.getSelectedFile(); //得到选择的文件 try { InputStream is = new FileInputStream(f); try { myPad.textPane.read(is,"d"); } catch (IOException e1){ e1.printStackTrace(); } } catch (FileNotFoundException e1){ e1.printStackTrace(); } } } }
package cn.com.action; import java.awt.event.*; import java.io.File; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import javax.swing.*; import cn.com.shizongger.MyPad; public class SavaAction extends AbstractAction { MyPad myPad = null; public SavaAction(MyPad myPad) { super("保存"); this.myPad= myPad; } @Override public void actionPerformed(ActionEvent e) { int i = myPad.fileChooser.showSaveDialog(myPad); //显示保存文件对话框 if(i== JFileChooser.APPROVE_OPTION) { File f = myPad.fileChooser.getSelectedFile(); try { FileOutputStream os = new FileOutputStream(f); try { os.write(myPad.textPane.getText().getBytes()); } catch (IOException e1) { e1.printStackTrace(); } } catch (FileNotFoundException e1) { e1.printStackTrace(); } } } }
package cn.com.shizongger; import java.awt.BorderLayout; import java.awt.Color; import java.awt.Container; import java.awt.Font; import java.awt.event.WindowEvent; import java.awt.event.WindowListener; import javax.swing.*; import cn.com.action.*; public class MyPadextendsJFrame{ public JTextPane textPane=new JTextPane(); //文本窗格,编辑窗口 public JLabel statusBar=new JLabel(); //状态栏 public JFileChooserfileChooser=new JFileChooser(); //文件选择器 Action[] actions=null; //构造方法 public MyPad(){ super("记事本1.0版本"); //设置文本的字体和文字大小 textPane.setBackground(Color.LIGHT_GRAY); textPane.setForeground(Color.ORANGE); textPane.setFont(newFont("TimesRoman",Font.PLAIN, 18)); Action[] actions={ //Action数组,各种操作的命令 new NewAction(this), new OpenAction(this), new SavaAction(this), new CutAction(this), new CopyAction(this), new PasteAction(this), new AboutAction(this), new ExitAction(this) }; this.actions= actions; //将构造方法的actions赋值给类的属性actions this.setJMenuBar(createJMenuBar(actions)); //设置菜单栏 Container container = this.getContentPane(); //获取容器 container.add(createJToolBar(actions),BorderLayout.NORTH); //增加工具栏 container.add(textPane,BorderLayout.CENTER); container.add(statusBar,BorderLayout.SOUTH); this.addWindowListener(newWindowListener(){ @Override public void windowActivated(WindowEvent arg0){ } @Override public void windowClosed(WindowEvent arg0){ } @Override public void windowClosing(WindowEvent arg0){ System.exit(0); } @Override public void windowDeactivated(WindowEvent arg0){ } @Override public void windowDeiconified(WindowEvent arg0){ } @Override public void windowIconified(WindowEvent arg0){ } @Override public void windowOpened(WindowEvent arg0){ } }); } //创建菜单栏,参数是各个事件,返回菜单栏JMenuBar private JMenuBar createJMenuBar(Action[] actions) { JMenuBar menuBar = newJMenuBar(); //实例化菜单栏 JMenu menuFile = new JMenu("文件"); JMenu menuEdit = new JMenu("编辑"); JMenu menuAbout = new JMenu("帮助"); //添加新菜单项 menuFile.add(newJMenuItem(actions[0])); menuFile.add(newJMenuItem(actions[1])); menuFile.add(newJMenuItem(actions[2])); menuFile.add(newJMenuItem(actions[3])); menuFile.add(newJMenuItem(actions[4])); menuFile.add(newJMenuItem(actions[5])); menuFile.add(newJMenuItem(actions[6])); menuFile.add(newJMenuItem(actions[7])); menuBar.add(menuFile); menuBar.add(menuEdit); menuBar.add(menuAbout); return menuBar; } //创建工具条, private JToolBar createJToolBar(Action[] actions) { JToolBar toolBar = new JToolBar(); //实例化工具条 for(int i= 0; i< actions.length; i++){ JButton button = newJButton(actions[i]); //实例化新按钮 button.setRequestFocusEnabled(false); //设置不需要焦点 toolBar.add(button); //增加按钮到工具栏 } return toolBar; } public staticvoid main(String[] args){ MyPad frame = new MyPad(); frame.setSize(500, 500); frame.setVisible(true); frame.setLocationRelativeTo(null); } }
package cn.com.action; import java.awt.event.*; import javax.swing.*; import cn.com.shizongger.MyPad; public class CutActionextendsAbstractAction{ MyPad myPad = null; public CutAction(MyPad myPad){ super("剪切"); this.myPad= myPad; } @Override public voidactionPerformed(ActionEvent e){ myPad.textPane.cut(); } }
package cn.com.action; import java.awt.event.*; import javax.swing.*; import cn.com.shizongger.MyPad; public class CopyActionextendsAbstractAction{ MyPad myPad = null; public CopyAction(MyPad myPad){ super("复制"); this.myPad= myPad; } @Override public voidactionPerformed(ActionEvent e){ myPad.textPane.copy(); } }
package cn.com.action; import java.awt.event.ActionEvent; import javax.swing.AbstractAction; import cn.com.shizongger.MyPad; public class PasteActionextendsAbstractAction{ private MyPad myPad = null; public PasteAction(MyPad myPad){ super("粘贴"); this.myPad= myPad; } @Override public voidactionPerformed(ActionEvent e){ myPad.textPane.paste(); } }
package cn.com.action; import java.awt.event.*; import java.io.File; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.OutputStream; import javax.swing.*; import cn.com.shizongger.MyPad; public class ExitActionextendsAbstractAction{ private MyPad myPad = null; public ExitAction(MyPad myPad){ super("退出"); this.myPad= myPad; } @Override public voidactionPerformed(ActionEvent e){ File f = myPad.fileChooser.getSelectedFile(); //如果文件不存在,那么直接推出 if(f!=null) { int option = JOptionPane.showConfirmDialog(null,"要保存文本么?"); if(JOptionPane.YES_OPTION==option){ try { OutputStream os = new FileOutputStream(f); try { os.write(myPad.textPane.getText().getBytes()); } catch (IOException e1){ e1.printStackTrace(); } } catch (FileNotFoundException e1){ e1.printStackTrace(); } System.exit(0); }elseif(JOptionPane.CANCEL_OPTION==option) { return; } } System.exit(0); } }
package cn.com.action; import java.awt.event.ActionEvent; import javax.swing.AbstractAction; import javax.swing.JOptionPane; import cn.com.shizongger.MyPad; public class AboutActionextendsAbstractAction{ MyPad myPad = null; public AboutAction(MyPad myPad){ super("关于"); this.myPad= myPad; } @Override public voidactionPerformed(ActionEvent e){ String output = "文本编辑器1.0版本\n"+ "作者:张仕宗\n" + "日期:2015.5,8\n" + "这是学习java以来的第一个比较综合的项目\n" + "你的支持是我前进的动力"; JOptionPane.showMessageDialog(myPad,output); } }
1 0
- Java se项目:记事本
- java GUI(实例项目--记事本)
- Java SE - 扫雷项目展示
- 简单记事本程序java源码项目
- 记事本项目
- Log4j在Java se项目中的使用
- java记事本
- java记事本
- Java记事本
- java记事本
- Java记事本
- java记事本
- Java--记事本
- Java记事本
- java记事本
- Java记事本
- Java记事本
- Java记事本
- Nim Game——Difficulty:Easy
- Codeforces Round #359 (Div. 2) A
- Android工具类--获取屏幕信息类
- oracle 执行计划学习
- 约瑟夫问题JAVA算法
- Java se项目:记事本
- JavaSrcipt的Object.keys()
- 树--二叉树的数组实现
- 期货交易流程
- 数组形参2
- MyBatis学习之三:动态SQL语句
- spring的事务配置详解
- CSS3:各种浏览器私有属性
- Android Dialog的 WindowLeaked窗体泄露异常