A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 刘旭 黑马帝   /  2011-12-24 16:55  /  2403 人查看  /  6 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

本帖最后由 刘旭 于 2011-12-29 16:55 编辑

//近一下午写的记事本(模仿windows记事本新建、保存、另存为、退出、打开功能),请大家帮忙找找bug,并改改下面两个错误
import java.awt.*;
import java.awt.event.*;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;

public class Notebook {
        public static void main(String[] args) {
                new MyNotebook().init();
        }
}

class MyNotebook {
        public void init() {
                /****** 界面初始化开始 ******/
                Frame frame = new Frame("My Notebook");
                TextArea ta = new TextArea();
                MenuBar menubar = new MenuBar();
                Menu mwj = new Menu("文件");
                MenuItem wj_xj = new MenuItem("新建");
                MenuItem wj_dk = new MenuItem("打开");
                MenuItem wj_bc = new MenuItem("保存");
                MenuItem wj_lc = new MenuItem("另存为");
                MenuItem wj_tc = new MenuItem("退出");

                Menu mbj = new Menu("编辑");
                Menu mgs = new Menu("格式");
                Menu mck = new Menu("查看");
                Menu mbz = new Menu("帮助");
                mbj.add(new MenuItem("功能暂时未定……"));
                mgs.add(new MenuItem("功能暂时未定……"));
                mck.add(new MenuItem("功能暂时未定……"));
                mbz.add(new MenuItem("功能暂时未定……"));

                frame.setVisible(true);
                frame.setLocation(350, 150);
                frame.setSize(600, 400);
                frame.setMenuBar(menubar);
                frame.add(ta);

                menubar.add(mwj);
                menubar.add(mbj);
                menubar.add(mgs);
                menubar.add(mck);
                menubar.add(mbz);

                mwj.add(wj_xj);
                mwj.add(wj_dk);
                mwj.add(wj_bc);
                mwj.add(wj_lc);
                mwj.add(wj_tc);
                /****** 界面初始化结束 ******/

               
            /****** 事件监听编辑开始 ******/
                frame.addWindowListener(new MyEvemt().getMyWindowListener_close());
                wj_tc.addActionListener(new MyEvemt().getActionListener_close());
                wj_dk.addActionListener(new MyEvemt().getActionListener_openFile(frame, ta));
                wj_bc.addActionListener(new MyEvemt().getActionListener_saveFile(frame, ta));
                wj_lc.addActionListener(new MyEvemt().getActionListener_saveAsNewFile(frame, ta));
                wj_xj.addActionListener(new MyEvemt().getActionListener_createNewFile(ta));
                /****** 事件监听编辑结束 ******/
        }
}

class MyEvemt {

        WindowListener getMyWindowListener_close() {
                return new WindowAdapter() {
                        public void windowClosing(WindowEvent e) {
                                System.exit(0);
                        }
                };
        }

        ActionListener getActionListener_close() {
                return new ActionListener() {
                        public void actionPerformed(ActionEvent e) {
                                System.exit(0);
                        }
                };
        }

        ActionListener getActionListener_openFile(final Frame frame, final TextArea ta) {
                return new ActionListener() {
                        public void actionPerformed(ActionEvent e) {
                                FileTools.openFile(frame, ta);
                        }
                };
        }

        ActionListener getActionListener_saveAsNewFile(final Frame frame, final TextArea ta) {
                return new ActionListener() {
                        public void actionPerformed(ActionEvent e) {
                                FileTools.saveAsNewFile(frame, ta);
                        }
                };
        }

        ActionListener getActionListener_saveFile(final Frame frame, final TextArea ta) {
                return new ActionListener() {
                        public void actionPerformed(ActionEvent e) {
                                FileTools.saveFile(frame, ta);
                        }
                };
        }
        ActionListener getActionListener_createNewFile(final TextArea ta){
                return new ActionListener() {
                        public void actionPerformed(ActionEvent e) {
                                FileTools.createNewFile(ta);
                        }
                };
        }
}

