Java编写的简单记事本

合集下载

我用java编写的记事本代码,分享给大家

我用java编写的记事本代码,分享给大家
backcolor.add(gray); backcolor.addSeparator();
backcolor.add(white); backcolor.addSeparator();
backcolor.add(pink); backcolor.addSeparator();
backcolor.add(morecolor);
JMenuItem red=new JMenuItem("红色");//","橙","黄","","青","蓝","紫","黑","灰","白","粉红"
JMenuItem green=new JMenuItem("绿色");
JMenuItem yellow=new JMenuItem("黄色");
clear.setFont(a); clear.setForeground(Color.MAGENTA);
paste.setFont(a); paste.setForeground(Color.MAGENTA);
cut.setFont(a); cut.setForeground(Color.MAGENTA);
JMenuItem black=new JMenuItem("黑色");
JMenuItem gray=new JMenuItem("灰色");
JMenuItem white=new JMenuItem("白色");
JMenuItem pink=new JMenuItem("粉红");

基于Swing的Mini记事本源程序

基于Swing的Mini记事本源程序

 Mini 记事本源程序 目录 1.程序入口  Notepad.java ........................................................................................................  1  2.工具栏按钮类 ToolBarButton.java ........................................................................................  1  3.字体对话框类 JFontChooser.java .........................................................................................  2  4.记事本核心类 FormMain.java  ...............................................................................................  6  5.在项目中创建属性文件 notepad.properties ................................................................. 21   说明:本程序是学习 Swing、文件流的练习程序,部分代码没有 没有实现,发现后自己补充;如果测试出 bug,自行改正。

   1.程序入口  Notepad.java package notepad; public class NotePad { public static void main(String[] args) { FormMain win = new FormMain("Mini记事本"); win.setVisible(true); } } 2.工具栏按钮类 ToolBarButton.java package notepad;      import javax.swing.Icon;  import javax.swing.ImageIcon;  import javax.swing.JButton;    /**      * @author  孙丰伟  E‐mail: sunfengweimail@    * @version  创建时间:May 3, 2008 12:49:50 PM      * @see      */  public class ToolBarButton extends JButton {      public ToolBarButton(Icon icon) {     super(icon);     setVerticalTextPosition(BOTTOM);     setHorizontalTextPosition(CENTER);    }      public ToolBarButton(String imageFile) {     this(new ImageIcon(imageFile)); // this 调用构造方法    }  } // end class ToolBarButton   3.字体对话框类 JFontChooser.java package notepad;    import java.awt.BorderLayout;  import java.awt.Container;  import java.awt.FlowLayout;  import java.awt.Font;  import java.awt.GraphicsEnvironment;  import java.awt.event.ActionEvent;  import java.awt.event.ActionListener;  import java.awt.event.ItemEvent;  import java.awt.event.ItemListener;    import javax.swing.*;  /******************************************    *  类    名: JFontChooser    *  作    者:孙丰伟    E‐mail: sunfengweimail@    *  时    间:2008‐4‐24    *  描    述:实现简单的字体设置    ******************************************/    public class JFontChooser extends JDialog implements ActionListener {    private Font font;    private Container contentPane;    private JButton btnOK,btnCancel;    private JComboBox comboFont,comboSize,comboStyle;    private JTextField txtFontName,txtFontSize,txtFontStyle;    private Box baseBoxV,fontBox,boxV1,boxV2,boxV3;    private JPanel btnPanel,labPanel;    private JLabel labFont,labStyle,labSize,labDemo;    public JFontChooser(JFrame frame,String title,boolean flag,Font font) {     super(frame,title,flag);       this.font=font;     this.initForm();      }      private void initForm() {     this.contentPane = this.getContentPane();     //     //  取系统中支持字体     //     GraphicsEnvironment gl = GraphicsEnvironment.getLocalGraphicsEnvironment();     String[] fontName = gl.getAvailableFontFamilyNames();     comboFont=new JComboBox(fontName);       labFont=new JLabel("字体:");     labStyle=new JLabel("样式:");     labSize=new JLabel("大小:");     txtFontName=new JTextField(font.getFamily());     txtFontSize=new JTextField(String.valueOf(font.getSize()));     txtFontStyle=new JTextField(String.valueOf(font.getStyle()));     labDemo=new JLabel("Hello,这是字体样例!");     labDemo.setFont(font);     labDemo.setHorizontalAlignment(JLabel.LEFT);          String[] fontStyle = { "常规", "粗体", "斜体"};     comboStyle=new JComboBox(fontStyle);       //大小          comboSize=new JComboBox();          for(int size=8;size<=72;size++)     {                                                                                                                                     comboSize.addItem(size);    }      //  //  为组合框设置事件  //  HandleItemListener itemListener=new HandleItemListener();  comboFont.addItemListener(itemListener);  comboStyle.addItemListener(itemListener);  comboSize.addItemListener(itemListener);    boxV1=Box.createVerticalBox();  boxV1.add(labFont);  boxV1.add(txtFontName);  boxV1.add(comboFont);    fontBox=Box.createHorizontalBox();  fontBox.add(boxV1);    boxV2=Box.createVerticalBox();  boxV2.add(labStyle);  boxV2.add(txtFontStyle);  boxV2.add(comboStyle);    boxV3=Box.createVerticalBox();  boxV3.add(labSize);  boxV3.add(txtFontSize);  boxV3.add(comboSize);      fontBox.add(boxV2);  fontBox.add(boxV3);    baseBoxV=Box.createVerticalBox();  baseBoxV.add(fontBox);  baseBoxV.add(Box.createVerticalStrut(20));  labPanel=new JPanel();  FlowLayout flow=new FlowLayout();  flow.setAlignment(FlowLayout.LEFT);  labPanel.setLayout(flow);  labPanel.add(labDemo);    baseBoxV.add(labPanel);            this.contentPane.add(baseBoxV);     btnPanel=new JPanel();     btnOK=new JButton("确定");     btnCancel=new JButton("取消");          btnOK.addActionListener(this);     btnCancel.addActionListener(this);          btnPanel.add(btnOK);     btnPanel.add(btnCancel);     this.contentPane.add(btnPanel, BorderLayout.SOUTH);     this.setLocationRelativeTo(null);     this.setResizable(false);     this.setSize(450, 200);     this.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);     this.setVisible(true);    }    class HandleItemListener implements ItemListener    {       @Override     public void itemStateChanged(ItemEvent e) {       // TODO Auto‐generated method stub       if(e.getSource()==comboFont)       {         txtFontName.setText(comboFont.getSelectedItem().toString());       }       if(e.getSource()==comboStyle)       {         txtFontStyle.setText(comboStyle.getSelectedItem().toString());       }       if(e.getSource()==comboSize)       {         txtFontSize.setText(comboSize.getSelectedItem().toString());       }       //setTitle(String.valueOf(comboStyle.getSelectedIndex()));       font=font=new  Font(txtFontName.getText(),comboStyle.getSelectedIndex(),Integer.parseInt(txtFontSize.getT ext()));       labDemo.setFont(font);     }              }    @Override    public void actionPerformed(ActionEvent e) {     // TODO Auto‐generated method stub     System.out.println(comboFont.getSelectedItem().toString());     System.out.println(Integer.parseInt(comboSize.getSelectedItem().toString()));     if(e.getSource()==btnOK)     {       //font=new  Font(comboFont.getSelectedItem().toString(),comboSize.getSelectedIndex(),Integer.parseInt( comboSize.getSelectedItem().toString()));       this.dispose();     }     else     {       font=null;       this.dispose();      }           }       public static void main(String[] args)    {     Font font=new Font("新宋体",Font.ITALIC,20);     JFontChooser fontChooser=new JFontChooser(null,"设置字体",true,font);              }    public Font getNoteFont() {          return font;    }  } 4.记事本核心类 FormMain.java /******************************************    *  文件名: FormMain.java    *  作    者:孙丰伟    *  时    间:2008‐4‐24    *  描    述:保存 FormMain 类,是记事本程序的核心类    *  测    试:本程序使用 Elipse 编写,JDK1.6 测试运行      ******************************************/    package notepad;    import java.awt.*;  import javax.swing.*;  import javax.swing.border.Border;  import javax.swing.event.DocumentEvent;  import javax.swing.event.DocumentListener;  import javax.swing.event.UndoableEditEvent;  import javax.swing.event.UndoableEditListener;  import javax.swing.undo.CannotRedoException;  import javax.swing.undo.CannotUndoException;  import javax.swing.undo.UndoManager;  import javax.swing.filechooser.*;  import java.awt.event.*;  import java.io.*;  import java.util.Properties;  /**    *      *  枚举类型:  用于标识视图菜单    *    */  enum VIEW{                //视图菜单    STATUS,              //状态栏    FOREGROUND,      //前景颜色    BACKGROUND        //背景颜色  }  /**    *      *  枚举类型:  用于标识文件菜单    *    */  enum FILE{                //用于文件菜单    NEW,                    //新建文件    OPEN,                  //打开文件      SAVE,                  //保存文件    SAVEAS,              //另存为    EXIT                    //退出  }  /**    *      *  枚举类型:  用于标识编辑菜单    *    */  enum EDIT                  //用于编辑菜单    {    COPY,                  //复制    CUT,                    //剪切    PASTE,                //粘贴    DELETE,              //删除    UNDO,                  //撤销      REDO                    //恢复  }  /******************************************    *  类    名: FormMain    *  作    者:孙丰伟    *  时    间:2008‐4‐24    *  描    述:记事本程序    *  内部类:    *      1. TxtNoteActionListener:编辑菜单监听    *      2. FileActionListener:文件菜单监听    *      3. ViewActionListener:视图菜单监听    *  方    法:      *            1. initialize()    初始窗体    *            2. saveFile()        保存文件    *            3. openFile()        打开文件    ******************************************/  class FormMain extends JFrame {       private Container contentPane;              //  内容面板    private JMenuBar mnuBar;              //  菜单容器    private JMenu mnuFile, mnuEdit,mnuFormat, mnuView;    //  主菜单    private JMenuItem mnuFileOpen, mnuFileSaveAs, mnuFileSave, mnuFileNew,  mnuFileExit;          //文件菜单项    private JMenuItem mnuEditCut, mnuEditPaste, mnuEditCopy,  mnuEditDelete,mnuEditUndo, mnuEditRedo; //编辑菜单项     private JMenuItem mnuPCut, mnuPPaste, mnuPCopy, mnuPDelete; //编辑菜单项 ____右键弹出菜单    private JCheckBoxMenuItem mnuViewStatus;            //  带复选的菜单, 控制状态栏是否出现    private JMenuItem mnuViewForeColor, mnuViewBackColor;        //前景与背景颜 色项    private JMenuItem mnuFormatFont;    //  字体菜单项    private JPopupMenu popupMenu;         //  右键菜单    private JToolBar toolBar;                 //  工具栏        private ToolBarButton toolNew, toolOpen, toolSave, toolExit; //工具栏按钮    private JTextArea txtNote;        //  记事本文本域    private JLabel labStatus;          //  状态栏    private final UndoManager undo = new UndoManager(); //  用于撤销    private JFileChooser fileChooser;  //  文件对话框    private boolean isChange = false;   //  文件内容是否改变    private String fileName = null;    //  保存文件名    private Font font;          //  文本域字体    private FileNameExtensionFilter filter = new FileNameExtensionFilter("文本文件",  "txt");      //设置文件过滤器      private Properties properties;    FormMain(String title) {     super(title);     initialize();    }      private void initialize() {     /*  程序窗口居中  */     this.setLocationRelativeTo(this);     this.setSize(600, 500);     this.setExtendedState(JFrame.MAXIMIZED_BOTH);     this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);       // JTextArea  事件监听器       TxtNoteActionListener txtListener = new TxtNoteActionListener();       //  文件监听器       FileActionListener fileListener = new FileActionListener();     //       //  设置工具栏     //     toolBar = new JToolBar("记事本");       toolSave = new ToolBarButton(new ImageIcon("images\\saveHS.GIF"));     toolOpen = new ToolBarButton(new ImageIcon("images\\OPENFOLD.GIF"));       toolSave.setText("保存文件");     toolOpen.setText("打开文件");       toolSave.setActionCommand("保存");     toolOpen.setActionCommand("打开");       toolSave.addActionListener(fileListener);     toolOpen.addActionListener(fileListener);       toolBar.add(toolOpen);     toolBar.addSeparator();     toolBar.add(toolSave);     toolBar.addSeparator();       Border toolBorder = BorderFactory.createBevelBorder(1);     toolBar.setBorder(toolBorder);     // ‐‐‐‐‐设置工具栏结束‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐          //     //  设置状态栏     //     labStatus = new JLabel("作者:孙丰伟");     labStatus.setBorder(BorderFactory.createBevelBorder(1));     this.add(bStatus, BorderLayout.SOUTH);     //     //  设置菜单     //     JMenuBar mnuBar = new JMenuBar();       this.setJMenuBar(mnuBar);     //     //  文件菜单     //     mnuFile = new JMenu("文件(F)");     Font mnuFont = new Font("新宋体", 0, 12);     mnuFile.setFont(mnuFont);     mnuFile.setMnemonic('F');       mnuFileNew = new JMenuItem("新建(N)");     mnuFileOpen = new JMenuItem("打开(O)");     mnuFileSave = new JMenuItem("保存(S)");     mnuFileSaveAs = new JMenuItem("另存为(A)");     mnuFileExit = new JMenuItem("退出(E)");       mnuFileNew.setMnemonic('N');     mnuFileOpen.setMnemonic('O');     mnuFileSave.setMnemonic('S');     mnuFileExit.setMnemonic('E');     mnuFileSaveAs.setMnemonic('A');       this.mnuFileNew.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_N,  InputEvent.CTRL_MASK));     this.mnuFileOpen.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_O,  InputEvent.CTRL_MASK));     this.mnuFileSave.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_S,  InputEvent.CTRL_MASK));       mnuFileNew.setActionCommand(FILE.NEW.toString());     mnuFileOpen.setActionCommand(FILE.OPEN.toString());     mnuFileSave.setActionCommand(FILE.SAVE.toString());     mnuFileSaveAs.setActionCommand(FILE.SAVEAS.toString());     mnuFileExit.setActionCommand(FILE.EXIT.toString());       mnuFileNew.addActionListener(fileListener);     mnuFileOpen.addActionListener(fileListener);     mnuFileSave.addActionListener(fileListener);     mnuFileSaveAs.addActionListener(fileListener);     mnuFileExit.addActionListener(fileListener);       mnuFile.add(mnuFileNew);     mnuFile.add(mnuFileOpen);     mnuFile.add(mnuFileSave);     mnuFile.addSeparator();     mnuFile.add(mnuFileExit);     mnuBar.add(mnuFile);       //  文件菜单设置结束‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐          //     //  编辑菜单     //     mnuEdit = new JMenu("编辑(E)");     mnuEdit.setFont(mnuFont);     mnuEdit.setMnemonic('E');       mnuEditCopy = new JMenuItem("复制(C)");     mnuEditCut = new JMenuItem("剪切(X)");     mnuEditPaste = new JMenuItem("粘贴(V)");     mnuEditDelete = new JMenuItem("删除(L)");     mnuEditUndo = new JMenuItem("撤销(U)");     mnuEditRedo = new JMenuItem("恢复(R)");       mnuEditCopy.setMnemonic('C');     mnuEditCut.setMnemonic('X');     mnuEditPaste.setMnemonic('V');     mnuEditDelete.setMnemonic('L');     mnuEditUndo.setMnemonic('U');     mnuEditRedo.setMnemonic('R');       mnuEditCopy.setActionCommand(EDIT.COPY.toString());     mnuEditCut.setActionCommand(EDIT.CUT.toString());     mnuEditPaste.setActionCommand(EDIT.PASTE.toString());     mnuEditDelete.setActionCommand(EDIT.DELETE.toString());     mnuEditUndo.setActionCommand(EDIT.UNDO.toString());     mnuEditRedo.setActionCommand(EDIT.REDO.toString());       mnuEditCopy.addActionListener(txtListener);     mnuEditCut.addActionListener(txtListener);     mnuEditPaste.addActionListener(txtListener);     mnuEditDelete.addActionListener(txtListener);     mnuEditUndo.addActionListener(txtListener);     mnuEditRedo.addActionListener(txtListener);       this.mnuEditCopy.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_C,  InputEvent.CTRL_MASK));     this.mnuEditCut.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_X,  InputEvent.CTRL_MASK));     this.mnuEditPaste.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_V,  InputEvent.CTRL_MASK));     this.mnuEditDelete.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_L,  InputEvent.CTRL_MASK));     this.mnuEditUndo.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_Z,  InputEvent.CTRL_MASK));     this.mnuEditRedo.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_Y,  InputEvent.CTRL_MASK));       mnuEdit.add(mnuEditUndo);     mnuEdit.add(mnuEditRedo);     mnuEdit.addSeparator();     mnuEdit.add(mnuEditCopy);     mnuEdit.add(mnuEditCut);     mnuEdit.add(mnuEditPaste);     mnuEdit.addSeparator();     mnuEdit.add(mnuEditDelete);     mnuBar.add(mnuEdit);          //  编辑菜单设置结束‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐          //     //  弹出式菜单     //     popupMenu = new JPopupMenu("我的记事本");       mnuPCopy = new JMenuItem("复制(C)");     mnuPCut = new JMenuItem("剪切(X)");     mnuPPaste = new JMenuItem("粘贴(V)");     mnuPDelete = new JMenuItem("删除(L)");       mnuPCopy.setActionCommand(EDIT.COPY.toString());     mnuPCut.setActionCommand(EDIT.CUT.toString());     mnuPPaste.setActionCommand(EDIT.PASTE.toString());     mnuPDelete.setActionCommand(EDIT.DELETE.toString());          mnuPCopy.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_C,  InputEvent.CTRL_MASK));     mnuPCopy.addActionListener(txtListener);     mnuPCut.addActionListener(txtListener);     mnuPPaste.addActionListener(txtListener);     mnuPDelete.addActionListener(txtListener);       mnuPCopy.setMnemonic('C');     mnuPCut.setMnemonic('X');     mnuPPaste.setMnemonic('V');     mnuPDelete.setMnemonic('L');     popupMenu.add(mnuPCopy);     popupMenu.add(mnuPCut);     popupMenu.add(mnuPPaste);     popupMenu.addSeparator();     popupMenu.add(mnuPDelete);       //  弹出式菜单设置结束‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐       //     //  格式菜单     //       mnuFormat=new JMenu("格式(O)");     mnuFormat.setMnemonic('O');          mnuFormatFont=new JMenuItem("字体(F)");     mnuFormatFont.setMnemonic('F');     //     //  字体菜单事件,调用字体窗口 JFontChooser     //     mnuFormatFont.addActionListener(new ActionListener(){       public void actionPerformed(ActionEvent e)       {         Font tempFont=new Font(font.getFamily(),font.getStyle(),font.getSize());         JFontChooser fontChooser=new JFontChooser(FormMain.this,"设置字 体",true,tempFont);               if(fontChooser.getNoteFont()!=null)               {             font=fontChooser.getNoteFont();             txtNote.setFont(font);             //             //  将字体写回属性文件             //               properties=new Properties();               properties.setProperty("fontName", font.getFamily());               properties.setProperty("fontStyle",  String.valueOf(font.getStyle()));               properties.setProperty("fontSize", String.valueOf(font.getSize()));               try {             properties.store(new FileWriter("notepad.properties"), null);           } catch (IOException e1) {             // TODO Auto‐generated catch block             e1.printStackTrace();           }               }       }       });          mnuFormat.add(mnuFormatFont);     mnuBar.add(mnuFormat);     //     //  视图菜单     //     ViewActionListener viewAction = new ViewActionListener();     mnuView = new JMenu("视图(V)");     mnuView.setMnemonic('V');     mnuBar.add(mnuView);     mnuViewStatus = new JCheckBoxMenuItem("状态栏", true);     mnuViewForeColor = new JMenuItem("前景颜色"); //  设置文本颜色     mnuViewBackColor = new JMenuItem("背景颜色"); //  设置背景颜色          mnuViewStatus.setActionCommand(VIEW.STATUS.toString());     mnuViewForeColor.setActionCommand(VIEW.FOREGROUND.toString());     mnuViewBackColor.setActionCommand(VIEW.BACKGROUND.toString());      。

java课程设计

java课程设计

课程设计(论文)题目名称记事本程序课程名称java 程序设计课程设计学生姓名学号系、专业指导教师2010年1 月3 日摘要本次课程设计的题目是用JA V A语言编写记事本程序,要求是:用图形界面实现;含有简单的文字编辑功能,如:剪切、复制、粘贴、删除;还能实现保存、另存为、查找、替换等功能。

本程序主要用到了Java swing组件和事件监听器,还用到了文件的输入输出流。

记事本的界面参考了Windows中的记事本界面,并且根据课程设计要求做了适当的调整。

关键词:记事本程序;Java语言;swing组件目录1 问题描述 (3)2 需求分析 (3)3 概要设计 (3)3.1自定义类说明 (3)3.2 程序流程图 (7)4 详细设计 (7)4.1 jishiben类的实现 (7)4.2 GUI程序界面设计 (7)4.3 事件处理 (9)5 测试分析 (12)6 课程设计总结 (16)6.1设计体会 (16)6.2致谢 (16)参考文献 (17)附录(源程序清单) (18)1 问题描述设计一个具有GUI界面的记事本,含有简单的文字编辑功能,如:剪切、复制、粘贴、删除;还能实现保存、另存为、查找、替换等功能。

这次课程设计由卢炜、刘秀春、伍霜霜和我四个人合作完成。

卢炜负责概要设计,刘秀春负责4.1 jishiben类的实现和4.2 GUI程序界面设计,伍霜霜负责5测试分析。

由于4.3事件处理和撰写程序关系密切,这两项都由我负责。

并且根据伍霜霜的测试的结果重新调整程序。

2 需求分析本程序要构建的记事本程序参照了Windows操作系统的记事本工具,其功能有以下几个方面:(1)、菜单中有“文件”、“编辑”、“帮助”三个主菜单;(2)、“文件”有“新建”、“打开”、“保存”、“另存为”、“退出”分别用于新建文件,打开文件,保存文件,另存文件,退出记事本。

(3)、“编辑”中,有“剪切”、“复制”、“粘贴”、“删除”、“查找”、“替换”、“全选”七个子菜单:用于剪切文字,复制文字,粘贴文字,删除文字,查找文件中的指定的字符串,替换文件中的指定的字符串,选定整个文件。

创建记事本程序的操作步骤

创建记事本程序的操作步骤

创建记事本程序的操作步骤
嘿,朋友们!今天咱就来讲讲怎么创建记事本程序。

这可不难哦,
就像走路一样,一步一步来就好啦!
首先,咱得找到那个神奇的“开始”按钮,就像找到开启宝藏的钥匙
孔一样。

点一下它,嘿,一大串东西就冒出来啦。

然后在那里面找“所
有程序”,这就像是在一个大宝藏堆里找我们想要的宝贝。

找到“所有程序”后,再在里面仔细瞅瞅,就能发现“附件”啦。

这“附件”就像是一个藏着各种小工具的小盒子。

打开这个小盒子,哇哦,
“记事本”就在里面等着我们呢!就像找到了我们一直想要的那个小玩
具一样。

这时候,只要轻轻点一下“记事本”,嘿,它就出来啦!就像变魔术
一样,一个可以让我们写字、记录想法的地方就出现啦。

你说这神奇不神奇?就这么简单几步,我们就创建好记事本程序啦。

这就好像我们盖房子,一块砖一块砖地往上垒,最后就建成了一座漂
亮的小房子。

我们可以在记事本里写日记,记录每天的喜怒哀乐;可以写计划,
规划我们未来要做的事情;还可以写一些小灵感,说不定哪天这些小
灵感就能变成大创意呢!
想象一下,如果没有记事本,我们的那些想法、那些记忆该往哪里放呢?难道就任由它们在脑子里飘来飘去,最后消失不见吗?那多可惜呀!
所以呀,学会创建记事本程序是多么重要的一件事呀!它就像我们的一个小助手,随时准备帮我们记录下生活中的点点滴滴。

现在,你是不是觉得创建记事本程序其实很简单呀?那就赶紧去试试吧,让我们的生活因为有了记事本而变得更加丰富多彩!别再犹豫啦,行动起来才是最重要的呢!。

java课程设计记事本设计报告

java课程设计记事本设计报告

java课程设计记事本设计报告一、教学目标本课程旨在通过Java编程语言实现一个记事本应用程序,让学生掌握Java编程的基本概念和方法,培养学生的编程能力和解决问题的能力。

1.理解Java编程语言的基本语法和结构。

2.掌握Java编程中的数据类型、变量、运算符、控制语句等基本概念。

3.学习Java中的类和对象的概念,理解封装、继承和多态的原理。

4.熟悉Java中的常用类库和方法。

5.能够运用Java编程语言编写简单的程序。

6.能够使用Java编程语言实现一个记事本应用程序,包括文本的增删改查等功能。

7.能够分析并解决编程过程中遇到的问题。

情感态度价值观目标:1.培养学生的团队合作意识和沟通能力,通过小组合作完成项目。

2.培养学生的创新思维和解决问题的能力,鼓励学生进行自主学习和探索。

3.培养学生的学习兴趣和自信心,让学生感受到编程的乐趣和成就感。

二、教学内容本课程的教学内容主要包括Java编程语言的基本概念和方法,以及记事本应用程序的设计和实现。

1.Java编程语言的基本概念和方法:–数据类型、变量、运算符、控制语句等基本概念。

–类和对象的概念,封装、继承和多态的原理。

–常用类库和方法的使用。

2.记事本应用程序的设计和实现:–用户界面设计:创建文本框、按钮等控件,实现用户输入和显示功能。

–文件操作:实现文件的打开、保存、关闭等功能,使用文件读写技术。

–文本处理:实现文本的增删改查等功能,使用数据结构和算法进行文本管理。

三、教学方法本课程将采用多种教学方法,包括讲授法、讨论法、案例分析法和实验法等,以激发学生的学习兴趣和主动性。

1.讲授法:教师通过讲解Java编程语言的基本概念和方法,以及记事本应用程序的设计和实现,引导学生掌握相关知识。

2.讨论法:学生分组进行讨论,分享自己的理解和思路,互相学习和交流。

3.案例分析法:分析实际案例,让学生了解记事本应用程序的实际应用场景和设计思路。

4.实验法:学生通过编写代码和进行实验,实现记事本应用程序的功能,培养学生的实际编程能力和解决问题的能力。

使用记事本编写java程序进行自我介绍

使用记事本编写java程序进行自我介绍

使用记事本编写java程序进行自我介绍Hi, my name is Emily. I am a computer science student with a passion for coding and problem solving. 你好,我叫Emily。

我是一名对编程和问题解决充满热情的计算机科学学生。

I have always been fascinated by the endless possibilities that programming offers. 从小到大,我一直被编程所带来的无限可能所吸引。

My journey with Java began when I was in high school. I remember the first time I wrote a simple "Hello, World!" program and was amazed at how a few lines of code could produce a message on the screen. 我的Java之旅始于我高中时期。

记得第一次写了一个简单的“Hello, World!”程序时,我被几行代码是如何在屏幕上产生一条消息而震惊。

As I delved deeper into Java, I discovered its versatility and power. Java's object-oriented nature allows for the creation of complex, modular programs, which was incredibly appealing to me. 随着我对Java的深入了解,我发现它的多功能性和强大性。

Java的面向对象的特性允许创建复杂、模块化的程序,这对我来说非常吸引人。

One of my most memorable coding projects was when I developed a Java application for a school project. It was a teamwork project where I collaborated with my classmates, and together we built a program that helped students manage their time and tasks effectively. 我在编码项目中最难忘的经历之一,就是我为学校项目开发一个Java应用程序。

《Java》课程设计》记事本

《Java》课程设计》记事本

《Java课程设计》记事本课程设计报告书目录一、设计课题二、设计目的三、操作环境四、设计场所(机房号、机器号或自己机器)五、设计过程(设计内容及主要程序模块)六、本次设计中用到的课程知识点(列出主要知识点)七、设计过程中遇到的问题及解决办法八、程序清单五、设计过程(设计内容及主要模块,不少于3000字)1.设计要求1)界面设计2)功能实现(1)文件菜单:新建、打开、保存、另存为、退出等。

