| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 804 人关注过本帖
标题:关于JAVA记事本问题
取消只看楼主 加入收藏
anhuizxw
Rank: 1
等 级:新手上路
帖 子:17
专家分:6
注 册:2009-7-12
结帖率:83.33%
收藏
已结贴  问题点数:20 回复次数: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
{
    String title="ERROR MESSAGE";
   
    int type=JOptionPane.ERROR_MESSAGE;
   
  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 jfont = new JMenuItem("字体...");
    jfont.addActionListener(new ActionListener()
    {
        public void actionPerformed(ActionEvent e)
        {
   
        }
    });
    jfont.setMnemonic('F');
    jfont.setAccelerator(KeyStroke.getKeyStroke('F',java.awt.Event.CTRL_MASK,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);
    jmode.add(jfont);
    jhelp.add(jabout);
    frame.setVisible(true);
  }
 public static void main(String[] args)
 {
    MyNoteBook myNoteBook = new MyNoteBook();
 }        
         
}
另存为、自动换行、字体要怎么实现???请高手解答
搜索更多相关主题的帖子: 记事本 JAVA 
2009-12-24 16:41
anhuizxw
Rank: 1
等 级:新手上路
帖 子:17
专家分:6
注 册:2009-7-12
收藏
得分:0 
我想问的是怎么在我上面的源代码上添加
2009-12-25 13:10
快速回复:关于JAVA记事本问题
数据加载中...
 
   



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

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