自己边学边做了一个JAVA记事本小程序,现学现卖,现在贴出来和喜欢JAVA的朋友一起分享下!
 
以下是程序代码:
//package shengang.book;
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.io.*;
import javax.swing.event.*;
import java.util.*;
public class NoteBook extends JFrame {
 JTextArea area = new JTextArea();
 static JFrame frame = new JFrame();
 File file = null;
 Color color = Color.black;
 JFileChooser filechooser = new JFileChooser();
 JColorChooser colorchooser = new JColorChooser();
 class color implements ActionListener { // 选择颜色的监听
  public void actionPerformed(ActionEvent e) {
   color = JColorChooser.showDialog(NoteBook.this, "", color);
   area.setForeground(color);
  }
 }
 class xinjian implements ActionListener { // 新建的监听
  public void actionPerformed(ActionEvent e) {
   area.setText("");
  }
 }
 class dakai implements ActionListener { // 打开的监听
  public void actionPerformed(ActionEvent e) {
   if (file != null)
    filechooser.setSelectedFile(file);
   int returnVal = filechooser.showOpenDialog(NoteBook.this);
   if (returnVal == JFileChooser.APPROVE_OPTION) {
    file = filechooser.getSelectedFile();
    openFile();
   }
  }
 }
 class baocun implements ActionListener { // 保存的监听
  public void actionPerformed(ActionEvent e) {
   if (file != null)
    filechooser.setSelectedFile(file);
   int returnVal = filechooser.showSaveDialog(NoteBook.this);
   if (returnVal == JFileChooser.APPROVE_OPTION) {
    file = filechooser.getSelectedFile();
    saveFile();
   }
  }
 }
 class close implements ActionListener { // 退出的监听
  public void actionPerformed(ActionEvent e) {
   int s = JOptionPane.showConfirmDialog(frame, "你真的要结束吗", "结束程序",
     JOptionPane.YES_NO_CANCEL_OPTION);
   if (s == JOptionPane.YES_OPTION)
    System.exit(0);
  }
 }
 class kaobei implements ActionListener { // 拷贝的监听
  public void actionPerformed(ActionEvent e) {
   area.copy();
  }
 }
 class niantie implements ActionListener { // 粘贴的监听
  public void actionPerformed(ActionEvent e) {
   area.paste();
  }
 }
 class jianqie implements ActionListener { // 剪切的监听
  public void actionPerformed(ActionEvent e) {
   area.cut();
  }
 }
 class adddate implements ActionListener { // 添加日期的监听
  public void actionPerformed(ActionEvent e) {
   Date d = new Date();
   area.append(d.toString());
  }
 }
 class About implements ActionListener { // 关于的监听
  public void actionPerformed(ActionEvent e) {
   JOptionPane.showMessageDialog(null, "制作人:沈刚  QQ:289972365",
     "关于本软件", JOptionPane.INFORMATION_MESSAGE);
  }
 }
 void openFile() { // 打开文件的方法
  try {
   FileReader fr = new FileReader(file);
   int len = (int) file.length();
   char[] buffer = new char[len];
   fr.read(buffer, 0, len);
   fr.close();
   area.setText(new String(buffer));
  } catch (Exception e) {
   e.printStackTrace();
  }
 }
 void saveFile() { // 保存文件方法
  try {
   FileWriter fw = new FileWriter(file);
   fw.write(area.getText());
   fw.close();
  } catch (Exception e) {
   e.printStackTrace();
  }
 }
 public NoteBook() {
  area.setLineWrap(true);
  JScrollPane jsp = new JScrollPane(area);
  JMenuBar jmb = new JMenuBar();
  JMenu mFile = new JMenu("文件");
  JMenu mEdit = new JMenu("编辑");
  JMenu mHelp = new JMenu("关于");
  JMenuItem mNew = new JMenuItem("新建");
  mNew.addActionListener(new xinjian());
  mFile.add(mNew);
  JMenuItem mOpen = new JMenuItem("打开");
  mOpen.addActionListener(new dakai());
  mFile.add(mOpen);
  JMenuItem mSave = new JMenuItem("保存");
  mSave.addActionListener(new baocun());
  mFile.add(mSave);
  mFile.addSeparator(); // 添加分割线
  JMenuItem mExit = new JMenuItem("退出");
  mExit.addActionListener(new close());
  mFile.add(mExit);
  mFile.setMnemonic(KeyEvent.VK_F);
  JMenuItem jmi;
  jmi = new JMenuItem("拷贝");
  jmi.addActionListener(new kaobei());
  mEdit.add(jmi);
  jmi = new JMenuItem("剪切");
  jmi.addActionListener(new jianqie());
  mEdit.add(jmi);
  jmi = new JMenuItem("粘贴");
  jmi.addActionListener(new niantie());
  mEdit.add(jmi);
  jmi = new JMenuItem("字体颜色");
  jmi.addActionListener(new color());
  mEdit.add(jmi);
  mEdit.addSeparator(); // 添加分割线
  jmi = new JMenuItem("插入当前日期");
  jmi.addActionListener(new adddate());
  mEdit.add(jmi);
  jmi = new JMenuItem("关于本软件");
  jmi.addActionListener(new About());
  mHelp.add(jmi);
  jmb.add(mFile);
  jmb.add(mEdit);
  jmb.add(mHelp);
  Image icon = Toolkit.getDefaultToolkit().getImage(
    "icon.gif");
  this.setJMenuBar(jmb);
  this.setTitle("刚刚记事本1.0");
  this.getContentPane().add(jsp);
  this.setSize(640, 480);
  this.setLocation(200, 150);
  this.setIconImage(icon);
  this.setVisible(true);
 }
 public static void main(String s[]) {
  String img = "logo.jpg";
  JDialog load = new JDialog();
  load.getContentPane().add(new JLabel(new ImageIcon(img)));
  load.setUndecorated(true);
  load.setAlwaysOnTop(true);
  load.pack();
  Dimension screen = Toolkit.getDefaultToolkit().getScreenSize();
  load.setLocation((screen.width - load.getWidth()) / 2,
    (screen.height - load.getHeight()) / 2);
  load.setVisible(true);
  try {
   Thread.sleep(5000);
  } catch (Exception e) {
   e.printStackTrace();
  }
  load.dispose();
  new NoteBook();
 }
}