(2)其中新建菜单项可以新建一个记事本程序;打开菜单项可以打开一个用户指定的文本文件,并将其内容显示在记事本的文本区域;保存和另存为菜单项可分别实现文件的保存和另存为3)编辑菜单:复制、剪切和粘贴等4)帮助菜单:软件版本信5)右键弹出快捷菜单2.总体设计1)功能模块图:图一功能模块图2)功能描述1、打开记事本。

首先是标准型的记事本,拥有文件、编辑。

格式和帮助。

如图1所示:图1标准型记事本界面2、在标准型的记事本界面中,进行的新建一个本件名字叫新记事本。

如图2记事本文件帮助新建打开保存另存为退出复制剪切粘贴编辑关于记事本右键快捷格式字体颜色图2新建记事本功能3、用打开文件打开刚刚新建的新记事本。

如图三所示。

图3—打开文件4、点击退出即可退出,如图4所示:图4—退出记事本5、点击帮助可以看到有关记事本的相关信息,其中有作者名、版本、许可条款、隐私声明等必要信息。

如图5所示:图5—帮助相关信息6、右键可实现复制、粘贴、剪切、清除等常用功能,方便用户可以快捷方便的使用记事本。

如图6所示:图6—右键功能7、编辑也可实现制、粘贴、剪切、清除等常用功能,方便用户选择自己适合的方式,自由选择方便的快捷方式使用。

