Java记事本的设计与实现

写了一个记事本,实现了部分功能,个人感觉界面并不是很好看,但是受限于能力不足,目前也只到了这个样子。以后再改进吧!

用Java语言实现一个Windows的记事本,要求菜单栏有“文件”和“编辑”,“ 文件”菜单下有“新建”、“打开”、“保存”、“另存为”和“退出”菜单项, “编辑”菜单下有“剪切”、“复制”、“粘贴”和“删除” 菜单项。




package com.zaizai.free.SY7;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.io.*;

public class Text {
    public static void main(String[] args) {
        JFrame f = new JFrame("JFrame窗口");
        f.setLayout(new FlowLayout(FlowLayout.LEFT));

        f.setSize(350, 300);
        f.setLocation(300, 400);
        //f.setBounds(400,300,400,200);
        JTextArea area=new JTextArea(100,100);
        JMenuBar menuBar = new JMenuBar();
        //menuBar.getBaseline(100,100);
        f.add(menuBar);
        JMenu menu1 = new JMenu("文件");

        menuBar.add(menu1);

        JMenuItem item1 = new JMenuItem("新建");
        item1.addActionListener(new AbstractAction() {
            @Override
            public void actionPerformed(ActionEvent e) {
                area.setText("");
            }
        });



        menu1.add(item1);
        JMenuItem item2 = new JMenuItem("打开");
        item2.addActionListener(new AbstractAction() {
            @Override
            public void actionPerformed(ActionEvent e) {
                FileDialog fileDialog=new FileDialog(f, "打开文件", FileDialog.LOAD);
                fileDialog.show();
                String dirName=fileDialog.getDirectory();
                String fileName=fileDialog.getFile();
  //              System.out.println(dirName+fileName);
                FileReader fileReader;
                try {
                    fileReader = new FileReader(dirName+fileName);
                    BufferedReader bufferedReader=new BufferedReader(fileReader);
                    String aline;
                    while((aline=bufferedReader.readLine())!=null){
                        area.append(aline);
                        area.append("\r\n");
                    }
                    area.append(bufferedReader.readLine());
                    fileReader.close();
                    bufferedReader.close();
                } catch (FileNotFoundException fileNotFoundException) {
                    fileNotFoundException.printStackTrace();
                } catch (IOException ioException) {
                    ioException.printStackTrace();
                }



            }
        });

        menu1.add(item2);

        JMenuItem item3= new JMenuItem("保存");
        menu1.add(item3);
        item3.addActionListener(new AbstractAction() {
            @Override
            public void actionPerformed(ActionEvent e) {


                FileDialog fileDialog=new FileDialog(f, "保存文件", FileDialog.SAVE);
                fileDialog.show();







//                FilenameFilter filenameFilter=new FilenameFilter() {
//                    @Override
//                    public boolean accept(File dir, String name) {
//                        if(name.endsWith(".txt")){
//                            return true;
//                        }
//
//                        return false;
//                    }
//                };
//                fileDialog.setFilenameFilter(filenameFilter);

                String dirName=fileDialog.getDirectory();   //获取路径
                String fileName=fileDialog.getFile();   //获取文件名

                FileWriter fileWriter = null;
                try {
                    fileWriter = new FileWriter(dirName+fileName+".txt");
                    fileWriter.write(area.getText());
                    fileWriter.close();
                } catch (IOException ioException) {
                    ioException.printStackTrace();
                }



            }
        });
//        JMenuItem item4= new JMenuItem("另存为");
//        menu1.add(item4);


        JMenuItem item5= new JMenuItem("退出");
        menu1.add(item5);
        item5.addActionListener(new AbstractAction() {
            @Override
            public void actionPerformed(ActionEvent e) {
                System.exit(0);
            }
        });
        f.add(area);
        JMenu menu2 = new JMenu("编辑");
        menuBar.add(menu2);
        JMenuItem item6= new JMenuItem("剪切");
        menu2.add(item6);
        item6.addActionListener(new AbstractAction() {
            @Override
            public void actionPerformed(ActionEvent e) {
                area.selectAll();
                area.cut();
            }
        });
        JMenuItem item7= new JMenuItem("复制");
        menu2.add(item7);
        item7.addActionListener(new AbstractAction() {
            @Override
            public void actionPerformed(ActionEvent e) {
                area.copy();
            }
        });
        JMenuItem item8= new JMenuItem("粘贴");
        menu2.add(item8);
        item8.addActionListener(new AbstractAction() {
            @Override
            public void actionPerformed(ActionEvent e) {
                area.paste();
            }
        });
        JMenuItem item9= new JMenuItem("删除");
        menu2.add(item9);
        item9.addActionListener(new AbstractAction() {
            @Override
            public void actionPerformed(ActionEvent e) {
                area.selectAll();
                area.replaceSelection("");
            }
        });
        f.setVisible(true);
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }



}

你可能感兴趣的:(java,java,开发语言,后端)