| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 761 人关注过本帖
标题:记事本的打开和保存功能问题,出错了。求帮忙看看
只看楼主 加入收藏
panlanghao
Rank: 1
等 级:新手上路
帖 子:10
专家分:3
注 册:2012-2-14
结帖率:33.33%
收藏
已结贴  问题点数:10 回复次数:1 
记事本的打开和保存功能问题,出错了。求帮忙看看
程序代码:
/**

 * 功能:记事本

 */
package com.test4;
import javax.swing.*;
import java.awt.event.*;
import *;
public class Note {

    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        NotePad np=new NotePad();
       
    }

}

class NotePad extends JFrame implements ActionListener{
    JTextArea jta=null;
    JMenuBar jmb=null;
    JMenu jm1=null;
    JMenuItem jmi1=null;
    JMenuItem jmi2=null;
   
    public NotePad(){
        //创建文本域
        jta=new JTextArea();
        jmb=new JMenuBar();
        jm1=new JMenu("文件");
        jm1.setMnemonic('F');
        jmi1=new JMenuItem("打开(O)");
        jmi2=new JMenuItem("保存(S)");
       
       
        this.add(jta);
        this.setJMenuBar(jmb);
        jmb.add(jm1);
        jm1.add(jmi1);
        jm1.add(jmi2);
       
        //注册监听
        jmi1.addActionListener(this);
        jmi2.addActionListener(this);
        //设置命令识别字符
        jmi1.setActionCommand("open");
        jmi2.setActionCommand("save");
       
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setSize(500, 600);
        this.setLocation(300, 100);
       
        this.setVisible(true);
         
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        // TODO Auto-generated method stub
        if(e.getActionCommand().equals("open")){
            //重要的文件组件JFileChooser
            JFileChooser jfc1=new JFileChooser();
            //设置名字
            jfc1.setDialogTitle("请选择文件");
            //默认方式
            jfc1.showOpenDialog(null);
            //显示
            jfc1.setVisible(true);
            //得到用户选择的文件的全路径
            String filename=jfc1.getSelectedFile().getAbsolutePath();
//            System.out.println(filename);
            //把文件的字符输到文本区域
            FileReader fr=null;
            BufferedReader br=null;
            try {
               
                fr=new FileReader(filename);
                br=new BufferedReader(fr);               
                //读取文件信息
                String s="";
                String alls="";
                while((s=br.readLine())!=null){
                    alls+=s+"\r\n";
                }
                //放置到JTextArea中
                jta.setText(alls);
               
            } catch (Exception e1) {
                e1.printStackTrace();
                // TODO: handle exception
            }finally{
                try {
                    fr.close();
                    br.close();
                } catch (Exception e2) {
                    e2.printStackTrace();
                    // TODO: handle exception
                }
            }
   
        }
        else if(e.getActionCommand().equals("save")){
            JFileChooser jfc2=new JFileChooser();
            jfc2.setDialogTitle("另存为");
            jfc2.showSaveDialog(null);//与打开不同
            jfc2.setVisible(true);
            //取得希望文件保存在何方,文件的全路径
            String filename2=jfc2.getSelectedFile().getAbsolutePath();
            //准备写入到指定文件
            FileWriter fw=null;
            BufferedWriter bw=null;       
            try {
                fw=new FileWriter(filename2);
                bw=new BufferedWriter(fw);               
                bw.write(this.jta.getText());
                bw.flush();
            } catch (Exception e2) {
                // TODO: handle exception
                e2.printStackTrace();
            }finally{
                try {
                    fw.close();
                    bw.close();
                } catch (Exception e3) {
                    // TODO: handle exception
                    e3.printStackTrace();
                }
            }
           
        }
    }
   
}
运行时,点击文件-打开,然后出错了
Exception in thread "AWT-EventQueue-0" java.lang.RuntimeException: Could not get shell folder ID list
    at sun.awt.shell.Win32ShellFolderManager2$ComInvoker.invoke(Win32ShellFolderManager2.java:506)
    at sun.awt.shell.Win32ShellFolder2.getFileSystemPath(Win32ShellFolder2.java:563)
    at sun.awt.shell.Win32ShellFolderManager2.getPersonal(Win32ShellFolderManager2.java:137)
    at sun.awt.shell.Win32ShellFolderManager2.get(Win32ShellFolderManager2.java:182)
    at sun.awt.shell.ShellFolder.get(ShellFolder.java:219)
    at javax.swing.filechooser.FileSystemView.getDefaultDirectory(FileSystemView.java:391)
    at javax.swing.JFileChooser.setCurrentDirectory(JFileChooser.java:552)
    at javax.swing.JFileChooser.<init>(JFileChooser.java:334)
    at javax.swing.JFileChooser.<init>(JFileChooser.java:286)
    at com.test4.NotePad.actionPerformed(Note.java:64)
    at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:1995)
    at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2318)
    at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:387)
    at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:242)
    at javax.swing.AbstractButton.doClick(AbstractButton.java:357)
    at javax.swing.plaf.basic.BasicMenuItemUI.doClick(BasicMenuItemUI.java:1225)
    at javax.swing.plaf.basic.BasicMenuItemUI$Handler.mouseReleased(BasicMenuItemUI.java:1266)
    at java.(Component.java:6216)
    at javax.swing.JComponent.processMouseEvent(JComponent.java:3265)
    at java.(Component.java:5981)
    at java.awt.Container.processEvent(Container.java:2041)
    at java.(Component.java:4583)
    at java.awt.Container.dispatchEventImpl(Container.java:2099)
    at java.(Component.java:4413)
    at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4556)
    at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4220)
    at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4150)
    at java.awt.Container.dispatchEventImpl(Container.java:2085)
    at java.awt.Window.dispatchEventImpl(Window.java:2475)
    at java.(Component.java:4413)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java:599)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:269)
    at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:184)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:174)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:169)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:161)
    at java.awt.EventDispatchThread.run(EventDispatchThread.java:122)
Caused by: Could not get shell folder ID list
    at sun.awt.shell.Win32ShellFolder2.getFileSystemPath0(Native Method)
    at sun.awt.shell.Win32ShellFolder2.access$900(Win32ShellFolder2.java:55)
    at sun.awt.shell.Win32ShellFolder2$8.call(Win32ShellFolder2.java:565)
    at sun.awt.shell.Win32ShellFolder2$8.call(Win32ShellFolder2.java:563)
    at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:303)
    at java.util.concurrent.FutureTask.run(FutureTask.java:138)
    at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)
    at sun.awt.shell.Win32ShellFolderManager2$ComInvoker$3.run(Win32ShellFolderManager2.java:458)
    at java.lang.Thread.run(Thread.java:619)

[ 本帖最后由 panlanghao 于 2013-6-11 16:06 编辑 ]
搜索更多相关主题的帖子: 记事本 
2013-06-11 14:16
yss0729
Rank: 3Rank: 3
来 自:江西 九江
等 级:论坛游侠
帖 子:43
专家分:197
注 册:2013-6-8
收藏
得分:10 
String filename=jfc1.getSelectedFile().getAbsolutePath();
获取系统文件异常。跟系统环境有关。 用GetPathFullName吧,
2013-06-17 11:27
快速回复:记事本的打开和保存功能问题,出错了。求帮忙看看
数据加载中...
 
   



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

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