2021-09-23 GUI(练习-保存文件)

2021-10-25  本文已影响0人  Denholm
import java.awt.FileDialog;
import java.awt.Frame;
import java.awt.Menu;
import java.awt.MenuBar;
import java.awt.MenuItem;
import java.awt.TextArea;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;

public class MyMenuTest {

    private Frame f;
    private MenuBar menuBar;
    private Menu menu;
    private MenuItem closeItem, openItem, saveItem;
    private FileDialog openDialog, saveDialog;
    private TextArea textArea;
    private File file;

    public MyMenuTest() {
        init();
    }

    private void init() {
        f = new Frame("my window");
        f.setBounds(300, 100, 500, 650);

        textArea = new TextArea();

        openDialog = new FileDialog(f, "我要打开", FileDialog.LOAD);
        saveDialog = new FileDialog(f, "我要保存", FileDialog.SAVE);

        menuBar = new MenuBar();
        menu = new Menu("文件");

        openItem = new MenuItem("打开");
        saveItem = new MenuItem("保存");
        closeItem = new MenuItem("退出");

        menu.add(openItem);
        menu.add(saveItem);
        menu.add(closeItem);

        menuBar.add(menu);

        f.setMenuBar(menuBar);

        f.add(textArea);

        MyEvent();

        f.setVisible(true);
    }

    private void MyEvent() {

        saveItem.addActionListener(e -> {
            saveDialog.setVisible(true);
            String fileName = saveDialog.getFile();
            String dirPath = saveDialog.getDirectory();
            if (null == fileName || null == dirPath) {
                return;
            }
            file = new File(dirPath, fileName);
            try {
                BufferedWriter writer = new BufferedWriter(new FileWriter(file));
                String text = textArea.getText();
                writer.write(text);
                writer.close();
            } catch (Exception e2) {
                throw new RuntimeException("写入失败");
            }
        });

        openItem.addActionListener(e -> {
            openDialog.setVisible(true);
            String fileName = openDialog.getFile();
            String dirPath = openDialog.getDirectory();
            // System.out.println(dirPath + "..." + fileName);

            if (null == fileName || null == dirPath) {
                return;
            }

            textArea.setText("");
            file = new File(dirPath, fileName);
            try {
                BufferedReader reader = new BufferedReader(new FileReader(file));
                String line = null;
                while ((line = reader.readLine()) != null) {
                    textArea.append(line + "\r\n");
                }
                reader.close();

                // BufferedInputStream in = new BufferedInputStream(new
                // FileInputStream(file));
                // byte[] buf = new byte[1024];
                // int len;
                // while ((len = in.read(buf)) != -1) {
                // String s = new String(buf, 0, len);
                // textArea.append(s + "\r\n");
                // }
                // in.close();
            } catch (Exception e2) {
                throw new RuntimeException("读取文件失败");
            }
        });

        closeItem.addActionListener(e -> System.exit(0));

        f.addWindowListener(new WindowAdapter() {

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

        });
    }

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

}
上一篇下一篇

猜你喜欢

热点阅读