| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 2559 人关注过本帖
标题:记事本的编程
只看楼主 加入收藏
wsaaa
Rank: 1
等 级:新手上路
帖 子:136
专家分:0
注 册:2007-5-8
收藏
 问题点数:0 回复次数:7 
记事本的编程
如何用基础java编写一个程序实现记事本的打开、保存、新建、退出等最基础的记事本所具有的功能。希望能提供程序的源代码。
搜索更多相关主题的帖子: 记事本 
2007-11-27 18:14
canyue
Rank: 1
等 级:新手上路
威 望:1
帖 子:159
专家分:0
注 册:2007-10-4
收藏
得分:0 
还有些功能没有完全实现,但打开和关闭文件,及基本的编辑功能已经实现了。

myfrist.rar (6.47 KB)

别看我现在只有这么亮,总有一天会月圆的!
2007-11-27 22:45
寻寻觅觅1986
Rank: 1
等 级:新手上路
帖 子:18
专家分:0
注 册:2007-11-21
收藏
得分:0 
请看一下
import java.awt.*;
import java.awt.event.*;
import *;

public class NoteEdit extends Frame implements ActionListener
{

 /*定义主界面,一个菜单,一个文本区*/
 TextArea tArea;
 MenuBar mbr;

 String str,fileName;
 byte byteBuf[]=new byte[10000];
 FileDialog fileDlg;
 
 NoteEdit()
 {
  super("记事本");
  setSize(521,700);
  setLocation(250,30);
  addWindowListener(new WindowAdapter(){
   public void windowClosing(WindowEvent e)
   { System.exit(0); }
   });

   
  tArea=new TextArea();
  add("Center",tArea);
  tArea.setFont(new Font("隶书",Font.BOLD,16));
  
  
   mbr=new MenuBar();

   /*设置文件菜单项*/
   Menu file=new Menu("文件");   
   MenuItem newFile=new MenuItem("新建");
   MenuItem open=new MenuItem("打开");
   MenuItem save=new MenuItem("保存");
   MenuItem saveAs=new MenuItem("另存为");
   MenuItem print=new MenuItem("打印");
   MenuItem exit=new MenuItem("退出");
   file.add(newFile);
   file.add(open);
   file.add(save);
   file.add(saveAs);
   file.addSeparator();
   file.add(print);
   file.addSeparator();
   file.add(exit);
   newFile.addActionListener(this);
   open.addActionListener(this);
   save.addActionListener(this);
   saveAs.addActionListener(this);
   exit.addActionListener(this);
   mbr.add(file);


   /*设置编辑菜单项*/
   Menu edit=new Menu("编辑");
   MenuItem cut=new MenuItem("剪切");
   MenuItem copy=new MenuItem("复制");
   MenuItem paste=new MenuItem("粘贴");
   MenuItem delete=new MenuItem("删除");   
   edit.add(cut);
   edit.add(copy);
   edit.add(paste);
   edit.add(delete);   
   cut.addActionListener(this);
   copy.addActionListener(this);
   paste.addActionListener(this);
   delete.addActionListener(this);
   mbr.add(edit);


   /*设置格式菜单项*/
   Menu model=new Menu("格式");
   MenuItem font=new MenuItem("字体...");
   model.add(font);
   font.addActionListener(this);
   mbr.add(model);

   
   /*设置帮助菜单项*/
   Menu help=new Menu("帮助");
   MenuItem aboutHelp=new MenuItem("关于记事本");
   help.add(aboutHelp);
   aboutHelp.addActionListener(this);
   mbr.add(help);
   
   setMenuBar(mbr);
   
   setVisible(true);
 }
 public static void main(String[] args)
 {
  new NoteEdit();
 }
 
  /*响应菜单顼事件*/
 public void actionPerformed(ActionEvent e)
  {
   if(e.getActionCommand()=="退出")
     System.exit(0);

      if(e.getActionCommand()=="打开")
     {
      fileDlg=new FileDialog(this,"打开文件");
      fileDlg.show();
      fileName=fileDlg.getFile();
      try{
          FileInputStream in=new FileInputStream(fileName);
          in.read(byteBuf);
          in.close();
          str=new String(byteBuf);
          tArea.setText(str);
          setTitle("记事本-"+fileName);
          }catch(IOException ioe){}
      }
    if(e.getActionCommand()=="保存")
      {
        fileDlg=new FileDialog(this,"保存文件",FileDialog.SAVE);
        fileDlg.show();
        fileName=fileDlg.getFile();
        str=tArea.getText();
        byteBuf=str.getBytes();
        try{
            FileOutputStream out=new FileOutputStream(fileName);
            out.write(byteBuf);
            out.close();
           }catch(IOException ioe){}
   
      }
    if(e.getActionCommand()=="另存为")
       {
         fileDlg=new FileDialog(this,"另存为",FileDialog.SAVE);
         fileDlg.show();
        fileName=fileDlg.getFile();
        str=tArea.getText();
        byteBuf=str.getBytes();
        try{
            FileOutputStream out=new FileOutputStream(fileName);
            out.write(byteBuf);
            out.close();
           }catch(IOException ioe){}
   
      }
  }
}
本程序只完成了文件菜单下设置(新建末实现),其它菜单项只是一个界面而已,希望各位继续完善。
2007-11-29 15:04
wsaaa
Rank: 1
等 级:新手上路
帖 子:136
专家分:0
注 册:2007-5-8
收藏
得分:0 
回复 2# 的帖子
你的源代码好象有问题呀,运行的时候说找不到类FRAME1.但是你的类FRAME1也写了呀,不知道是什么原因.请高手完善一下.
2007-12-12 19:06
bobofei
Rank: 1
等 级:新手上路
帖 子:32
专家分:0
注 册:2007-5-9
收藏
得分:0 
基本功能都有了,你自己去看看!
你自己去运行的看看,我是可以的 !