如图7:图7—编辑八、程序清单package test;import java.io.File;import java.io.*;import java.awt.event.*;import java.awt.Toolkit;import java.awt.*;import javax.swing.*;import javax.swing.filechooser.*;public class Notebook extends JFrame implements ActionListener,ItemListener{ //组件创建JButton b_save,b_close; //按钮JTextArea textArea; //文本框File tempFile; //文件JPanel jp; //面板JMenu file,edit,style,help; //菜单JMenuItemf_new,f_open,f_save,f_close,f_saveas,e_copy, e_paste,e_cut,e_clear,e_selectAll,e_find,e_rep lace,s_font,s_color,h_editor,h_help; //菜单条JMenuBar jmb;JScrollPane jsp; //滚动面板JPopupMenu popUpMenu = new JPopupMenu(); //右键弹出式菜单JLabel stateBar;//标签JLabel jl,jj;JFileChooser jfc = new JFileChooser(); //文件选择JMenuItemje_copy,je_paste,je_cut,je_clear,je_selectAll,je _find,je_replace; //弹出式菜单条public Notebook(){jfc.addChoosableFileFilter(new FileNameExtensionFilter("文本文件(*.txt)","txt"));jmb = new JMenuBar();textArea = new JTextArea();jsp = new JScrollPane(textArea);file = new JMenu("文件");edit = new JMenu("编辑");style = new JMenu("格式");help = new JMenu("帮助");je_copy = new JMenuItem("复制(C) ");je_paste = new JMenuItem("粘贴(P) ");je_cut = new JMenuItem("剪切(X) ");je_clear = new JMenuItem("清除(D) ");je_selectAll = new JMenuItem("全选(A) ");je_find = new JMenuItem("查找(F) ");je_replace = new JMenuItem("替换(R) ");je_copy.addActionListener(this); //给弹窗式的各组件添加监听器je_paste.addActionListener(this);je_cut.addActionListener(this);je_clear.addActionListener(this);je_selectAll.addActionListener(this);je_find.addActionListener(this);je_replace.addActionListener(this);//给界面上方的菜单条添加监听器f_new = new JMenuItem("新建(N)");f_new.setAccelerator(KeyStroke.getKeyS troke('N',InputEvent.CTRL_MASK,false)); //设置带修饰符快捷键f_new.addActionListener(this);f_open = new JMenuItem("打开(O)");f_open.setAccelerator(KeyStroke.getKey Stroke('O',InputEvent.CTRL_MASK,false));f_open.addActionListener(this);f_save = new JMenuItem("保存(S)");f_save.setAccelerator(KeyStroke.getKey Stroke('S',InputEvent.CTRL_MASK,false));f_save.addActionListener(this);f_saveas = new JMenuItem("另存为");f_saveas.setAccelerator(KeyStroke.getKe yStroke(KeyEvent.VK_S,InputEvent.CTRL_ MASK|InputEvent.SHIFT_MASK));f_saveas.addActionListener(this);f_close = new JMenuItem("退出(W)");f_close.setAccelerator(KeyStroke.getKey Stroke('W',InputEvent.CTRL_MASK,false));f_close.addActionListener(this);e_copy = new JMenuItem("复制(C)");e_copy.setAccelerator(KeyStroke.getKey Stroke('C',InputEvent.CTRL_MASK,false));e_copy.addActionListener(this);e_paste = new JMenuItem("粘贴(V)");e_paste.setAccelerator(KeyStroke.getKeyStro ke('V',InputEvent.CTRL_MASK,false));e_paste.addActionListener(this);e_cut = new JMenuItem("剪切(X)"); e_cut.setAccelerator(KeyStroke.getKeyStroke ('X',InputEvent.CTRL_MASK,false));e_cut.addActionListener(this);e_clear = new JMenuItem("清除(D)");e_clear.setAccelerator(KeyStroke.getKeyStrok e('D',InputEvent.CTRL_MASK,false));e_clear.addActionListener(this);e_selectAll = new JMenuItem("全选(A)");e_selectAll.setAccelerator(KeyStroke.getKey Stroke('A',InputEvent.CTRL_MASK,false));e_selectAll.addActionListener(this);e_find = new JMenuItem("查找(F)");e_find.setAccelerator(KeyStroke.getKeyStrok e('F',InputEvent.CTRL_MASK,false));e_copy.addActionListener(this);e_replace = new JMenuItem("替换(R)");e_replace.setAccelerator(KeyStroke.getK eyStroke('R',InputEvent.CTRL_MASK,false)) ;e_replace.addActionListener(this);s_font = new JMenuItem("字体(T)");s_font.setAccelerator(KeyStroke.getKeyStrok e('T',InputEvent.CTRL_MASK,false));s_font.addActionListener(this);s_color = new JMenuItem("颜色(C)...");s_color.setAccelerator(KeyStroke.getKeyStro ke(KeyEvent.VK_C,InputEvent.CTRL_MAS K | InputEvent.SHIFT_MASK));s_color.addActionListener(this);h_editor = new JMenuItem("关于记事本");h_editor.setAccelerator(KeyStroke.getKeyStro ke(KeyEvent.VK_E,InputEvent.CTRL_MAS K));h_editor.addActionListener(this);h_help = new JMenuItem("帮助信息(H)");h_help.setAccelerator(KeyStroke.getKeyStrok e(KeyEvent.VK_I,InputEvent.CTRL_MASK) );h_help.addActionListener(this);//添加右键弹出式菜单popUpMenu.add(je_copy);popUpMenu.add(je_paste);popUpMenu.add(je_cut);popUpMenu.add(je_clear);popUpMenu.addSeparator();popUpMenu.add(je_selectAll);popUpMenu.add(je_find);popUpMenu.add(je_replace);//编辑区鼠标事件,点击右键弹出"编辑"菜单textArea.addMouseListener(new MouseAdapter(){public void mouseReleased(MouseEvent e) {if(e.getButton() == MouseEvent.BUTTON3)popUpMenu.show(e.getComponent(), e.getX(), e.getY());} //e.getComponent()和textArea具有同等效果public void mouseClicked(MouseEvent e){if(e.getButton() == MouseEvent.BUTTON1)popUpMenu.setVisible(false);}});this.setJMenuBar(jmb);this.setTitle("记事本程序");file.add(f_new); //添加文件菜单组件file.add(f_open);file.addSeparator(); //加分隔线file.add(f_save);file.add(f_saveas);file.addSeparator();file.add(f_close);edit.add(e_copy); //添加编辑菜单组件edit.add(e_paste);edit.add(e_cut);edit.add(e_clear);edit.addSeparator();edit.add(e_selectAll);edit.add(e_find);edit.add(e_replace);style.addSeparator();style.add(s_font);style.add(s_color);jmb.add(file); //添加格式菜单组件jmb.add(edit);jmb.add(style);jmb.add(help);help.add(h_editor); //添加帮助菜单组件help.add(h_help);//textArea.setWrapStyleWord(true); //设置在单词过长的时候是否要把长单词移到下一行。

Java记事本源代码(完整)

Java记事本源代码(完整)

/*** 作品:记事本* 作者:**** 功能:简单的文字编辑*/import java.awt.*;import java.awt.event.*;import java.io.*;import javax.swing.*;import javax.swing.event.ChangeEvent;import javax.swing.event.ChangeListener;class NotePad extends JFrame{private JMenuBar menuBar;private JMenu fielMenu,editMenu,formMenu,aboutMenu;private JMenuItemnewMenuItem,openMenuItem,saveMenuItem,exitMenuItem;private JMenuItemcutMenuItem,copyMenuItem,pasteMenuItem,foundItem,replaceItem,s electAll;private JMenuItem font,about;private JTextArea textArea;private JFrame foundFrame,replaceFrame;private JCheckBoxMenuItem wrapline;private JTextField textField1=new JTextField(15);private JTextField textField2=new JTextField(15);private JButton startButton,replaceButton,reallButton;int start=0;String value;File file=null;JFileChooser fileChooser=new JFileChooser();boolean wrap=false;public NotePad(){//创建文本域textArea=new JTextArea();add(new JScrollPane(textArea),BorderLayout.CENTER);//创建文件菜单及文件菜单项fielMenu=new JMenu("文件");fielMenu.setFont(new Font("微软雅黑",0,15));newMenuItem=new JMenuItem("新建",newImageIcon("icons\\new24.gif"));newMenuItem.setFont(new Font("微软雅黑",Font.BOLD,13));newMenuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent. VK_N,InputEvent.CTRL_MASK));newMenuItem.addActionListener(listener);openMenuItem=new JMenuItem("打开",newImageIcon("icons\\open24.gif"));openMenuItem.setFont(new Font("微软雅黑",Font.BOLD,13));openMenuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent .VK_O,InputEvent.CTRL_MASK));openMenuItem.addActionListener(listener);saveMenuItem=new JMenuItem("保存",newImageIcon("icons\\save.gif"));saveMenuItem.setFont(new Font("微软雅黑",Font.BOLD,13));saveMenuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent .VK_S,InputEvent.CTRL_MASK));saveMenuItem.addActionListener(listener);exitMenuItem=new JMenuItem("退出",newImageIcon("icons\\exit24.gif"));exitMenuItem.setFont(new Font("微软雅黑",Font.BOLD,13));exitMenuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent .VK_E,InputEvent.CTRL_MASK));exitMenuItem.addActionListener(listener);//创建编辑菜单及菜单项editMenu=new JMenu("编辑");editMenu.setFont(new Font("微软雅黑",0,15));cutMenuItem=new JMenuItem("剪切",newImageIcon("icons\\cut24.gif"));cutMenuItem.setFont(new Font("微软雅黑",Font.BOLD,13));cutMenuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent. VK_X,InputEvent.CTRL_MASK));cutMenuItem.addActionListener(listener);copyMenuItem=new JMenuItem("复制",newImageIcon("icons\\copy24.gif"));copyMenuItem.setFont(new Font("微软雅黑",Font.BOLD,13));copyMenuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent .VK_C,InputEvent.CTRL_MASK));copyMenuItem.addActionListener(listener);pasteMenuItem=new JMenuItem("粘贴",newImageIcon("icons\\paste24.gif"));pasteMenuItem.setFont(new Font("微软雅黑",Font.BOLD,13));pasteMenuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEven t.VK_V,InputEvent.CTRL_MASK));pasteMenuItem.addActionListener(listener);foundItem=new JMenuItem("查找");foundItem.setFont(new Font("微软雅黑",Font.BOLD,13));foundItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK _F,InputEvent.CTRL_MASK));foundItem.addActionListener(listener);replaceItem=new JMenuItem("替换");replaceItem.setFont(new Font("微软雅黑",Font.BOLD,13));replaceItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent. VK_R,InputEvent.CTRL_MASK));replaceItem.addActionListener(listener);selectAll=new JMenuItem("全选");selectAll.setFont(new Font("微软雅黑",Font.BOLD,13));selectAll.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK _A,InputEvent.CTRL_MASK));selectAll.addActionListener(listener);//创建格式菜单及菜单项formMenu=new JMenu("格式");formMenu.setFont(new Font("微软雅黑",0,15));wrapline=new JCheckBoxMenuItem("自动换行");wrapline.setFont(new Font("微软雅黑",Font.BOLD,13));wrapline.addActionListener(listener);wrapline.addChangeListener(new ChangeListener() {public void stateChanged(ChangeEvent e) {if(wrapline.isSelected()){textArea.setLineWrap(true);}elsetextArea.setLineWrap(false);}});font=new JMenuItem("字体");font.setFont(new Font("微软雅黑",Font.BOLD,13)); font.addActionListener(listener);//创建关于菜单aboutMenu=new JMenu("关于");aboutMenu.setFont(new Font("微软雅黑",0,15)); about=new JMenuItem("记事本……");about.setFont(new Font("微软雅黑",Font.BOLD,13)); about.addActionListener(listener);//添加文件菜单项fielMenu.add(newMenuItem);fielMenu.add(openMenuItem);fielMenu.add(saveMenuItem);fielMenu.addSeparator();fielMenu.add(exitMenuItem);//添加编辑菜单项editMenu.add(cutMenuItem);editMenu.add(copyMenuItem);editMenu.add(pasteMenuItem);editMenu.add(foundItem);editMenu.add(replaceItem);editMenu.addSeparator();editMenu.add(selectAll);//添加格式菜单项formMenu.add(wrapline);formMenu.add(font);//添加关于菜单项aboutMenu.add(about);//添加菜单menuBar=new JMenuBar();menuBar.add(fielMenu);menuBar.add(editMenu);menuBar.add(formMenu);menuBar.add(aboutMenu);setJMenuBar(menuBar);//创建两个框架,用作查找和替换foundFrame=new JFrame();replaceFrame=new JFrame();//创建两个文本框textField1=new JTextField(15);textField2=new JTextField(15);startButton=new JButton("开始");startButton.addActionListener(listener);replaceButton=new JButton("替换为");replaceButton.addActionListener(listener);reallButton=new JButton("全部替换");reallButton.addActionListener(listener);}//创建菜单项事件监听器ActionListener listener=new ActionListener() {public void actionPerformed(ActionEvent e) {String name=e.getActionCommand();if(e.getSource() instanceof JMenuItem){if("新建".equals(name)){textArea.setText("");file=null;}if("打开".equals(name)){if(file!=null){fileChooser.setSelectedFile(file);}intreturnVal=fileChooser.showOpenDialog(NotePad.this);if(returnVal==JFileChooser.APPROVE_OPTION){file=fileChooser.getSelectedFile();}try{FileReader reader=new FileReader(file);int len=(int)file.length();char[] array=new char[len];reader.read(array,0,len);reader.close();textArea.setText(new String(array));}catch(Exception e_open){e_open.printStackTrace();}}}if("保存".equals(name)){if(file!=null){fileChooser.setSelectedFile(file);}intreturnVal=fileChooser.showSaveDialog(NotePad.this);if(returnVal==JFileChooser.APPROVE_OPTION){file=fileChooser.getSelectedFile();}try{FileWriter writer=new FileWriter(file);writer.write(textArea.getText());writer.close();}catch (Exception e_save) {e_save.getStackTrace();}}if("退出".equals(name)){System.exit(0);}if("剪切".equals(name)){textArea.cut();}if("复制".equals(name)){textArea.copy();}if("粘贴".equals(name)){textArea.paste();}if("查找".equals(name)){value=textArea.getText();foundFrame.add(textField1,BorderLayout.CENTER);foundFrame.add(startButton,BorderLayout.SOUTH);foundFrame.setLocation(300,300);foundFrame.setTitle("查找");foundFrame.pack();foundFrame.setVisible(true);foundFrame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE );}if("替换".equals(name)){value=textArea.getText();JLabel label1=new JLabel("查找内容:");JLabel label2=new JLabel("替换为:");JPanel panel1=new JPanel();panel1.setLayout(new GridLayout(2,2));JPanel panel2=new JPanel();panel2.setLayout(new GridLayout(1,3));replaceFrame.add(panel1,BorderLayout.NORTH);replaceFrame.add(panel2,BorderLayout.CENTER);panel1.add(label1);panel1.add(textField1);panel1.add(label2);panel1.add(textField2);panel2.add(startButton);panel2.add(replaceButton);panel2.add(reallButton);replaceFrame.setTitle("替换");replaceFrame.setLocation(300,300);replaceFrame.pack();replaceFrame.setVisible(true);replaceFrame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLO SE);}if("开始".equals(name)||"下一个".equals(name)){String temp=textField1.getText();int s=value.indexOf(temp,start);if(value.indexOf(temp,start)!=-1){textArea.setSelectionStart(s);textArea.setSelectionEnd(s+temp.length());textArea.setSelectedTextColor(Color.GREEN);start=s+1;startButton.setText("下一个");}else {JOptionPane.showMessageDialog(foundFrame, "查找完毕!", "提示", 0,new ImageIcon("icons\\search.gif"));foundFrame.dispose();}}if("替换为".equals(name)){String temp=textField1.getText();int s=value.indexOf(temp,start);if(value.indexOf(temp,start)!=-1){textArea.setSelectionStart(s);textArea.setSelectionEnd(s+temp.length());textArea.setSelectedTextColor(Color.GREEN);start=s+1;textArea.replaceSelection(textField2.getText());}else {JOptionPane.showMessageDialog(foundFrame, "查找完毕!", "提示", 0,new ImageIcon("icons\\search.gif"));foundFrame.dispose();}}if("全部替换".equals(name)){String temp=textArea.getText();temp=temp.replaceAll(textField1.getText(),textField2.getTex t());textArea.setText(temp);}if("全选".equals(name)){textArea.selectAll();}if("字体".equals(name)){FontDialog fontDialog=newFontDialog(NotePad.this);fontDialog.setVisible(true);if(textArea.getFont()!=fontDialog.getFont()){textArea.setFont(fontDialog.getFont());}}if("记事本……".equals(name)){AboutDialog aboutDialog=newAboutDialog(NotePad.this);aboutDialog.setVisible(true);}}};//创建字体设置对话面板,并添加相应事件监听器class FontDialog extends JDialog implements ItemListener, ActionListener, WindowListener{public JCheckBox Bold=new JCheckBox("Bold",false);public JCheckBox Italic=new JCheckBox("Italic",false);public List Size,Name;public int FontName;public int FontStyle;public int FontSize;public JButton OK=new JButton("OK");public JButton Cancel=new JButton("Cancel");public JTextArea Text=new JTextArea("字体预览文本域\n0123456789\nAaBbCcXxYyZz");public FontDialog(JFrame owner) {super(owner,"字体设置",true);GraphicsEnvironmentg=GraphicsEnvironment.getLocalGraphicsEnvironment();String name[]=g.getAvailableFontFamilyNames();Name=new List();Size=new List();FontName=0;FontStyle=0;FontSize=8;int i=0;Name.add("Default Value");for(i=0;i<name.length;i++)Name.add(name[i]);for(i=8;i<257;i++)Size.add(String.valueOf(i));this.setLayout(null);this.setBounds(250,200,480, 306);this.setResizable(false);OK.setFocusable(false);Cancel.setFocusable(false);Bold.setFocusable(false);Italic.setFocusable(false);Name.setFocusable(false);Size.setFocusable(false);Name.setBounds(10, 10, 212, 259);this.add(Name);Bold.setBounds(314, 10, 64, 22);this.add(Bold);Italic.setBounds(388, 10, 64, 22);this.add(Italic);Size.setBounds(232, 10, 64, 259);this.add(Size);Text.setBounds(306, 40, 157, 157);this.add(Text);OK.setBounds(306, 243, 74, 26);this.add(OK);Cancel.setBounds(390, 243, 74, 26);this.add(Cancel);Name.select(FontName);Size.select(FontSize);Text.setFont(getFont());Name.addItemListener(this);Size.addItemListener(this);Bold.addItemListener(this);Italic.addItemListener(this);OK.addActionListener(this);Cancel.addActionListener(this);this.addWindowListener(this);}public void itemStateChanged(ItemEvent e) {Text.setFont(getFont());}public void actionPerformed(ActionEvent e) {if(e.getSource()==OK){FontName=Name.getSelectedIndex();FontStyle=getStyle();FontSize=Size.getSelectedIndex();this.setVisible(false);}else cancel();}public void windowClosing(WindowEvent e) {cancel();}public Font getFont(){if(Name.getSelectedIndex()==0) return new Font("新宋体",getStyle(),Size.getSelectedIndex()+8);else return newFont(Name.getSelectedItem(),getStyle(),Size.getSelectedIndex() +8);}public void cancel(){Name.select(FontName);Size.select(FontSize);setStyle();Text.setFont(getFont());this.setVisible(false);}public void setStyle(){if(FontStyle==0 || FontStyle==2)Bold.setSelected(false);else Bold.setSelected(true);if(FontStyle==0 || FontStyle==1)Italic.setSelected(false);else Italic.setSelected(true);}public int getStyle(){int bold=0,italic=0;if(Bold.isSelected()) bold=1;if(Italic.isSelected()) italic=1;return bold+italic*2;}public void windowActivated(WindowEvent arg0) {}public void windowClosed(WindowEvent arg0) {}public void windowDeactivated(WindowEvent arg0) {}public void windowDeiconified(WindowEvent arg0) {}public void windowIconified(WindowEvent arg0) {}public void windowOpened(WindowEvent arg0) {} }//创建关于对话框class AboutDialog extends JDialog implements ActionListener{ public JButton OK,Icon;public JLabel Name,Version,Author,Java;public JPanel Panel;AboutDialog(JFrame owner) {super(owner,"关于",true);OK=new JButton("OK");Icon=new JButton(new ImageIcon("icons\\edit.gif"));Name=new JLabel("Notepad");Version=new JLabel("Version 1.0");Java=new JLabel("JRE Version 6.0");Author=new JLabel("Copyright (c) 11-5-2012 By Jianmin Chen");Panel=new JPanel();Color c=new Color(0,95,191);Name.setForeground(c);Version.setForeground(c);Java.setForeground(c);Author.setForeground(c);Panel.setBackground(Color.white);OK.setFocusable(false);this.setBounds(250,200,280, 180);this.setResizable(false);this.setLayout(null);Panel.setLayout(null);OK.addActionListener(this);Icon.setFocusable(false);Icon.setBorderPainted(false);Author.setFont(new Font(null,Font.PLAIN,11));Panel.add(Icon);Panel.add(Name);Panel.add(Version);Panel.add(Author);Panel.add(Java);this.add(Panel);this.add(OK);Panel.setBounds(0, 0, 280, 100);OK.setBounds(180, 114, 72, 26);Name.setBounds(80, 10, 160, 20);Version.setBounds(80, 27, 160, 20);Author.setBounds(15, 70, 250, 20);Java.setBounds(80, 44, 160, 20);Icon.setBounds(16, 14, 48, 48);}public void actionPerformed(ActionEvent e) { this.setVisible(false);}}}//创建记事本public class ChenJianmin {public static void main(String[] args){EventQueue.invokeLater(new Runnable() {public void run() {NotePad notePad=new NotePad();notePad.setTitle("记事本");notePad.setVisible(true);notePad.setBounds(100,100,800,600);notePad.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);}});}}。

  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
// 替换对话框组件 private JLabel s_findTargetLabel = null;// 查找内容标签 private JTextField s_targetTextField = null;// 输入查找目标的 input private JButton s_findButton = null;// 查找按钮 private JLabel replaceLabel = null;// 查找内容标签 private JTextField replaceTargetTextField = null;// 输入查找目标的 input private JButton replaceButton = null;// 查找按钮 private JLabel nothingLabel_first = null;// 占位标签 first private JLabel nothingLabel_second = null;// 占位标签 second private JButton replaceAllButton = null;// 替换全部 private JCheckBox s_ignoreCaseCheckBox = null;// 区分大小写 private JLabel nothingLabel_third = null;// 占位标签 third private JButton s_cancleFindButton = null;// 取消按钮 private boolean isSearchFind = false;// 是否为"替换"对话框中的"查找下一个"按钮 private boolean isReplaceOne = false;// 是否替换一个 private boolean isRepalceAll = false;// 是否替换全部 private int time = 0;
Java 编写的简单记事本
目录结构:
下面是 NotepadGUI.java 中的代码
import java.awt.BorderLayout; import ponent; import java.awt.Dimension; import java.awt.Event; import java.awt.FlowLayout; import java.awt.Font; import java.awt.GraphicsEnvironment; import java.awt.GridLayout; import java.awt.HeadlessException; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.KeyEvent; import java.awt.event.KeyListener; import java.awt.event.MouseAdapter; import java.awt.event.MouseEvent; import java.awt.event.MouseMotionListener; import java.awt.event.WindowAdapter; import java.awt.event.WindowEvent; import java.io.File; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.FileWriter; import java.io.IOException; import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.Stack; import java.util.regex.Matcher; import java.util.regex.Pattern;
public class NotepadGUI implements ActionListener, MouseMotionListener,
PopupMenuListener, CaretListener, UndoableEditListener { // JFrame 主框架 private ImageIcon icon = null;// 主窗体图标 private JFrame mainFrame = null;// 主窗体 private JMenuBar menuBar = null;// 菜单栏 private JPanel mainPane = null;// 面板 private JTextArea textArea = null;// 文本域 private int maxRows = 25;// 文本域最大行数 private int maxColumns = 60;// 文本域最大列数 private JLabel state = null;// 状态栏:显示光标所在行数、列数 private JScrollPane scrollPane = null;// 滚动条 private JPopupMenu popupMenu; // 右键单击事件列表 private JFileChooser choose = null; // 文件对话框 private UndoManager undoManager;// 撤销管理类 private JDialog findDialog = null;// 查找对话框 private JDialog repalceDialog = null;// 替换对话框 private JDialog fontDialog = null;// 字体对话框 // 查找对话框组件
private JTextField fontKindTextField = null;// "字体"对话框"字体"input private JTextField font StyleTextField = null;// "字体"对话框"字形"input private JTextField font SizeTextField = null;// "字体"对话框"大小"input private JButton fontConfirmButton = null;// "字体"对话框"确定"按钮 private JComboBox fontKindComboBox = null;// "字体"对话框"字体"下拉列表 private JComboBox fontStyleComboBox = null;// "字体"对话框"字形"下拉列表 private JComboBox fontSizeComboBox = null;// "字体"对话框"大小"下拉列表 private JButton fontCancleButton = null;// "字体"对话框"取消"按钮 private JLabel fontNothingSecond = null;// "字体"对话框第四行占位组件 private JTextField fontExample = null;// "字体"对话框"实例"文本域 private JLabel fontNothingThrid = null;// "字体"对话框第四行占位组件
// 字体对话框组件 private String[] fontKind = null; private String[] fontStyle = { "PLAIN", "BOLD", "ITALIC" };// 常规、粗体、斜体 private String[] font Size = { "8", "10", "12", "14", "16"Байду номын сангаас "18", "20", "22", "24", "26", "28", "32", "36", "48", "72", "96" }; private String currentFontKind = "隶书"; private String currentFontStyel = "0"; private String currentFontSize = "20"; private JLabel fontKindLabel = null;// 字体 F private JLabel fontStyleLabel = null;// 字形 Y private JLabel fontSizeLabel = null;// 大小 S private JLabel fontNothingFirst = null;// "字体"对话框第一行占位组件
KeyListener,
private JLabel findTargetLabel = null;// 查找内容标签 private JTextField targetTextField = null;// 输入查找目标的 input private JButton findButton = null;// 查找按钮 private JCheckBox ignoreCaseCheckBox = null;// 区分大小写 private JPanel directionPanel = null;// 方向面板 private JRadioButton upRadioButton = null;// 向上单选按钮 private JRadioButton downRadioButton = null;// 向下单选按钮 private JButton cancleFindButton = null;// 取消按钮 private int startPosition = 0;// 查找开始时光标的位置 private int currentPosition = 0;// 查找当前光标的位置 private boolean isDownCase = true;// 是否默认向下 private boolean isIgnoreCase = true;// 是否区分大小写,默认不区分 true private Stack<Integer> matcherStack = null;// "向上"查找时用于储存匹配项开始索引的栈
相关文档
最新文档