class FileTools {
        static boolean isNewFile=true;
    static File openFile=null;
    static void createNewFile(TextArea ta){
            isNewFile=true;
            openFile=null;
            ta.setText("");
    }
        static void openFile(Frame frame, TextArea ta) {
                FileDialog fdOpen = new FileDialog(frame, "打开", FileDialog.LOAD);
                fdOpen.setVisible(true);
                FileReader fr = null;
                String fdir = fdOpen.getDirectory();
                String fname = fdOpen.getFile();
                if ((fdir == null) || (fname == null)) {
                        System.out.println("错误路径:" + fdir + "   文件:" + fname);
                        return;
                }
                openFile = new File(fdir, fname);
                isNewFile=false;
                try {
                        fr = new FileReader(openFile);
                        char ch[] = new char[1024];
                        int len = 0;
                        ta.setText("");
                        while ((len = fr.read(ch)) != -1) {
                                ta.append(new String(ch, 0, len));
                        }
                } catch (Exception e) {
                } finally {
                        try {
                                if (fr != null)
                                        fr.close();
                        } catch (IOException e) {
                        }
                }
        }
        static void saveFile(Frame frame, TextArea ta){
                if(isNewFile==true){
                        saveAsNewFile(frame, ta);
                        System.out.println("isNewFile");
                }
                else{
                        //为什么此处false改为true时, Dialog对话框显示不正确????
                        final Dialog d=new Dialog(frame,"提示",false);
                        d.setBounds(600, 300, 150, 95);
                        d.setLayout(new FlowLayout());
                        Label l=new Label("是否覆盖原文件内容?");
                        Button yes=new Button("Yes");
                        Button no=new Button("No");
                        d.setVisible(true);
                        d.add(l);
                        d.add(yes);
                        d.add(no);
                        d.setResizable(false);
                //        d.setModal(true);//当初始化d没有设置模式时,为什么此处设置的模式没效果???
                        final TextArea t=ta;
                        yes.addActionListener(new ActionListener() {                               
                                public void actionPerformed(ActionEvent e) {
                                  saveFcontent(openFile.toString(), t.getText());
                                  d.setVisible(false);
                                }
                        });
                        no.addActionListener(new ActionListener() {                               
                                public void actionPerformed(ActionEvent e) {
                                  d.setVisible(false);
                                }
                        });
                       
                       
                }
        }
        static void saveAsNewFile(Frame frame, TextArea ta){
        //        System.out.println("保存");
                FileDialog fdOpen = new FileDialog(frame, "保存", FileDialog.SAVE);
                fdOpen.setVisible(true);
                String fdir = fdOpen.getDirectory();
                String fname = fdOpen.getFile();
                if ((fdir == null) || (fname == null)) {
                        System.out.println("错误路径:" + fdir + "   文件:" + fname);
                        return;
                }
                saveFcontent(fdir+"\\"+fname, ta.getText());
        }
       
        private static void saveFcontent(String fdirAndfname,String fcontent) {
                FileWriter fw=null;
                try {
                        fw=new FileWriter(fdirAndfname);
                        fw.write(fcontent);
                } catch (IOException e) {
                       
                        e.printStackTrace();
                }finally{
                        try {
                                if(fw!=null)
                                        fw.close();
                        } catch (IOException e) {
                               
                        }
                }
               
        }
}

评分

参与人数 1技术分 +1 收起 理由
杨强 + 1

查看全部评分

6 个回复

倒序浏览
刘旭 黑马帝 2011-12-28 16:02:09
沙发
郁闷,都没人回答
回复 使用道具 举报
刘旭 黑马帝 2011-12-29 16:54:41
藤椅
既然没人回答,我就结贴了
回复 使用道具 举报
本帖最后由 monghuan 于 2011-12-29 17:27 编辑

呵呵,我还没学这部分,所以不清楚。不过lz这样结贴肯定得不到分的,

以下是这两个API中的方法,lz再研究一下,相信难不倒你的,
public Dialog(Frame owner,
              String title,
              boolean modal)构造一个最初不可见的 Dialog,它带有指定的所有者 Frame、标题和模式。

参数:
owner - dialog 的所有者,如果此 dialog 没有所有者,则该参数为 null
title - dialog 的标题,如果此 dialog 没有标题,则该参数为 null
modal - 指定在显示的时候是否阻止用户将内容输入到其他顶级窗口中。如果该参数为 false,则 dialog 是 MODELESS;如果该参数为 true,则模式类型属性被设置为 DEFAULT_MODALITY_TYPE

public void setModal(boolean modal)指定此 dialog 是否应该是有模式的。
此方法已过时,保留它只是为了后向兼容。可以使用 setModalityType() 代替。

注:更改可见 dialog 的模式只在隐藏该 dialog 并再次显示它时产生效果。

参数:
modal - 指定 dialog 是否阻止在显示的时候将内容输入其他窗口;调用 setModal(true) 等效于 setModalityType(Dialog.DEFAULT_MODALITY_TYPE),而调用 setModal(false) 等效于 setModalityType(Dialog.ModalityType.MODELESS)

评分

参与人数 1技术分 +1 收起 理由
杨强 + 1

查看全部评分

回复 使用道具 举报
刘旭 黑马帝 2011-12-29 21:11:25
报纸
找到原因了,就是d.setVisible(true);要放在d的各种参数设置后面才行,不过不知道为什么!!!
回复 使用道具 举报
刘旭 黑马帝 2011-12-29 21:12:00
地板
monghuan 发表于 2011-12-29 17:21
呵呵,我还没学这部分,所以不清楚。不过lz这样结贴肯定得不到分的,

以下是这两个API中的方法,lz再研究 ...

找到原因了,就是d.setVisible(true);要放在d的各种参数设置后面才行,不过不知道为什么!!!
回复 使用道具 举报
刘基军 黑马帝 2011-12-30 09:27:44
7#
从逻辑上理解:应该把衣服穿好了,再出来见人,而不能衣服还没穿好就跑出来,当着大家的面穿衣服,呵呵,学到时再研究研究
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马