记事本.rar (2.42 KB)
2007-12-26 13:05
longrm
Rank: 1
等 级:新手上路
帖 子:129
专家分:0
注 册:2007-6-18
收藏
得分:0 
不知道楼上几位是怎么搞的,安装java之后在demo目录下就有

[[italic] 本帖最后由 longrm 于 2007-12-26 14:11 编辑 [/italic]]

java群: 55919698

My blog: http://hi.baidu.com/longrm
2007-12-26 13:23
nwpu063417
Rank: 3Rank: 3
等 级:论坛游民
威 望:8
帖 子:428
专家分:28
注 册:2007-5-11
收藏
得分:0 
我也给你一个记事本代码,供你参考(有点简单,不要笑 ):
/*!Begin Snippet:file*/
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import *;

/**
 * Demonstrates the class {@link JFileChooser}. This class extends
 * class {@link @JPanel}.
 *
 * @author author name
 * @version 1.0.0
 */
public class TextEditor extends JPanel {

    /* Line separator */
    private final static String NEW_LINE =
            System.getProperty("line.separator");

    /* Standard error stream */
    private static PrintWriter  stdErr =
            new PrintWriter(System.err, true);


    private JButton  openButton;
    private JButton  saveButton;
    private TextArea  textArea;

    private JFileChooser  fileChooser;

    /**
     * Creates a window.
     *
     * @param args  not used.
     */
    public static void main(String[] args) {

        JFrame frame = new JFrame("TextEditor");

        frame.setContentPane(new TextEditor());
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(400,400);
        frame.setVisible(true);
    }

    /**
     * Creates a Graphical User Interface.
     */
    public TextEditor() {

        setBackground(Color.white);

        // Create the components
        openButton = new JButton("Open");
        saveButton = new JButton("Save");
        openButton.setFont(new Font("Serif", Font.BOLD, 30));
        saveButton.setFont(new Font("Serif", Font.BOLD, 30));
        
        textArea = new TextArea();

        fileChooser = new JFileChooser();
        //fileChooser.setFileSelectionMode(JFileChooser.FILES_ONLY);

        // Register the listeners for the buttons
        openButton.addActionListener(new OpenButtonListener());
        saveButton.addActionListener(new SaveButtonListener());

        // Add components to the container
        setLayout(new BorderLayout());
        add(openButton, BorderLayout.NORTH);
        add(textArea, BorderLayout.CENTER);
        add(saveButton, BorderLayout.SOUTH);
    }

    /**
     * This inner class handles button clicks for the Open button.
     */
    class OpenButtonListener implements ActionListener {

        /**
         * Opens a text file.
         *
         * @param event  the event object.
         */
        public void actionPerformed(ActionEvent event)  {

            int result = fileChooser.showOpenDialog(null);

            if (result == JFileChooser.APPROVE_OPTION) {

                try {

                    File file = fileChooser.getSelectedFile();
                    BufferedReader  input =
                            new BufferedReader(new FileReader(file));

                    String  line = input.readLine();

                    while (line != null)  {
                        textArea.append(line + NEW_LINE);
                        line = input.readLine();
                    }
                    input.close();
                    
                } catch (IOException ioe) {
                    stdErr.println(ioe.toString());
                }
            }
        }
    }

    /**
     * This inner class handles button clicks for the Save button.
     */
    class SaveButtonListener implements ActionListener {

        /**
         * Saves a text file.
         *
         * @param event  the event object.
         */
        public void actionPerformed(ActionEvent event) {

            int result = fileChooser.showSaveDialog(null);

            if (result == JFileChooser.APPROVE_OPTION) {

                try {

                    File file = fileChooser.getSelectedFile();
                    PrintWriter output =
                            new PrintWriter(new FileWriter(file));

                    output.print(textArea.getText());
                    output.close();
                    
                } catch (IOException ioe) {
                    stdErr.println(ioe.toString());
                }
            }
        }
    }
}
/*!End Snippet:file*/

2007-12-26 16:19
心笛
Rank: 1
等 级:新手上路
帖 子:24
专家分:0
注 册:2008-5-24
收藏
得分:0 
5楼给提供的代码好像不太完善吧,比如说“新建”,你没有判断文本域是否为空就直接清空了,有待完善
2008-05-27 10:26
快速回复:记事本的编程
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.025036 second(s), 9 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved