| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 609 人关注过本帖
标题:能不能帮忙看看记事本
只看楼主 加入收藏
笔墨痕干
Rank: 1
等 级:新手上路
威 望:1
帖 子:56
专家分:0
注 册:2014-3-24
结帖率:84.21%
收藏
 问题点数:0 回复次数:3 
能不能帮忙看看记事本
package 记事本;
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import
import
import
import
import
import
import

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.filechooser.FileNameExtensionFilter;
public class MyNoteBook extends JFrame implements ActionListener {
    static final int i=0;
    static final String LINE_SEPARATOR =System.getProperty("Line_separator");
    JFrame jf,jf1,jf3;
    JScrollPane jsc;
    JMenu menu;
    JMenuBar menubar;
    JMenuItem item1,item2,item3,item4;
    JTextArea jtext,jtext1,jtext2;
    JFileChooser chooser;
    JDialog dialog;
    JButton jb1,jb2;
    JPanel jp;
    public MyNoteBook() {
        Inint();   
    }
    private void Inint() {
        jf=new JFrame("我的记事本");
        menu=new JMenu("文件");
        menubar=new JMenuBar();
        jf.setJMenuBar(menubar);
        menubar.add(menu);
        jtext=new JTextArea();
        jsc=new JScrollPane(jtext);
        jf.add(jsc);
        item1=new JMenuItem("打开");
        item2=new JMenuItem("保存");
        item4=new JMenuItem("另存为");
        item3=new JMenuItem("退出");
        menu.add(item1);
        menu.addSeparator();
        menu.add(item2);
        menu.addSeparator();
        menu.add(item4);
        menu.addSeparator();
        menu.add(item3);
        item1.addActionListener(this);
        item2.addActionListener(this);
        item3.addActionListener(this);
        jf.setVisible(true);
        jf.setBounds(300, 100, 500, 500);
    }
    public void actionPerformed(ActionEvent e) {
        BufferedReader buf = null;
        FileReader fr=null;
        if(e.getSource()==item1)//打开文件
        {
            
               try
               {
                   JFileChooser chooser = new JFileChooser();
                   int c=chooser.showOpenDialog(this);
                   if(c==JFileChooser.CANCEL_OPTION)
                        return;
                    else
                    {
                        
                        File file=chooser.getSelectedFile();
                        fr = new FileReader(file);
                        buf=new BufferedReader(fr);
                        String line=null;
                        jtext.setText("");
                        while((line=buf.readLine())!=null)
                        jtext.append(line+"\r\n");
                        
                    }
                   } catch (FileNotFoundException e1) {
                    
                } catch (IOException e1) {
            
                }
               finally
               {
                   if(buf!=null)
                    try {
                        buf.close();
                    } catch (IOException e1) {
                        
                    }
               }      
        }   
        if(e.getSource()==item2)//保存文件
        {
            try
            {
                File f =new File("新建文本文件.txt");
                if(f.exists()==true)
                {
                    Show();
                }
                else
                {
                    Method();
                }
               
               
            } catch (IOException e1) {
   
            }
        }
        if(e.getSource()==item3)//另存为文件
        {
            FileWriter fw=null;
            BufferedWriter buw=null;
            try
            {
                JFileChooser chooser = new JFileChooser();
                chooser.showSaveDialog(this);
                String S=chooser.getSelectedFile().getAbsolutePath();
            
                fw=new FileWriter(S);
                buw=new BufferedWriter(fw);
                String s=jtext.getText();
                buw.write(s);
                buw.flush();
               
            } catch (IOException e1) {
               
            }
            finally{
                if(buw!=null)
                    try {
                        buw.close();
                    } catch (IOException e1) {
                    }
            }
            
            
            
            
        }

    }
   
    private void Method() throws IOException {
        FileWriter fw=new FileWriter("新建文本文件.txt");
        BufferedWriter buw=new BufferedWriter(fw);
        String s=jtext.getText();
        buw.write(s);
        buw.flush();
        buw.close();
    }
    void Show() {
        jf1=new JFrame("警告");
        jb1=new JButton("确定");
        jb2=new JButton("取消");
        jp=new JPanel();
        jp.add(jb1);
        jp.add(jb2);
        jtext1=new JTextArea("有同名文件,是否覆盖!!");
        jtext1.setEditable(false);
        jf1.setLayout(new BorderLayout());
        jf1.add("Center",jtext1);
        jf1.add("South",jp);
        jf1.setVisible(true);
        jf1.setBounds(400,200,300,200);
        jb1.addMouseListener(new MouseAdapter(){
            public void mouseClicked(MouseEvent e) {
                    try
                    {
                        Method() ;
                    } catch (IOException e1) {
                    
                    }
                    
            }
            
        });
        jb2.addMouseListener(new MouseAdapter(){

            public void mouseClicked(MouseEvent e) {
                    jf3=new JFrame("提示");
                    jtext2=new JTextArea("请把文件另存为!");
                    jtext2.setEditable(false);
                    jf3.add(jtext2);
                    jf3.setVisible(true);
                    jf3.setBounds(400,400, 300, 100);   
            }
        });
    }
   
   
    public static void main(String[] args) {
        
        new MyNoteBook();
    }
   

}
请问下在另存为的地方怎么弄啊!!!!我弄的就是显示不出来!!
搜索更多相关主题的帖子: package 记事本 import 
2014-10-30 17:47
liucao
Rank: 10Rank: 10Rank: 10
来 自:恶灵之城
等 级:贵宾
威 望:13
帖 子:538
专家分:1575
注 册:2014-8-6
收藏
得分:0 
帮不上……我现在只有做出一个复制文件功能的水平,特此发帖证明咱论坛还有热血青年只是无力回天

one car come one car go ,two car peng peng people die.
2014-10-31 16:01
日知己所无
Rank: 11Rank: 11Rank: 11Rank: 11
等 级:贵宾
威 望:38
帖 子:427
专家分:2071
注 册:2014-3-22
收藏
得分:0 
代码当中定义的是:item4 = new JMenuItem("另存为");

但是关于item4没有任何处理,所以点击"另存为"就没有反应了
可以参考 退出 ,那里的处理修改一下就好了
2014-11-01 11:55
笔墨痕干
Rank: 1
等 级:新手上路
威 望:1
帖 子:56
专家分:0
注 册:2014-3-24
收藏
得分:0 
回复 3 楼 日知己所无
谢谢了,我已经改过来了,老长时间看没人回复,我就已经结账了,不好意思了!!
2014-11-05 19:48
快速回复:能不能帮忙看看记事本
数据加载中...
 
   



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

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