| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 863 人关注过本帖
标题:JAVA记事本问题
取消只看楼主 加入收藏
anhuizxw
Rank: 1
等 级:新手上路
帖 子:17
专家分:6
注 册:2009-7-12
结帖率:83.33%
收藏
已结贴  问题点数:6 回复次数:1 
JAVA记事本问题
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import *;
import java.text.SimpleDateFormat;
import java.util.Calendar;


public class MyNoteBook extends JFrame implements ActionListener
{
    String title="ERROR MESSAGE";
   
    int type=JOptionPane.ERROR_MESSAGE;
   
    static String Path;
   
  public MyNoteBook()
  {
    final JFrame frame = new JFrame("我的记事本");
   
    final JTextArea text=new JTextArea();
   
    frame.setSize(600, 500);
   
    frame.addWindowListener(new WindowAdapter()
    {
        public void windowClosing(WindowEvent e)
        {
             System.exit(0);
        }
    });
   
    JPanel panel=new JPanel();
   
    panel.setLayout(new GridLayout(1,1));
   
    panel.add(new JScrollPane(text));
   
    frame.getContentPane().add(panel);
   
    JMenuBar Mbar = new JMenuBar();
   
    frame.setJMenuBar(Mbar);
   
    JMenu jfile = new JMenu("文件(F)", true);
   
    JMenu jedit = new JMenu("编辑(E)", true);
   
    JMenu jmode = new JMenu("格式(O)", true);
   
    JMenu jhelp = new JMenu("帮助(H)", true);
   
    jfile.setMnemonic('F');
   
    jedit.setMnemonic('E');
   
    jmode.setMnemonic('O');
   
    jhelp.setMnemonic('H');
   
    Mbar.add(jfile);
   
    Mbar.add(jedit);
   
    Mbar.add(jmode);
   
    Mbar.add(jhelp);
   
   
    JMenuItem jnew = new JMenuItem("新建");
    jnew.addActionListener(new ActionListener()
    {
        public void actionPerformed(ActionEvent e)
        {
              text.setText(" ");
        }
    });
    jnew.setMnemonic('N');
    jnew.setAccelerator( KeyStroke.getKeyStroke('N',java.awt.Event.CTRL_MASK,true));
   
    JMenuItem jopen = new JMenuItem("打开");
    jopen.addActionListener(new ActionListener()
    {
        public void actionPerformed(ActionEvent e)
        {
             JFileChooser openfile=new JFileChooser();
             openfile.setDialogTitle("打开文件");
             openfile.setApproveButtonText("打开");
             openfile.showOpenDialog(frame);
             File file=openfile.getSelectedFile();
             FileInputStream inputfile=null;
             String message="The file not Found";
            try
            {
              inputfile=new FileInputStream(file);
            }
             catch(FileNotFoundException fe)
             {      
             JOptionPane.showMessageDialog(frame,message,title,type);
             }
             int readbytes;
             String message1="read file error";
             try
             {
                while((readbytes=inputfile.read())!=-1)
                {
                   text.append(String.valueOf((char)readbytes));   
                }
             }
             catch(IOException ioe)
             {
               JOptionPane.showMessageDialog(frame,message1,title,type);  
             }
             String closemessage="close stream error";
             try{
                     inputfile.close();
             }
             catch(IOException ioe)
             {
               JOptionPane.showMessageDialog(frame,closemessage,title,type);   
             }
        }
    });
    jopen.setMnemonic('O');
    jopen.setAccelerator(KeyStroke.getKeyStroke('O',java.awt.Event.CTRL_MASK,true));
   
    JMenuItem jsave = new JMenuItem("保存");
    jsave.addActionListener(new ActionListener()
    {
        public void actionPerformed(ActionEvent e)
        {
           JFileChooser savefile=new JFileChooser();
           savefile.setApproveButtonText("保存");
           savefile.setDialogTitle("保存文件");
           savefile.showSaveDialog(frame);
           File filesa=savefile.getSelectedFile();
           String messagef="File not Found";
           FileOutputStream outputfile=null;
           try
           {
             outputfile=new FileOutputStream(filesa);
           }
           catch(FileNotFoundException fe)
           {      
             JOptionPane.showMessageDialog(frame,messagef,title,type);
           }
           String filecontent=text.getText();
           String wrmessage="write error";
           try
           {
              outputfile.write(filecontent.getBytes());
           }
           catch(IOException ioe)
           {
            JOptionPane.showMessageDialog(frame,wrmessage,title,type);   
           }
           String cmessage="close stream error";
           try
           {
               outputfile.close();
           }
           catch(IOException ioe)
           {
               JOptionPane.showMessageDialog(frame,cmessage,title,type);
           }
        }
    });
    jsave.setMnemonic('S');
    jsave.setAccelerator(KeyStroke.getKeyStroke('S',java.awt.Event.CTRL_MASK,true));
   
    JMenuItem jothersava = new JMenuItem("另存为...");
    jothersava.addActionListener(new ActionListener()
    {
        public void actionPerformed(ActionEvent e)
        {
           
        }
    });
   
    JMenuItem jquite = new JMenuItem("退出");
    jquite.addActionListener(new ActionListener()
    {
        public void actionPerformed(ActionEvent e)
        {
            /*int i=JOptionPane.showConfirmDialog(this,"确定要退出吗?","关闭",JOptionPane.YES_NO_OPTION);
            if(i==JOptionPane.YES_OPTION)*/
            System.exit(0);
        }
    });
    jquite.setMnemonic('Q');
    jquite.setAccelerator(KeyStroke.getKeyStroke('Q',java.awt.Event.CTRL_MASK,true));
   
     
    JMenuItem jcut = new JMenuItem("剪切");
    jcut.addActionListener(new ActionListener()
    {
        public void actionPerformed(ActionEvent e)
        {
              text.cut();
        }
    });
    jcut.setMnemonic('C');
    jcut.setAccelerator(KeyStroke.getKeyStroke('C',java.awt.Event.CTRL_MASK,true));
   
    JMenuItem jcopy = new JMenuItem("复制");
    jcopy.addActionListener(new ActionListener()
    {
        public void actionPerformed(ActionEvent e)
        {
              text.copy();
        }
    });
    jcopy.setMnemonic('o');
    jcopy.setAccelerator(KeyStroke.getKeyStroke('O',java.awt.Event.CTRL_MASK,true));
   
    JMenuItem jpaste = new JMenuItem("粘贴");
    jpaste.addActionListener(new ActionListener()
    {
        public void actionPerformed(ActionEvent e)
        {
              text.paste();
        }
    });
    jpaste.setMnemonic('P');
    jpaste.setAccelerator(KeyStroke.getKeyStroke('P',java.awt.Event.CTRL_MASK,true));
   
     JMenuItem jallselect = new JMenuItem("全选");
    jallselect.addActionListener(new ActionListener()
    {
        public void actionPerformed(ActionEvent e)
        {
              text.selectAll();
        }
    });
    jallselect.setMnemonic('A');
    jallselect.setAccelerator(KeyStroke.getKeyStroke('A',java.awt.Event.CTRL_MASK,true));
   
    JMenuItem jtimeDate = new JMenuItem("插入日期时间");
    jtimeDate.addActionListener(new ActionListener()
    {
        public void actionPerformed(ActionEvent e)
        {
            SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy'年'MM'月'dd'日'+HH:mm:ss");
            Calendar today = Calendar.getInstance();
            String timeDate = dateFormat.format(today.getTime());
            text.append(timeDate);
        }
     });
    jtimeDate.setMnemonic('D');
    jtimeDate.setAccelerator(KeyStroke.getKeyStroke('D',java.awt.Event.CTRL_MASK,true));
   
    JMenuItem jnewline = new JMenuItem("自动换行");
    jnewline.addActionListener(new ActionListener()
    {
        public void actionPerformed(ActionEvent e)
        {
            text.setLineWrap(true);
        }
    });
        
        
     JMenuItem jabout = new JMenuItem("帮助");
    jabout.addActionListener(new ActionListener()
    {
         public void actionPerformed(ActionEvent e)
         {
             int type=JOptionPane.INFORMATION_MESSAGE;
             String title="关于";
             String message="我的记事本";
             JOptionPane.showMessageDialog(frame,message,title,type);
         }
    });
    jfile.add(jnew);
    jfile.add(jopen);
    jfile.add(jsave);
    jfile.add(jothersava);
    jfile.addSeparator();
    jfile.add(jquite);
    jedit.add(jcut);
    jedit.add(jcopy);
    jedit.add(jpaste);
    jedit.addSeparator();
    jedit.add(jallselect);
    jedit.add(jtimeDate);  
    jmode.add(jnewline);
    jhelp.add(jabout);
    frame.setVisible(true);
  }
 public static void main(String[] args)
 {
    MyNoteBook myNoteBook = new MyNoteBook();
    myNoteBook.addWindowListener(new WindowAdapter()
        {
            public void windowClosing(WindowEvent e)
            {
                if(!MyNoteBook.JTGhost.getText().equals(MyNoteBook.text.getText()))
            {
            
                int Choose=JOptionPane.showConfirmDialog(myNoteBook,"文字已经改变,是否需要保存!","记事本",JOptionPane.YES_NO_CANCEL_OPTION);
                //选择需要保存。
                if(Choose==JOptionPane.YES_OPTION)
                {
                    try
                    {

                            FileDialog FDlog=new FileDialog(myNoteBook,"保存文件",FileDialog.SAVE);
                            FDlog.setVisible(true);
                            MyNoteBook.Path=FDlog.getDirectory()+FDlog.getFile();

                        FileWriter FW=new FileWriter(MyNoteBook.Path);
                        FW.write(WordPad.JT.getText());
                        FW.close();
                        //退出程序
                        System.exit(0);
                    }
                    catch(Exception s1)
                    {
                    }
                }
                //不保存,直接退出。
                else if(Choose==JOptionPane.NO_OPTION)
                {
                    System.exit(0);
                }
                //不进行任何操作。
                else if(Choose==JOptionPane.CANCEL_OPTION)
                {   
                }
           }
           //文件已经保存,则直接退出。
           else
           {
                  System.exit(0);
           }
            }
        });
 }        
         
}
不知道要怎么改了,好多错,请高手帮忙下,谢谢
搜索更多相关主题的帖子: 记事本 JAVA 
2009-12-28 21:49
anhuizxw
Rank: 1
等 级:新手上路
帖 子:17
专家分:6
注 册:2009-7-12
收藏
得分:0 
各位大侠帮帮忙呀
2009-12-29 09:14
快速回复:JAVA记事本问题
数据加载中...
 
   



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

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