终于把马士兵的J2SE的视频了…前两天在学校用了一个下午写了一个很简易的Swing记事本,这也是自己独立写的一个小程序,大约500行,第一次写这么多…哈哈
先把源码弄出来
- import java.awt.BorderLayout;
- import java.awt.GridLayout;
- import java.awt.event.ActionEvent;
- import java.awt.event.ActionListener;
- import java.awt.event.WindowAdapter;
- import java.awt.event.WindowEvent;
- 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.io.PrintWriter;
- import javax.swing.JButton;
- import javax.swing.JFileChooser;
- import javax.swing.JFrame;
- import javax.swing.JLabel;
- import javax.swing.JMenu;
- import javax.swing.JMenuBar;
- import javax.swing.JMenuItem;
- import javax.swing.JOptionPane;
- import javax.swing.JPanel;
- import javax.swing.JScrollPane;
- import javax.swing.JTextArea;
- import javax.swing.JTextField;
- import javax.swing.undo.UndoManager;
- public class NotePad extends JFrame {
- private JMenuBar jmb = new JMenuBar();// 菜单栏容器
- private JMenu jm1 = new JMenu("文件");
- private JMenu jm2 = new JMenu("编辑");
- private JMenu jm3 = new JMenu("帮助");
- private JMenu jm4 = new JMenu("格式");
- private JMenuItem openItem = new JMenuItem("打开");
- private JMenuItem newItem = new JMenuItem("新建");
- private JMenuItem saveItem = new JMenuItem("保存");
- private JMenuItem otherSaveItem = new JMenuItem("另存为");
- private JMenuItem closeItem = new JMenuItem("退出");
- private JMenuItem reStepItem = new JMenuItem("撤销");
- private JMenuItem cutItem = new JMenuItem("剪切");
- private JMenuItem copyItem = new JMenuItem("复制");
- private JMenuItem casteItem = new JMenuItem("粘贴");
- private JMenuItem replaceItem = new JMenuItem("查找和替换");
- private JMenuItem allItem = new JMenuItem("全选");
- private JMenuItem helpItem = new JMenuItem("关于");
- private JMenuItem autoEnterItem = new JMenuItem("自动换行");
- private JTextArea jta = new JTextArea(10, 10);
- private JScrollPane jsp = new JScrollPane(jta);
- private boolean alOpen = false;// 判断文件是否是从外部读取的
- private boolean alSave = false;// 判断文件是否已经保存修改
- private String openPath = "";
- private int count = 0;// 控制查找时的第二次点击时的起始位置
- static UndoManager manager = new UndoManager();// UndoManager 管理
- // UndoableEdit
- // 列表,提供撤消或恢复适当编辑的方法
- public NotePad() {
- super("记事本");
- this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- this.addWindowListener(new WindowAdapter() {// 为窗口的关闭添加监听
- public void windowClosing(WindowEvent e) {
- otherSave();
- }
- });
- jm1.add(newItem);
- jm1.add(openItem);
- jm1.add(saveItem);
- jm1.add(otherSaveItem);
- jm1.addSeparator();
- jm1.add(closeItem);
- jm2.add(reStepItem);
- jm2.addSeparator();
- jm2.add(cutItem);
- jm2.add(copyItem);
- jm2.add(casteItem);
- jm2.addSeparator();
- jm2.add(replaceItem);
- jm2.addSeparator();
- jm2.add(allItem);
- jm3.add(helpItem);
- jm4.add(autoEnterItem);
- jmb.add(jm1);
- jmb.add(jm2);
- jmb.add(jm4);
- jmb.add(jm3);
- openItem.addActionListener(new ActionListener() {// 为打开事件添加监听器
- public void actionPerformed(ActionEvent e) {
- jta.setText("");
- alOpen = true;
- JFileChooser chooser = new JFileChooser();
- int returnVal = chooser.showOpenDialog(null);// 打开窗口没有父级窗口,可以随便移动
- File f = chooser.getSelectedFile();// 获得选择的文件
- if (returnVal == JFileChooser.APPROVE_OPTION) {
- openPath = f.getAbsolutePath();
- // 打开文件输入
- try {
- FileReader fr = new FileReader(f);
- BufferedReader in = new BufferedReader(fr);
- // ////缓存文件
- FileWriter fwCheck = new FileWriter("D:/$~check.txt");
- BufferedWriter out = new BufferedWriter(fwCheck);
- PrintWriter pw = new PrintWriter(fwCheck);// 文本的复制使用这个
- // ////缓存文件
- String s = "";
- while ((s = in.readLine()) != null) {
- jta.append(s + "/n");
- pw.println(s);
- }
- out.flush();// 刷新缓冲区
- fr.close();
- fwCheck.close();
- } catch (Exception e1) {
- e1.printStackTrace();
- }
- }
- }
- });
- closeItem.addActionListener(new ActionListener() {// 为关闭按钮添加监听器
- public void actionPerformed(ActionEvent e) {
- otherSave();
- }
- });
- newItem.addActionListener(new ActionListener() {// 为新建按钮添加监听器,两种情况,一是有内容时点新建,二是无内容时点新建
- public void actionPerformed(ActionEvent e) {
- if (jta.getText().equals("")) {// 无内容时,两种可能,一是本来就没有内容,二是打开东西后手工删除的,这时需要询问是否保存
- // 1.
- if (alOpen == false) {
- jta.setText("");
- } else {// 2.
- File fileCheck = new File("D:/$~check.txt");
- int statVal = JOptionPane.showConfirmDialog(null,
- "是否保存文件?");
- if (statVal == JOptionPane.YES_OPTION) {
- // 添加保存文件对话框,同时删除缓存文件
- fileCheck.delete();
- JFileChooser saveChooser = new JFileChooser();
- int flagVal = saveChooser.showSaveDialog(null);
- if (flagVal == saveChooser.APPROVE_OPTION) {
- String filePath = "";
- filePath = saveChooser.getSelectedFile()
- .getAbsolutePath();
- File newF = new File(filePath);
- try {
- FileWriter fw = new FileWriter(newF);
- fw.write("");
- fw.close();
- } catch (Exception ex) {
- ex.printStackTrace();
- }
- }
- }
- if (statVal == JOptionPane.NO_OPTION) {
- fileCheck.delete();
- jta.setText("");
- }
- }
- } else {// 有内容时点击新建
- File fileCheck = new File("D:/$~check.txt");
- String jtaStr = jta.getText();
- String checkStr = "";
- String s = "";
- try {
- FileReader fr = new FileReader(fileCheck);
- BufferedReader in = new BufferedReader(fr);
- while ((s = in.readLine()) != null) {
- checkStr = checkStr + s + "/n";
- }
- fr.close();
- } catch (Exception ex) {
- ex.printStackTrace();
- }
- if (checkStr.equals(jta.getText())) {// 如果没有改动
- jta.setText("");
- } else {// 如果改动
- int statVal = JOptionPane.showConfirmDialog(null,
- "是否保存文件?");
- if (statVal == JOptionPane.YES_OPTION) {
- // 添加保存文件对话框
- fileCheck.delete();
- JFileChooser saveChooser = new JFileChooser();
- int flagVal = saveChooser.showSaveDialog(null);
- if (flagVal == saveChooser.APPROVE_OPTION) {
- String filePath = "";
- filePath = saveChooser.getSelectedFile()
- .getAbsolutePath();
- File newF = new File(filePath);
- try {// *
- FileWriter fw = new FileWriter(newF);
- BufferedWriter bw = new BufferedWriter(fw);
- String mainText = jta.getText();
- bw.write(mainText);
- bw.flush();
- bw.close();
- fw.close();
- } catch (Exception ex) {
- ex.printStackTrace();
- }
- }
- }
- if (statVal == JOptionPane.NO_OPTION) {
- fileCheck.delete();
- jta.setText("");
- }
- }
- }
- }
- });
- saveItem.addActionListener(new ActionListener() {// 为保存按钮添加监听器
- public void actionPerformed(ActionEvent e) {
- // 保存直接是覆盖源文件即可
- alSave = true;
- File saveFile = new File(openPath);
- String mainText = jta.getText();
- try {
- FileWriter fw = new FileWriter(saveFile);
- fw.write(mainText);
- fw.flush();
- fw.close();
- } catch (Exception ex) {
- ex.printStackTrace();
- }
- }
- });
- otherSaveItem.addActionListener(new ActionListener() {// 为另存为按钮添加监听器
- public void actionPerformed(ActionEvent e) {
- JFileChooser otherChooser = new JFileChooser();
- int flagVal = otherChooser.showSaveDialog(null);
- if (flagVal == JFileChooser.APPROVE_OPTION) {
- String mainText = jta.getText();
- String filePath = otherChooser.getSelectedFile()
- .getAbsolutePath();
- File saveFile = new File(filePath);
- try {
- FileWriter fw = new FileWriter(saveFile);
- fw.write(mainText);
- fw.flush();
- fw.close();
- } catch (Exception ex) {
- ex.printStackTrace();
- }
- }
- }
- });
- cutItem.addActionListener(new ActionListener() {// 剪切操作
- public void actionPerformed(ActionEvent e) {
- jta.cut();
- }
- });
- copyItem.addActionListener(new ActionListener() {// 复制操作
- public void actionPerformed(ActionEvent e) {
- jta.copy();
- }
- });
- casteItem.addActionListener(new ActionListener() {// 粘贴操作
- public void actionPerformed(ActionEvent e) {
- jta.paste();
- }
- });
- autoEnterItem.addActionListener(new ActionListener() {// 自动换行
- public void actionPerformed(ActionEvent e) {
- if (jta.getLineWrap() == true) {// 如果获取已经换行
- jta.setLineWrap(false);// 不设置换行
- } else {
- jta.setLineWrap(true);
- }
- }
- });
- helpItem.addActionListener(new ActionListener() {// 为帮助主题按钮添加监听器
- public void actionPerformed(ActionEvent e) {
- JFrame jf = new JFrame("帮助主题");
- JLabel jl = new JLabel(" 记事本V0.1 薛琪 2011-4-26 ");
- jf.add(jl, BorderLayout.CENTER);
- jf.setBounds(80, 80, 250, 140);
- jf.setVisible(true);
- }
- });
- jta.getDocument().addUndoableEditListener(manager);// CSDN搜的,为文本框里文本注册监听器,因为操作对象是文本
- reStepItem.addActionListener(new ActionListener() {// 撤销
- public void actionPerformed(ActionEvent e) {
- if (manager.canUndo()) {// 如果可以撤销
- manager.undo();
- }
- }
- });
- allItem.addActionListener(new ActionListener() {// 全选
- public void actionPerformed(ActionEvent e) {
- jta.selectAll();
- }
- });
- replaceItem.addActionListener(new ActionListener() {// 查找和替换
- public void actionPerformed(ActionEvent e) {
- final JFrame f = new JFrame("查找和替换");
- JPanel p1 = new JPanel();
- JPanel p2 = new JPanel();
- final JTextField jtf1 = new JTextField(20);
- final JTextField jtf2 = new JTextField(20);
- JLabel jl1 = new JLabel("查找内容(N):");
- JLabel jl2 = new JLabel("替换为(P):");
- JButton b1 = new JButton("查找下一个");
- JButton b2 = new JButton("替换");
- JButton b3 = new JButton("全部替换");
- JButton b4 = new JButton("取消");
- p1.setLayout(new GridLayout(2, 2));
- // p2.setLayout(new GridLayout(2,2));
- p1.add(jl1);
- p1.add(jtf1);
- p1.add(jl2);
- p1.add(jtf2);
- p2.add(b1);
- p2.add(b2);
- p2.add(b3);
- p2.add(b4);
- f.add(p1, BorderLayout.NORTH);
- f.add(p2, BorderLayout.CENTER);
- f.pack();
- f.setVisible(true);
- b4.addActionListener(new ActionListener() {// 取消按钮
- public void actionPerformed(ActionEvent e) {
- f.setVisible(false);
- count = 0;
- }
- });
- b3.addActionListener(new ActionListener() {// 全部替换
- public void actionPerformed(ActionEvent e) {
- String s1 = jtf1.getText();
- String s2 = jtf2.getText();
- String mainText = jta.getText();
- if (mainText.indexOf(s1) != -1) {
- String mainText1 = mainText.replace(s1, s2);
- jta.setText(mainText1);
- JOptionPane.showMessageDialog(null,
- "全部替换已成功");
- }
- }
- });
- b2.addActionListener(new ActionListener() {// 替换
- public void actionPerformed(ActionEvent e) {
- String s1 = jtf1.getText();
- String s2 = jtf2.getText();
- String mainText = jta.getText();
- int c = mainText.indexOf(s1);
- if (c != -1) {
- String mainText1 = mainText.replaceFirst(
- s1, s2);
- jta.setText(mainText1);
- }
- }
- });
- b1.addActionListener(new ActionListener() {// 查找下一个
- public void actionPerformed(ActionEvent e) {
- String s1 = jtf1.getText();
- String mainText = jta.getText();
- int start = mainText.indexOf(s1, count);
- int replaceLength = s1.length();
- int end = start + replaceLength;
- jta.select(start, end);
- count = count + end;
- }
- });
- }
- });
- this.setJMenuBar(jmb);
- this.add(jsp, BorderLayout.CENTER);
- this.setSize(800, 600);
- this.setVisible(true);
- }
- // 为关闭加方法,精简代码
- public void otherSave() {
- // 1.已经打开文件 判断文件是否修改,使用缓存文件
- if (alOpen) {
- File fileCheck = new File("D:/$~check.txt");
- String jtaStr = jta.getText();
- String checkStr = "";
- String s = "";
- try {
- FileReader fr = new FileReader(fileCheck);
- BufferedReader in = new BufferedReader(fr);
- while ((s = in.readLine()) != null) {
- checkStr = checkStr + s + "/n";
- }
- // System.out.println(checkStr);
- fr.close();
- if (jtaStr.equals(checkStr)) {
- int statVal = JOptionPane.showConfirmDialog(null, "确定关闭");
- if (statVal == JOptionPane.YES_OPTION) {
- fileCheck.delete();// 要删除文件,必须先关闭文件
- System.exit(0);
- }
- } else {// 文件有改动
- int statVal = JOptionPane
- .showConfirmDialog(null, "是否保存文件?");
- if (statVal == JOptionPane.YES_OPTION) {
- // 添加保存文件对话框,关闭是另存为
- JFileChooser saveChooser = new JFileChooser();
- int flagVal = saveChooser.showSaveDialog(null);
- if (flagVal == saveChooser.APPROVE_OPTION) {
- String filePath = "";
- filePath = saveChooser.getSelectedFile()
- .getAbsolutePath();
- // System.out.println(filePath);
- File newF = new File(filePath);
- FileWriter fw = new FileWriter(newF);
- BufferedWriter bw = new BufferedWriter(fw);
- String mainText = jta.getText();
- bw.write(mainText);
- bw.flush();
- fw.close();
- bw.close();
- fileCheck.delete();
- System.exit(0);
- }
- }
- if (statVal == JOptionPane.NO_OPTION) {
- fileCheck.delete();
- System.exit(0);
- }
- }
- } catch (Exception e1) {
- e1.printStackTrace();
- }
- } else {
- if (jta.getText().equals("")) {
- int statVal = JOptionPane.showConfirmDialog(null, "确定关闭");
- if (statVal == JOptionPane.YES_OPTION) {
- System.exit(0);
- }
- } else {
- int statVal = JOptionPane.showConfirmDialog(null, "是否保存文件?");
- if (statVal == JOptionPane.YES_OPTION) {
- // 添加保存文件对话框,关闭是另存为
- JFileChooser saveChooser = new JFileChooser();
- int flagVal = saveChooser.showSaveDialog(null);
- if (flagVal == saveChooser.APPROVE_OPTION) {
- String filePath = "";
- filePath = saveChooser.getSelectedFile()
- .getAbsolutePath();
- File newF = new File(filePath);
- FileWriter fw;
- try {
- fw = new FileWriter(newF);
- BufferedWriter bw = new BufferedWriter(fw);
- String mainText = jta.getText();
- bw.write(mainText);
- bw.flush();
- fw.close();
- bw.close();
- System.exit(0);
- } catch (IOException e1) {
- e1.printStackTrace();
- }
- }
- }
- }
- }
- }
- public static void main(String[] args) {
- // JFrame.setDefaultLookAndFeelDecorated(true);
- new NotePad();
- }
- }
实现了打开,关闭,保存,复制,粘贴,撤销,剪切,查找,复制和替换,全部替换,自动换行…主要是练习了Swing和事件监听…