首页 > 代码库 > 简易的记事本程序

简易的记事本程序

利用GUI和IO流做的简易的记事本程序

import java.awt.BorderLayout;import java.awt.EventQueue;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import java.io.BufferedReader;import java.io.FileNotFoundException;import java.io.FileReader;import java.io.IOException;import javax.swing.JFileChooser;import javax.swing.JFrame;import javax.swing.JMenu;import javax.swing.JMenuBar;import javax.swing.JMenuItem;import javax.swing.JScrollPane;import javax.swing.JTextArea;import javax.swing.filechooser.FileNameExtensionFilter;public class BarDemo extends JFrame {    /**     * Launch the application.     */    public static void main(String[] args) {        EventQueue.invokeLater(new Runnable() {            public void run() {                try {                    BarDemo frame = new BarDemo();                    frame.setVisible(true);                } catch (Exception e) {                    e.printStackTrace();                }            }        });    }    /**     * Create the frame.     */    public BarDemo() {        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);        setBounds(100, 100, 450, 300);                JMenuBar menuBar = new JMenuBar();        setJMenuBar(menuBar);                JMenu menu = new JMenu("\u6587\u4EF6");        menuBar.add(menu);                JScrollPane scrollPane = new JScrollPane();        getContentPane().add(scrollPane, BorderLayout.CENTER);                JTextArea textArea = new JTextArea();        scrollPane.setViewportView(textArea);                JMenuItem menuItem = new JMenuItem("\u6253\u5F00");        menuItem.addActionListener(new ActionListener() {            public void actionPerformed(ActionEvent e) {                                //1.创建一个文件选择器                JFileChooser chooser = new JFileChooser();                                //2.创建一个文件扩展名过滤器                FileNameExtensionFilter filter = null;                filter = new FileNameExtensionFilter("Java & Txt","java","txt" );                                //3.给文件选择器设置过滤器                chooser.setFileFilter(filter);                                //文件选择器调用了显示选择文件对话框。并获取该对话框的返回值                int returnVul = chooser.showOpenDialog(getContentPane());                //判断返回值                if(returnVul== JFileChooser.APPROVE_OPTION){                                        //清空                    textArea.setText("");                                        //获取选择的目录                    String str = chooser.getSelectedFile().getAbsolutePath();//                    System.out.println(str);                                        //读取文件                    try {                        BufferedReader bufr = new BufferedReader(new FileReader(str));                        String line = null;                        while((line=bufr.readLine())!=null){                            textArea.append(line+System.getProperty("line.separator"));                        }                                            } catch (FileNotFoundException e1) {                        e1.printStackTrace();                    } catch (IOException e1) {                        // TODO Auto-generated catch block                        e1.printStackTrace();                    }                                                                                                }                            }        });        menu.add(menuItem);                JMenuItem menuItem_1 = new JMenuItem("\u9000\u51FA");        menuItem_1.addActionListener(new ActionListener() {            public void actionPerformed(ActionEvent e) {                System.exit(0);            }        });        menu.add(menuItem_1);                    }}

 

简易的记事本程序