美文网首页
day22-13-GUI(练习-保存文件)

day22-13-GUI(练习-保存文件)

作者: 姗婷 | 来源:发表于2021-03-26 11:06 被阅读0次
import java.awt.*;
import java.awt.event.*;
import java.io.*;

public class MyMenuDemo
{
    public static void main(String[] args)
    {
        new MyMenuDemo();
    }

    MyMenuDemo()
    {
        init();
    }

    private Frame f;
    private MenuBar mb;
    private Menu m;
    private MenuItem mi,opernItem,saveItem;
    private FileDialog openDig,saveDig;
    private TextArea ta;
    private File file;

    public void init()
    {
        f = new Frame("i am a Frame");
        mb = new MenuBar();
        m = new Menu("文件");
        mi = new MenuItem("exit");
        opernItem =new MenuItem("打开");
        saveItem = new MenuItem("保存");
        ta = new TextArea();


        m.add(mi);
        m.add(opernItem);
        m.add(saveItem);
        mb.add(m);

        //不写模式默认也是load
        openDig= new FileDialog(f,"我要打开",FileDialog.LOAD);
        saveDig = new FileDialog(f,"我要保存",FileDialog.SAVE);


        f.setBounds(200,300,600,500);
        //不设置默认是边界布局
        // f.setLayout(new FlowLayout());
        f.setMenuBar(mb);
        f.add(ta);
        myEvent();
        f.setVisible(true);

    }

    public void myEvent()
    {


        saveItem.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e)
            {
                //是否弹出对话框,如果文件存在就弹出,存在就不弹直接保存
                if(file ==null)
                {
                    saveDig.setVisible(true);
                    String dirPath = saveDig.getDirectory();
                    String fileName = saveDig.getFile();
                    //如果是点击取消就不获取路径及文件名
                    if (dirPath == null || fileName == null) {
                        return;
                    }
                    //把文件提供到成员变量上来
                    file = new File(dirPath, fileName);
                }
                //源是文本区域中的数据写到文件里面进去,这时候就是目的
                try
                {
                    BufferedWriter bw = new BufferedWriter(new FileWriter(file));
                    String text = ta.getText();
                    bw.write(text);
                    bw.flush();
                    bw.close();

                }catch(IOException ex)
                {
                    throw new RuntimeException("写入失败");
                }
            }
        });

        //点击打开,就弹出我要打开窗口
        opernItem.addActionListener(new ActionListener()
        {
            @Override
            public void actionPerformed(ActionEvent e)
            {
                openDig.setVisible(true);
                //选择哪个文件,FileDialog
                String dirpath = openDig.getDirectory();
                String filepath = openDig.getFile();
                System.out.println(dirpath+"..."+filepath);
//                如果没有选择,就会报空指针,没有文件
                if(dirpath==null || filepath==null)
                {
                    return;
                }
                //如果没有清空,就会一直添加,我们就选择了就清空
                ta.setText("");
                file = new File(dirpath,filepath);
                try
                {
                    BufferedReader br = new BufferedReader(new FileReader(file));
                    String s = null;
                    while ((s =br.readLine())!=null)
                    {
                        ta.append(s+"\n\t");
                    }

                }
                catch (IOException ex)
                {
                    throw new RuntimeException("读取失败");
                }

            }
        });

        f.addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e)
            {
                System.exit(0);
            }
        });

        mi.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e)
            {
                System.exit(0);
            }
        });
    }

}

相关文章

  • day22-13-GUI(练习-保存文件)

  • 2017.9.28

    今天上午老师讲了文件的保存和读取,就是把上节课练习的程序进行完善,使它可以把输入的信息保存到一个文件里,然后再次打...

  • 22-模型的保存与加载

    api 模型的保存是保存为checkpoint文件。summary保存的文件为events文件

  • Git Shell命令

    提供练习的Demo地址:Demo项目地址 1、Git本地操作流程 修改工作区中文件(创建或编辑文件)->保存到暂存...

  • 站长素材图片下载之xpath学习

    主要是要了解 网页图片的懒图片加载在xpath上的应用 xpath的用法练习 图片文件的下载和保存练习网页对象:h...

  • vim文本编辑器的一些快捷键

    在命令模式下: :w//保存文件 :q //退出文件 :wq //保存并退出文件 :q! //不保存强制退出...

  • 4期c++9月28号

    今天课上练习了CArchive类和类对象的串行化相关的保存和读取功能的练习,课上还讲了关于文件对话框和COb...

  • iOS保存图片或视频

    导入头文件 保存图片 保存视频

  • Jabref和Docear联用解决Docear的中文乱码问题

    一定要用Jabref去保存bib文件 一定要用Jabref去保存bib文件 一定要用Jabref去保存bib文件 ...

  • vim

    末行模式 :w   保存不退出:w 新文件名  把文件另存为新文件:q   不保存退出:wq   保存退出:! ...

网友评论

      本文标题:day22-13-GUI(练习-保存文件)

      本文链接:https://www.haomeiwen.com/subject/dunbjktx.html