java记事本

package com.scan;

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

public class Window extends JFrame implements ActionListener {
 /**
  *
  */
 public static void main(String[] args) {
  
  
  Window w=new Window("test");
  

  
  
 }

 private static final long serialVersionUID = -2739406712124680419L;
 JMenuBar menubar;
 JPopupMenu menu;// 创建弹出式菜单对象
 JMenu menu1, menu2, menu3, menu4, menu5, menu6;
 JToolBar toolbar1, toolbar2;
 JButton button1, button2, button3, button4, button5, button6;
 JMenuItem item1, item2, item3, item4, item5, item6, item7, item8, item9, item11, item12, item13, item14, item15,
   item16, item17, item18, item19;
 JTextArea text;
 JFileChooser fileChooser;
 JTextField text1, text2, text3, text4;
 JComboBox listFont, listSize;
 JLabel label, label1;
 JCheckBox box;

 Window(String s) {
  setTitle(s);
  // 实例化复选框
  box = new JCheckBox("自动换行");
  // 实例化文件对话框
  fileChooser = new JFileChooser();
  // 实例化菜单栏
  menubar = new JMenuBar();
  // 实例化工具栏
  toolbar1 = new JToolBar();
  // 实例化状态栏
  toolbar2 = new JToolBar();
  // 加载工具栏
  add(toolbar1, BorderLayout.NORTH);
  // 加载状态栏
  add(toolbar2, BorderLayout.SOUTH);
  // 实例化标签
  label = new JLabel("字体设置");
  label1 = new JLabel("字号");
  // 实例化工具栏按钮
  button1 = new JButton(new ImageIcon("ico\\new.gif"));
  button2 = new JButton(new ImageIcon("ico\\open.gif"));
  button3 = new JButton(new ImageIcon("ico\\save.gif"));
  button4 = new JButton(new ImageIcon("ico\\copy.gif"));
  button5 = new JButton(new ImageIcon("ico\\paste.gif"));
  button6 = new JButton(new ImageIcon("ico\\cut.gif"));
  // 设置工具栏大小
  toolbar1.setPreferredSize(new Dimension(800, 30));
  toolbar2.setPreferredSize(new Dimension(800, 25));
  // 设置按钮大小
  button1.setMaximumSize(new Dimension(40, 30));
  button2.setMaximumSize(new Dimension(40, 30));
  button3.setMaximumSize(new Dimension(40, 30));
  button4.setMaximumSize(new Dimension(40, 30));
  button5.setMaximumSize(new Dimension(40, 30));
  button6.setMaximumSize(new Dimension(40, 30));
  // 添加按钮
  toolbar1.add(button1);
  toolbar1.add(button2);
  toolbar1.add(button3);
  toolbar1.addSeparator();
  toolbar1.add(button4);
  toolbar1.add(button5);
  toolbar1.add(button6);
  toolbar1.addSeparator();
  // 添加标签
  toolbar1.add(label);
  // 字体设置下拉列表
  GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
  String fontName[] = ge.getAvailableFontFamilyNames();
  // 添加字体下拉列表
  listFont = new JComboBox(fontName);
  toolbar1.add(listFont);
  // 添加标签
  toolbar1.addSeparator();
  toolbar1.add(label1);
  // 添加字号下拉类表
  String[] size = { "8", "9", "10", "12", "14", "15", "16", "18", "20", "21", "22", "24", "26", "28", "30", "36",
    "48", "54", "72", "89" };
  listSize = new JComboBox(size);
  toolbar1.add(listSize);
  // 加载复选框
  toolbar1.addSeparator();
  toolbar1.add(box);
  // 实例化菜单
  menu1 = new JMenu("文件");
  menu2 = new JMenu("编辑");
  menu3 = new JMenu("页面设置");
  menu4 = new JMenu("颜色设置");
  menu5 = new JMenu("字体设置");
  menu6 = new JMenu("关于");
  // 实例化菜单选项
  item1 = new JMenuItem("新建");
  item2 = new JMenuItem("打开");
  item3 = new JMenuItem("保存");
  item4 = new JMenuItem("退出");
  item5 = new JMenuItem("复制");
  item6 = new JMenuItem("粘贴");
  item7 = new JMenuItem("剪切");
  item9 = new JMenuItem("关于记事本");
  // 实例化子菜单
  item8 = new JMenuItem("宋体");
  item17 = new JMenuItem("黑体");
  item18 = new JMenuItem("楷体");
  item19 = new JMenuItem("隶书");
  item15 = new JMenuItem("背景颜色设置");
  item16 = new JMenuItem("字体颜色设置");
  // 实例化弹出式菜单选项
  item11 = new JMenuItem("复制");
  item12 = new JMenuItem("粘贴");
  item13 = new JMenuItem("剪切");
  item14 = new JMenuItem("全选");
  // 加载菜单栏
  setJMenuBar(menubar);
  // 菜单选项及快捷键
  menu1.add(item1);
  item1.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_N, InputEvent.CTRL_MASK));
  menu1.add(item2);
  item2.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_O, InputEvent.CTRL_MASK));
  menu1.addSeparator();// 添加分割线
  menu1.add(item3);
  item3.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_S, InputEvent.CTRL_MASK));
  menu1.add(item4);
  item4.setAccelerator(KeyStroke.getKeyStroke("X"));
  menu2.addSeparator(); // 添加分割线
  menu2.add(item5);
  item5.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_C, InputEvent.CTRL_MASK));
  menu2.add(item6);
  item6.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_V, InputEvent.CTRL_MASK));
  menu2.addSeparator(); // 添加分割线
  menu2.add(item7);
  item7.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_T, InputEvent.CTRL_MASK));
  // 页面设置菜单及子菜单
  menu3.add(menu4);
  menu3.add(menu5);
  menu4.add(item15);
  menu4.add(item16);
  menu5.add(item8);
  menu5.add(item17);
  menu5.add(item18);
  menu5.add(item19);
  menu6.add(item9);
  // 添加菜单
  menubar.add(menu1);
  menubar.add(menu2);
  menubar.add(menu3);
  menubar.add(menu6);
  // 建立文本区域
  text = new JTextArea();
  add(text);
  text.setText("");
  // 加入滚动条
  add(new JScrollPane(text));
  // 实例化文本框
  text1 = new JTextField("就绪");
  text2 = new JTextField("欢迎使用该记事本!!");
  text3 = new JTextField();
  text4 = new JTextField();
  // 状态栏中加载文本框
  toolbar2.add(text1);
  toolbar2.add(text2);
  toolbar2.add(text3);
  toolbar2.add(text4);
  // 文本框为不可编辑状态
  text1.setEditable(false);
  text2.setEditable(false);
  text3.setEditable(false);
  text4.setEditable(false);
  // 弹出式菜单及快捷键
  menu = new JPopupMenu();
  menu.add(item11);
  item11.setAccelerator(KeyStroke.getKeyStroke("C"));
  menu.add(item12);
  item12.setAccelerator(KeyStroke.getKeyStroke("V"));
  menu.add(item13);
  item13.setAccelerator(KeyStroke.getKeyStroke("T"));
  menu.addSeparator(); // 添加分割线
  menu.add(item14);
  item14.setAccelerator(KeyStroke.getKeyStroke("A"));
  // 鼠标右键时间(弹出菜单)
  text.addMouseListener(new MouseAdapter() {
   public void mousePressed(MouseEvent e) {
    if (e.getModifiers() == InputEvent.BUTTON3_MASK)
     menu.show(text, e.getX(), e.getY());
   }
  });
  // 菜单注册监听器
  item1.addActionListener(this);
  item2.addActionListener(this);
  item3.addActionListener(this);
  item4.addActionListener(this);
  item5.addActionListener(this);
  item6.addActionListener(this);
  item7.addActionListener(this);
  item8.addActionListener(this);
  item9.addActionListener(this);
  item17.addActionListener(this);
  item18.addActionListener(this);
  item19.addActionListener(this);
  item15.addActionListener(this);
  item16.addActionListener(this);
  item11.addActionListener(this);
  item12.addActionListener(this);
  item13.addActionListener(this);
  item14.addActionListener(this);
  // 按钮注册监听器
  button1.addActionListener(this);
  button2.addActionListener(this);
  button3.addActionListener(this);
  button4.addActionListener(this);
  button5.addActionListener(this);
  button6.addActionListener(this);
  // 字体和字号下拉列表事件响应
  listFont.addItemListener(new ItemListener() {
   public void itemStateChanged(ItemEvent e) {
    String name = (String) listFont.getSelectedItem();
    Font f = new Font(name, Font.BOLD, text.getFont().getSize());
    text.setFont(f);
   }
  });
  listSize.addItemListener(new ItemListener() {
   public void itemStateChanged(ItemEvent e) {
    int fontsize = (new Integer((String) e.getItem()).intValue());
    Font f = new Font(text.getFont().getName(), text.getFont().getStyle(), fontsize);
    text.setFont(f);
   }
  });
  // 自动换行事件响应
  box.addItemListener(new ItemListener() {
   public void itemStateChanged(ItemEvent e) {
    if (box.isSelected())
     text.setLineWrap(true);
    else
     text.setLineWrap(false);
   }
  });
  // 窗体设置
  setSize(800, 550);
  setLocation(120, 120);
  setVisible(true);
  validate();
  setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
  // 窗体事件响应
  addWindowListener(new WindowAdapter() {
   public void windowClosing(WindowEvent e) {
    closefile();
   }
  });
 }

 // 事件响应
 public void actionPerformed(ActionEvent e) {
  // 复制
  if (e.getSource() == item11 || e.getSource() == item5 || e.getSource() == button4)
   text.copy();
  // 粘贴
  else if (e.getSource() == item12 || e.getSource() == item6 || e.getSource() == button5)
   text.paste();
  // 剪切
  else if (e.getSource() == item13 || e.getSource() == item7 || e.getSource() == button6)
   text.cut();
  // 退出
  else if (e.getSource() == item4) {
   if (text.getText().equals("")) {
    System.exit(0);
   } else {
    int n = JOptionPane.showConfirmDialog(this, "无标题记事本已改变。\n想要保存吗?", "记事本",
      JOptionPane.YES_NO_CANCEL_OPTION);
    if (n == JOptionPane.NO_OPTION)
     System.exit(0);
    else if (n == JOptionPane.YES_OPTION) {
     int m = fileChooser.showSaveDialog(null);
     if (m == JFileChooser.APPROVE_OPTION) {
      File file = fileChooser.getSelectedFile();
      // 获取文件名
      fileChooser.getName(file);
      try {
       FileWriter out = new FileWriter(file);
       out.write(text.getText());
       out.close();
      } catch (IOException ee) {
      }
     }
     System.exit(0);
    }
   }
  }
  // 新建
  else if (e.getSource() == item1) {
   if (text.getText().equals("")) {
    text.setText("");
   } else {
    int n = JOptionPane.showConfirmDialog(this, "无标题记事本已改变。\n想要保存吗?", "记事本",
      JOptionPane.YES_NO_CANCEL_OPTION);
    if (n == JOptionPane.NO_OPTION) {
     text.setText("");
     setTitle("无标题" + "——" + "记事本");
    } else if (n == JOptionPane.YES_OPTION) {
     fileChooser.showSaveDialog(null);
     if (n == JFileChooser.APPROVE_OPTION) {
      File file = fileChooser.getSelectedFile();
      fileChooser.getName(file);
      try {
       FileWriter out = new FileWriter(file);
       out.write(text.getText());
       out.close();
      } catch (IOException ee) {
      }
      text.setText("");
     }
    }
   }
  }
  // 打开
  else if (e.getSource() == item2) {
   int n = fileChooser.showOpenDialog(null);
   if (n == JFileChooser.APPROVE_OPTION) {
    File file = fileChooser.getSelectedFile();
    try {
     setTitle(fileChooser.getName(file) + "——" + "记事本");
     FileReader readfile = new FileReader(file);
     BufferedReader in = new BufferedReader(readfile);
     String s = null;
     while ((s = in.readLine()) != null)
      text.append(s + "\n");
     readfile.close();
    } catch (IOException ee) {
    }
   }
  }
  // 保存
  else if (e.getSource() == item3) {
   if (text.getText().equals("")) {
    text.setText("");
   } else {
    int n = fileChooser.showSaveDialog(null);
    if (n == JFileChooser.APPROVE_OPTION) {
     File file = fileChooser.getSelectedFile();
     // 获取文件名
     fileChooser.getName(file);
     try {
      setTitle(fileChooser.getName(file) + "——" + "记事本");
      FileWriter out = new FileWriter(file);
      out.write(text.getText());
      out.close();
     } catch (IOException ee) {
     }
    }
   }
  }
  // 字体设置
  else if (e.getSource() == item8) {
   Font font = new Font("宋体", Font.BOLD, getFont().getSize());
   text.setFont(font);
  } else if (e.getSource() == item17) {
   Font font = new Font("黑体", Font.ITALIC, getFont().getSize());
   text.setFont(font);
  } else if (e.getSource() == item18) {
   Font font = new Font("楷体", Font.PLAIN, getFont().getSize());
   text.setFont(font);
  } else if (e.getSource() == item19) {
   Font font = new Font("隶书", Font.TYPE1_FONT, getFont().getSize());
   text.setFont(font);
  }
  // 背景颜色设置
  else if (e.getSource() == item15) {
   Color color = JColorChooser.showDialog(this, "调色板", text.getBackground());
   if (color != null)
    text.setBackground(color);
  }
  // 字体颜色设置
  else if (e.getSource() == item16) {
   Color color = JColorChooser.showDialog(this, "调色板", text.getForeground());
   if (color != null) {
    text.setForeground(color);
   }
  }
  // 全选
  else if (e.getSource() == item14) {
   text.selectAll();
  }
  // 新建按钮
  else if (e.getSource() == button1) {
   if (text.getText().equals("")) {
    text.setText("");
   } else {
    int n = JOptionPane.showConfirmDialog(this, "无标题记事本已改变。\n想要保存吗?", "记事本",
      JOptionPane.YES_NO_CANCEL_OPTION);
    if (n == JOptionPane.NO_OPTION) {
     setTitle("无标题" + "——" + "记事本");
     text.setText("");
    } else if (n == JOptionPane.YES_OPTION) {
     fileChooser.showSaveDialog(null);
     if (n == JFileChooser.APPROVE_OPTION) {
      File file = fileChooser.getSelectedFile();
      fileChooser.getName(file);
      try {
       FileWriter out = new FileWriter(file);
       out.write(text.getText());
       out.close();
      } catch (IOException ee) {
      }
      text.setText("");
     }
    }
   }
  }
  // 打开按钮
  else if (e.getSource() == button2) {
   int n = fileChooser.showOpenDialog(null);
   if (n == JFileChooser.APPROVE_OPTION) {
    File file = fileChooser.getSelectedFile();
    try {
     setTitle(fileChooser.getName(file) + "——" + "记事本");
     FileReader readfile = new FileReader(file);
     BufferedReader in = new BufferedReader(readfile);
     String s = null;
     while ((s = in.readLine()) != null)
      text.append(s + "\n");
     readfile.close();
    } catch (IOException ee) {
    }
   }

  }
  // 保存按钮
  else if (e.getSource() == button3) {
   if (text.getText().equals("")) {
    text.setText("");
   } else {
    int n = fileChooser.showSaveDialog(null);
    if (n == JFileChooser.APPROVE_OPTION) {
     File file = fileChooser.getSelectedFile();
     // 获取文件名
     fileChooser.getName(file);
     try {
      setTitle(fileChooser.getName(file) + "——" + "记事本");
      FileWriter out = new FileWriter(file);
      out.write(text.getText());
      out.close();
     } catch (IOException ee) {
     }
    }
   }
  } else if (e.getSource() == item9) {
   JOptionPane.showMessageDialog(this,
     "这个是简单的记事本,但又不同于系统自带的记事本。\n" + "完全是自己写的,因为是初习者,没有用太多的方法。\n但是我相信这是很多初学者的实用的,简单易懂。\n"
       + "所有版权归程世强所有,最终解释武汉科技学院。\n联系方式:QQ:61289581  420721597\n" + "电话:15927162152 ",
     "关于记事本", JOptionPane.INFORMATION_MESSAGE);
  }
 }

 // 关闭窗体是调用的方法
 public void closefile() {
  if (text.getText().equals("")) {
   System.exit(0);
  } else {
   int n = JOptionPane.showConfirmDialog(this, "无标题记事本已改变。\n想要保存吗?", "记事本", JOptionPane.YES_NO_CANCEL_OPTION);
   if (n == JOptionPane.NO_OPTION)
    System.exit(0);
   else if (n == JOptionPane.YES_OPTION) {
    fileChooser.showSaveDialog(null);
    if (n == JFileChooser.APPROVE_OPTION) {
     File file = fileChooser.getSelectedFile();
     fileChooser.getName(file);
     try {
      FileWriter out = new FileWriter(file);
      out.write(text.getText());
      out.close();
     } catch (IOException ee) {
     }
     System.exit(0);
    }
   } else if (n == JOptionPane.CANCEL_OPTION)
    setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
  }

 }
}

  • 3
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值