oDiBo “开封菜”WWW.KFC.IM

KFC.im记录学习、工作、生活中的点点滴滴…
posts - 51, comments - 0, trackbacks - 0, articles - 0
  BlogJava :: 首页 :: 新随笔 :: 联系 :: 聚合  :: 管理

学习:写的一个记事本

Posted on 2009-06-28 22:06 London2012 阅读(153) 评论(0)  编辑  收藏 所属分类: CODE

以后要注意写注释。
写得不好,高手指正,程序较简单,预习流而写。

import java.awt.FileDialog;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;

import javax.swing.JFileChooser;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.JSeparator;
import javax.swing.JTextArea;

import javax.swing.WindowConstants;
import javax.swing.SwingUtilities;


public class NoteBookJFrame extends javax.swing.JFrame implements
        ActionListener 
{
    
private JMenuBar jMenuBar1;
    
private JMenuItem openMenu;
    
private JMenuItem otherSavaMenu;
    
private JMenuItem cutMenu;
    
private JScrollPane jScrollPane1;
    
private JTextArea jTextArea1;
    
private JMenuItem aboutMenu;
    
private JMenu helpMenu;
    
private JMenuItem allsMenu;
    
private JMenuItem reMenu;
    
private JMenuItem findMenu;
    
private JSeparator jSeparator3;
    
private JMenuItem deleteMenu;
    
private JMenuItem pasteMenu;
    
private JMenuItem copyMenu;
    
private JSeparator jSeparator2;
    
private JMenuItem passMenu;
    
private JMenu editMenu;
    
private JMenuItem cancelMenu;
    
private JSeparator jSeparator1;
    
private JMenuItem savaMenu;
    
private JMenuItem newMenu;
    
private JMenu jMenu1;
    
//存储文件的path,在“打开”中获取,在“保存”中使用
    String filename;            
    
//存储文件的复制信息
    String copy;                
    
//给撤销功能使用,存储删除、剪切、粘贴操作之前jTextArea中的数据
    String pass;                
    
static String reString;        //
    /*查找文件方法,失败*/
//    int  findIndex = 0 ;
//    public   void  find(String str,  int  cur)
//    {
//       int  i = jTextArea1.getText().indexOf(str,cur);
//       if (i >= 0 )
//        {
//           this.jTextArea1.setSelectionStart(i); // 使找到的字符串反白选中 
//            this.jTextArea1.setSelectionEnd(i + str.length());
//          findIndex =++ i; // 用于查找下一个 
//       } 
//       else 
//         {
//          JOptionPane.showMessageDialog( this , " 查找完毕! " ,  " 记事本 " ,
//              JOptionPane.OK_OPTION  +  JOptionPane.INFORMATION_MESSAGE);    
//      }     
//  } 
    /*查找文件方法end*/
    
/**
     * Auto-generated main method to display this JFrame
     
*/

    
public static void main(String[] args) {
        SwingUtilities.invokeLater(
new Runnable() {
            
public void run() {
                NoteBookJFrame inst 
= new NoteBookJFrame();
                inst.setLocationRelativeTo(
null);
                inst.setVisible(
true);
            }

        }
);
    }


    
public NoteBookJFrame() {
        
super();
        initGUI();
    }


    
private void initGUI() {
        
try {
            setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
            
this.setTitle("\u8bb0\u4e8b\u672c\u7a0b\u5e8f");
            getContentPane().setLayout(
null);
            
{
                jScrollPane1 
= new JScrollPane();
                getContentPane().add(jScrollPane1);
                jScrollPane1.setBounds(
00512348);
                
{
                    jTextArea1 
= new JTextArea();
                    jScrollPane1.setViewportView(jTextArea1);
//                    jTextArea1.setBounds(0, 64, 512, 348);
                }

            }

            
{
                jMenuBar1 
= new JMenuBar();
                setJMenuBar(jMenuBar1);
                
{
                    jMenu1 
= new JMenu();
                    jMenuBar1.add(jMenu1);
                    jMenu1.setText(
"\u6587\u4ef6");
                    
{
                        newMenu 
= new JMenuItem();
                        jMenu1.add(newMenu);
                        newMenu.setText(
"\u65b0\u5efa");
                        newMenu.addActionListener(
this);
                    }

                    
{
                        openMenu 
= new JMenuItem();
                        jMenu1.add(openMenu);
                        openMenu.setText(
"\u6253\u5f00");
                        openMenu.addActionListener(
this);
                    }

                    
{
                        savaMenu 
= new JMenuItem();
                        jMenu1.add(savaMenu);
                        savaMenu.setText(
"\u4fdd\u5b58");
                        savaMenu.addActionListener(
this);
                    }

                    
{
                        otherSavaMenu 
= new JMenuItem();
                        jMenu1.add(otherSavaMenu);
                        otherSavaMenu.setText(
"\u53e6\u5b58\u4e3a");
                        otherSavaMenu.addActionListener(
this);
                    }

                    
{
                        jSeparator1 
= new JSeparator();
                        jMenu1.add(jSeparator1);
                    }

                    
{
                        cancelMenu 
= new JMenuItem();
                        jMenu1.add(cancelMenu);
                        cancelMenu.setText(
"\u9000\u51fa");
                        cancelMenu.addActionListener(
this);
                    }

                }

                
{
                    editMenu 
= new JMenu();
                    jMenuBar1.add(editMenu);
                    editMenu.setText(
"\u7f16\u8f91");
                    
{
                        passMenu 
= new JMenuItem();
                        editMenu.add(passMenu);
                        passMenu.setText(
"\u64a4\u9500");
                        passMenu.addActionListener(
this);
                    }

                    
{
                        jSeparator2 
= new JSeparator();
                        editMenu.add(jSeparator2);
                    }

                    
{
                        cutMenu 
= new JMenuItem();
                        editMenu.add(cutMenu);
                        cutMenu.setText(
"\u526a\u5207");
                        cutMenu.addActionListener(
this);
                    }

                    
{
                        copyMenu 
= new JMenuItem();
                        editMenu.add(copyMenu);
                        copyMenu.setText(
"\u590d\u5236");
                        copyMenu.addActionListener(
this);
                    }

                    
{
                        pasteMenu 
= new JMenuItem();
                        editMenu.add(pasteMenu);
                        pasteMenu.setText(
"\u7c98\u8d34");
                        pasteMenu.addActionListener(
this);
                    }

                    
{
                        deleteMenu 
= new JMenuItem();
                        editMenu.add(deleteMenu);
                        deleteMenu.setText(
"\u5220\u9664");
                        deleteMenu.addActionListener(
this);
                    }

                    
{
                        jSeparator3 
= new JSeparator();
                        editMenu.add(jSeparator3);
                    }

                    
{
                        findMenu 
= new JMenuItem();
                        editMenu.add(findMenu);
                        findMenu.setText(
"\u67e5\u627e");
                        findMenu.addActionListener(
this);
                    }

                    
{
                        reMenu 
= new JMenuItem();
                        editMenu.add(reMenu);
                        reMenu.setText(
"\u66ff\u6362");
                        reMenu.addActionListener(
this);
                    }

                    
{
                        allsMenu 
= new JMenuItem();
                        editMenu.add(allsMenu);
                        allsMenu.setText(
"\u5168\u9009");
                        allsMenu.addActionListener(
this);
                    }

                }

                
{
                    helpMenu 
= new JMenu();
                    jMenuBar1.add(helpMenu);
                    helpMenu.setText(
"\u5e2e\u52a9");
                    
{
                        aboutMenu 
= new JMenuItem();
                        helpMenu.add(aboutMenu);
                        aboutMenu.setText(
"\u5173\u4e8e\u8bb0\u4e8b\u672c");
                        aboutMenu.addActionListener(
this);
                    }

                }

            }

            pack();
            
this.setSize(517406);
            
this.setResizable(false);
        }
 catch (Exception e) {
            e.printStackTrace();
        }

    }


    
public void actionPerformed(ActionEvent e) {
        String comm 
= e.getActionCommand();
        
if ("关于记事本".equals(comm)) {
            JOptionPane.showMessageDialog(
this"版本信息1.1 谢谢!");
        }

        
if ("退出".equals(comm)) {
            System.exit(
1);
        }

        
if ("新建".equals(comm)) {
            jTextArea1.setText(
"");    
        }

        
if ("打开".equals(comm)) {
            
/*------------      第一种方法       --------------*/
//            FileDialog d = new FileDialog(this, "打开文件", FileDialog.LOAD);
//            d.setVisible(true);
//            File f = null;
//            f = new File(d.getDirectory() + d.getFile());
//            // 读取文件到编辑区
//            filename=f.getPath();
//            for (int i = 0; i <= f.length(); i++) {
//                char[] ch = new char[i];
//                try {
//                    FileReader fr = new FileReader(f);
//                    fr.read(ch);
//                    String str = new String(ch);
//                    jTextArea1.setText(str);
//
//                } catch (FileNotFoundException e1) {
//                    e1.printStackTrace();
//                } catch (IOException e1) {
//                    e1.printStackTrace();
//                }
//            }
            /*------------      end       --------------*/
            
/*------------      第二种方法       --------------*/
            JFileChooser jfc
=new JFileChooser();
            jfc.setCurrentDirectory(
new File("c:"));//设置当前目录
            
//APPROVE_OPTION标识用于确认(yes、ok)按钮的工具提示文本的更改
            if(jfc.showOpenDialog(this)==JFileChooser.APPROVE_OPTION){
                File f
=new File(jfc.getSelectedFile().getPath());
                
for (int i = 0; i <= f.length(); i++{
                    
char[] ch = new char[i];
                    
try {
                        FileReader fr 
= new FileReader(f);
                        fr.read(ch);
                        String str 
= new String(ch);
                        jTextArea1.setText(str);
    
                    }
 catch (FileNotFoundException e1) {
                        e1.printStackTrace();
                    }
 catch (IOException e1) {
                        e1.printStackTrace();
                    }

                }

//                fname=jfc.getSelectedFile().getName();
//                txtFIieName.setText(fname);
//                filename=jfc.getSelectedFile().getPath();
//                System.out.println(path);
                /*-----------      end      ------------*/
            }


            
        }

        
if ("保存".equals(comm)) {
//            System.out.println(filename);
            if(null==filename){//如果是新建文件,提示执行“另存为”
                JOptionPane.showMessageDialog(this"这是新的文档,请执行\"另存为\"保存");
            }
else{
                
try {
                    FileOutputStream fout 
= new FileOutputStream(filename);
                    
byte bb[] = jTextArea1.getText().getBytes();//getBytes()解码为字节序列
                    fout.write(bb);
                    fout.close();
                }
 catch (IOException e1) {
                    
// TODO Auto-generated catch block
                    e1.printStackTrace();
                }

            }

        }

        
if("另存为".equals(comm)){
            
/*-------------       方法一      ---------------*/
//            FileDialog d = new FileDialog(this, "另存为", FileDialog.SAVE);
//            d.setVisible(true);
//            try {
//                filename = d.getDirectory() + d.getFile();
//                FileOutputStream fout = new FileOutputStream(filename + ".txt");
//                byte bb[] = jTextArea1.getText().getBytes();
//                fout.write(bb);
//                fout.close();
//            } catch (IOException ioe) {
//                System.err.println(e);
//            }
            /*-------------       end      ---------------*/
            
/*-------------       方法二    ---------------*/
            JFileChooser jfc
=new JFileChooser();
            jfc.setCurrentDirectory(
new File("c:"));//设置当前目录
            
//APPROVE_OPTION标识用于确认(yes、ok)按钮的工具提示文本的更改
            if(jfc.showSaveDialog(this)==JFileChooser.APPROVE_OPTION){
                File f
=new File(jfc.getSelectedFile().getPath());
                
try {
                    FileOutputStream fos 
= new FileOutputStream(f+".txt");
                    
byte bb[] = jTextArea1.getText().getBytes();
                    
try {
                        fos.write(bb);
                        fos.close();
                    }
 catch (IOException e1) {
                        
// TODO Auto-generated catch block
                        e1.printStackTrace();
                    }

                    
                }
 catch (FileNotFoundException e1) {
                    
// TODO Auto-generated catch block
                    e1.printStackTrace();
                }

            }

            
/*-------------       end    ---------------*/
        }

        
if("复制".equals(comm)){
            copy
=jTextArea1.getSelectedText();  
//            System.out.println(copy);
        }

        
if("粘贴".equals(comm)){
            pass
=jTextArea1.getText();
            jTextArea1.append(copy);
        }

        
if("剪切".equals(comm)){
            pass
=jTextArea1.getText();
            copy
=jTextArea1.getSelectedText();
            jTextArea1.replaceSelection(
"");
        }

        
if("删除".equals(comm)){
            pass
=jTextArea1.getText();
            jTextArea1.replaceSelection(
"");
        }

        
if("撤销".equals(comm)){
            jTextArea1.setText(pass);
        }

        
if("全选".equals(comm)){
            jTextArea1.selectAll();
        }

        
if("替换".equals(comm)){
            ReJFrame reJFrame
=new ReJFrame();
            reJFrame.setVisible(
true);
        }

        
if("查找".equals(comm)){
            reString
=jTextArea1.getText();
            ReJFrame reJFrame
=new ReJFrame();
            reJFrame.setVisible(
true);
//            System.out.println(reString);
        }

    }


}


只有注册用户登录后才能发表评论。


网站导航: