简单记事本的实现(JAVA)

来源:互联网 发布:mac电脑开机问号文件夹 编辑:程序博客网 时间:2024/05/01 11:06
import java.io.*;import java.util.ArrayList;import java.util.Scanner;public class Notebook{    private ArrayList <String> notes;    private File file;    public Notebook()    {        notes = new ArrayList <String>();        file = new File("note.txt");        Scanner fReader;        try {            fReader = new Scanner(file);            while (fReader.hasNext()) {                notes.add(fReader.next());            }            fReader.close();        }        catch (FileNotFoundException e) {            e.printStackTrace();        }    }    public void storeNote(String note)    {        notes.add(note);        try {            PrintWriter pw = new PrintWriter(new FileWriter(file,true));            pw.println(note);            pw.close();        }        catch (IOException e) {            e.printStackTrace();        }    }    public int numberOfNotes()    {        return notes.size();    }    public String showNote(int noteNumber)    {        if (noteNumber < 0) {            return null;        }        else if (noteNumber < numberOfNotes()) {            //This is a valid note number, so we can print it.            return notes.get(noteNumber);        }        else {            return null;        }    }    public void removeNote(int noteNumber)    {        if (noteNumber < 0) {            //This is not a valid note number, so do nothing.        }        else if (noteNumber < numberOfNotes()) {            //This is a valid note number.            notes.remove(noteNumber);        }        else {            //This is not a valid note number, so do nothing.        }    }    public static void main(String[] args) {        Notebook note1 = new Notebook();        note1.storeNote("2013-09-25");        note1.storeNote("2012-12-12");        note1.storeNote("2012-09-10");        System.out.println("当前记事本中有" + note1.numberOfNotes() + "条记录" );        note1.showNote(2);        note1.listNotes();        note1.removeNote(0);        note1.listNotes();    }    public String listNotes()//for-each循环    {        String notesString = "";        for (String note : notes) {            notesString = notesString + note + "\n";        }        return notesString;    }    /*public void listNotes()//while循环    {      int index = 0;      while (index < notes.size()) {        System.out.println(notes.get(index));        index ++;      }    }    public void listNotes()//迭代器循环    {      Iterator <String> it = notes.iterator();      while (it.hasNext()) {        System.out.println(it.next());      }    }*/    public String search(String searchString)    {        String noteString = "";        int index = 0;        while (index < notes.size()) {            String note = notes.get(index);            if (note.contains(searchString)) {                noteString = noteString + note + "\n";            }            index ++;        }        return noteString;    }}
import java.awt.*;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import javax.swing.*;public class NotebookFrame {    private JFrame frame;    private JTextField add_text;    private JTextField find_text;    private JTextArea view_text;    private JButton add_button;    private JButton find_button;    private JButton delete_button;    private JButton list_button;    private JLabel state_Label;    private JLabel add_Label;    private JLabel find_Label;    private JRadioButton rb1;    private JRadioButton rb2;    private Notebook notebook;    public NotebookFrame() {        notebook = new Notebook();        makeFrame();    }    public void makeFrame() {        frame = new JFrame("记事本");        JPanel contentPane = (JPanel)frame.getContentPane();        JPanel northPanel = new JPanel(new GridLayout(3,1));        JPanel north1 = new JPanel(new FlowLayout());        JPanel north2 = new JPanel(new FlowLayout());        JPanel north3 = new JPanel(new GridLayout());        northPanel.add(north1);        northPanel.add(north2);        northPanel.add(north3);        JPanel southPanel = new JPanel(new GridLayout(0,1));        JPanel south1 = new JPanel(new GridLayout(1,2,5,5));        JPanel south2 = new JPanel(new FlowLayout());        southPanel.add(south1);        southPanel.add(south2);        contentPane.add(northPanel,BorderLayout.NORTH);        contentPane.add(southPanel,BorderLayout.SOUTH);        add_text = new JTextField(16);        find_text = new JTextField(16);        view_text = new JTextArea(12,16);        JScrollPane scrollbar = new JScrollPane(view_text);        add_button = new JButton("添加");        String newNote = add_text.getText();        notebook.storeNote(newNote);        state_Label = new JLabel();        state_Label.setText("当前记事本中有" + notebook.numberOfNotes() + "条记录");        add_text.setText(null);        find_button = new JButton("查询");        find_button.addActionListener(new ActionListener() {            @Override            public void actionPerformed(ActionEvent args0) {                if (rb2.isSelected()) {                    int noteNum = Integer.parseInt(find_text.getText());                    String note = notebook.showNote(noteNum);                    view_text.setText(note);                }                else if (rb1.isSelected()) {                    String note = notebook.search(find_text.getText().trim());                    view_text.setText(note);                }            }        });        delete_button = new JButton("删除记录");        list_button = new JButton("列表显示");        list_button.addActionListener(new ActionListener() {            @Override            public void actionPerformed(ActionEvent args0) {                view_text.setText(notebook.listNotes());            }        });        state_Label = new JLabel("当前记事本中有" + notebook.numberOfNotes() + "条记录");        add_Label = new JLabel("输入新记录:");        find_Label = new JLabel("输入关键字:");        ButtonGroup buttonGroup = new ButtonGroup();        rb1 = new JRadioButton("按关键字",true);        rb1.addActionListener(new ActionListener() {            @Override            public void actionPerformed(ActionEvent e) {                find_Label.setText("按关键字:");            }        });        rb2 = new JRadioButton("按记录号");        rb2.addActionListener(new ActionListener() {            @Override            public void actionPerformed(ActionEvent arg0) {                find_Label.setText("按记录号:");            }        });        buttonGroup.add(rb1);        buttonGroup.add(rb2);        contentPane.add(scrollbar);        north1.add(add_text);        north1.add(add_button);        north1.add(add_Label);        north2.add(find_text);        north2.add(find_button);        north2.add(find_Label);        north3.add(rb1);        north3.add(rb2);        south1.add(delete_button);        south1.add(list_button);        south2.add(state_Label);        frame.pack();        Dimension d = Toolkit.getDefaultToolkit().getScreenSize();        frame.setLocation(d.width/2 - frame.getWidth()/2, d.height/2 - frame.getHeight()/2);        frame.setVisible(true);    }    public static void main(String[] args) {        new NotebookFrame();    }}
0 0
原创粉丝点击