⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 main.java

📁 实现记事本功能 包括打开
💻 JAVA
字号:
/* * To change this template, choose Tools | Templates * and open the template in the editor. */package noteofjava;/** * * @author Administrator *///package noteofjava;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import java.awt.event.WindowAdapter;import java.awt.event.WindowEvent;import java.awt.event.WindowListener;import java.io.BufferedReader;import java.io.BufferedWriter;import java.io.File;import java.io.FileReader;import java.io.FileWriter;import java.io.IOException;import java.util.logging.Level;import java.util.logging.Logger;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;class Note {    static JFrame jframe=new JFrame("记事本");    static JMenuBar jmenubar=new JMenuBar();    static JMenu jmenu1=new JMenu("文件");    static JMenu jmenu2=new JMenu("编辑");    static JMenu jmenu3=new JMenu("帮助");    static JMenuItem jopen=new JMenuItem("打开");    static JMenuItem jsave=new JMenuItem("保存");    static JMenuItem jsaveas=new JMenuItem("另存为");    private File file;    Note()    {        jframe.setSize(700, 600);        jframe.setVisible(true);        WindowListener l=new WindowAdapter()        {            @Override            public void windowClosing(WindowEvent e)            {                System.exit(0);            }        };        jframe.addWindowListener(l);        jframe.setJMenuBar(jmenubar);        jmenu1.add(jopen);        jmenu1.add(jsave);        jmenu1.add(jsaveas);        jmenubar.add(jmenu1);        jmenubar.add(jmenu2);        jmenubar.add(jmenu3);        final JTextArea jtext=new JTextArea();        JScrollPane jpane=new JScrollPane(jtext);        jframe.add(jpane);        ////////////////////////////////////////////////打开        jopen.addActionListener(new ActionListener()        {            public void actionPerformed(ActionEvent e) {                  JFileChooser fselect = new JFileChooser();                  fselect.showOpenDialog(null);                  file = fselect.getSelectedFile();                  try {                         FileReader fr = new FileReader(file);                         BufferedReader br = new BufferedReader(fr);                         String line = br.readLine();                         String str = ""; //保存文件中的字符串                         while (line != null) {                         jtext.append(line + "\n");                         line = br.readLine();                      }                      br.close();                      fr.close();                      }                       catch (Exception ex) {                        System.out.println(ex);                      }            }        }        );        ///////////////////////////////////////////////////////保存        jsave.addActionListener(new ActionListener()        {        public void actionPerformed(ActionEvent e) {        if (file == null) {        //打开对话框        JFileChooser fselect = new JFileChooser();        fselect.showSaveDialog(null);        //读取        file = fselect.getSelectedFile();}                                   try {                   FileWriter fw = null;                    try {                        fw = new FileWriter(file);                    } catch (IOException ex) {                        Logger.getLogger(Note.class.getName()).log(Level.SEVERE, null, ex);                    }                    BufferedWriter bw = new BufferedWriter(fw);                    bw.write(jtext.getText());                    bw.flush();                    fw.close();           }        catch (IOException ex)        {        System.out.println(ex);        }        }}        );        }    public static void main(String[] args) {       Note n=new Note();       //n.OpenClickPerformed(e);               Note.jframe.setVisible(true);        // TODO code application logic here    }}/* * To change this template, choose Tools | Templates * and open the template in the editor. */

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -