JAVA 編寫的記事本


package abc;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Container;
import java.awt.FileDialog;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.GraphicsEnvironment;
import java.awt.GridBagConstraints;
import java.awt.GridLayout;
import java.awt.LayoutManager;
import java.awt.List;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.InputEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import java.awt.image.BufferedImage;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileDescriptor;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.PrintWriter;

import javax.swing.JButton;
import javax.swing.JColorChooser;
import javax.swing.JDialog;
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.JPopupMenu;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.KeyStroke;
import javax.swing.ScrollPaneConstants;
import javax.swing.JInternalFrame.JDesktopIcon;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;

import sun.security.jca.JCAUtil;

public class notepad extends JFrame {
    /**
* 
*/
    private static final long serialVersionUID = 1L;
    private JTextArea ja = new JTextArea(5, 6);
    String str;
    private String filePath;
    private String fileName;

    // 自動換行和狀態欄
    int a = 0;
    boolean flag = false;

    public jishiben() {

        super("記事本");
        this.setSize(768, 384);
        this.setLocationRelativeTo(null);

        // 菜單欄
        JMenu wj = new JMenu();
        wj.setText("文件");
        JMenu bj = new JMenu("編輯");
        JMenu gs = new JMenu("格式");
        JMenu ck = new JMenu("查看");
        JMenu bz = new JMenu("幫助");

        // 工具欄
        JMenuBar gjl = new JMenuBar();

        // 菜單項定義
        final JMenuItem open = new JMenuItem();
        open.setText("打開");
        JMenuItem New = new JMenuItem();
        New.setText("新建");
        JMenuItem save = new JMenuItem();
        save.setText("保存");
        final JMenuItem saveas = new JMenuItem();
        saveas.setText("另存為…");
        JMenuItem exit = new JMenuItem();
        exit.setText("退出");
        final JMenuItem copy = new JMenuItem();
        copy.setText("復制");
        final JMenuItem cut = new JMenuItem();
        cut.setText("剪切");
        JMenuItem paste = new JMenuItem();
        paste.setText("粘貼");
        final JMenuItem selectall = new JMenuItem();
        selectall.setText("全選");
        JMenuItem auto = new JMenuItem();
        auto.setText("自動換行");
        final JMenuItem font = new JMenuItem();
        font.setText("字體");
        JMenuItem about = new JMenuItem();
        about.setText("關於記事本…");
        JMenuItem ztl = new JMenuItem();
        ztl.setText("狀態欄");

        // 菜單項添加(文件菜單)
        wj.add(open);
        wj.add(New);
        wj.add(save);
        wj.add(saveas);
        wj.addSeparator();
        wj.add(exit);

        // 菜單項添加(編輯菜單)
        bj.add(cut);
        bj.add(copy);
        bj.add(paste);
        bj.addSeparator();
        bj.add(selectall);

        // 菜單項添加(格式菜單)
        gs.add(auto);
        gs.add(font);

        // 菜單項添加(查看菜單)
        ck.add(ztl);

        // 菜單項添加(幫助菜單)
        bz.add(about);

        // 把各個菜單添加到菜單欄
        gjl.add(wj);
        gjl.add(bj);
        gjl.add(gs);
        gjl.add(ck);
        gjl.add(bz);
        this.setJMenuBar(gjl);
        this.add(ja);

        // 字體
        JLabel lbfont = new JLabel("字體");
        JLabel lbsize = new JLabel("大小");
        JLabel lbshape = new JLabel("字形");
        JPanel jpfont1 = new JPanel();
        JPanel jpfont2 = new JPanel();
        JPanel jpfont3 = new JPanel();
        JPanel jpfont4 = new JPanel();
        final JFrame fontMain = new JFrame();
        final JDialog fontJd = new JDialog(fontMain, "字體,顏色,調色板", true);
        JButton ok = new JButton("確定");
        final JButton canel = new JButton("取消");
        JButton color = new JButton("顏色");
        final List fontList = new List(10, false);
        final List fontShape = new List(10, false);
        final List sizeList = new List(10, false);

        // 加系統字體
        String[] fontNames = GraphicsEnvironment.getLocalGraphicsEnvironment()
                .getAvailableFontFamilyNames();
        for (int i = 0; i < fontNames.length; i++) {
            fontList.add(fontNames[i]);
        }

        // 字體形狀
        fontShape.add("常規");
        fontShape.add("粗體");
        fontShape.add("斜體");
        fontShape.add("粗斜體");

        // 設置字體對話框
        fontJd.setSize(450, 300);
        jpfont1.add(lbfont);
        jpfont1.add(fontList);
        jpfont2.add(lbsize);
        jpfont2.add(lbshape);
        jpfont3.add(lbshape);
        jpfont3.add(fontShape);
        jpfont4.setLayout(new FlowLayout(FlowLayout.CENTER, 20, 10));
        jpfont4.add(ok);
        jpfont4.add(canel);
        jpfont4.add(color);

        fontJd.setLayout(new FlowLayout(FlowLayout.CENTER, 10, 10));
        fontJd.add(jpfont1);
        fontJd.add(jpfont2);
        fontJd.add(jpfont3);
        fontJd.add(jpfont4);

        fontList.select(a);
        sizeList.select(4);
        fontShape.select(0);

        // 字體大小
        final String[] size = { "1", "2", "3", "4", "5", "6", "7", "8", "9",
                "10" };
        for (int i = 0; i < size.length; i++) {
            sizeList.add(size[i]);
        }

        // 設置顏色按鈕監聽器
        color.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                JColorChooser jcolor = new JColorChooser();
                Color fcolor = ja.getForeground();
                ja.setForeground(jcolor.showDialog(ja, "選擇字體顏色", fcolor));
            }

        });

        // 取消按鈕監聽器
        canel.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                fontMain.setVisible(false);
            }

        });

        // 確定按鈕監聽器
        ok.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                fontMain.setVisible(false);
            }

        });

        // 字體監聽器
        font.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                fontJd.setLocationRelativeTo(jishiben.this);
                fontJd.setVisible(true);

                int style = 0;
                if (fontShape.getSelectedItem().equals("常規") == true) {
                    style = Font.PLAIN;
                }
                if (fontShape.getSelectedItem().equals("斜體") == true) {
                    style = Font.ITALIC;
                }
                if (fontShape.getSelectedItem().equals("粗體") == true) {
                    style = Font.BOLD;
                }
                if (fontShape.getSelectedItem().equals("粗斜體") == true) {
                    style = Font.BOLD + Font.ITALIC;
                }

                // 設置字體大小
                String[] size = { "1", "2", "3", "4", "5", "6", "7", "8", "9",
                        "10" };
                for (int i = 0; i < size.length; i++) {
                    if (sizeList.getSelectedItem().equals(size[i]) == true) {
                        ja.setFont(new Font(String.valueOf(fontList
                                .getSelectedItem()), style, (10 - i) * 10));
                        fontJd.dispose();
                    }
                }

            }
        });

        // 文件改變事件
        ja.getDocument().addDocumentListener(new DocumentListener() {

            public void changedUpdate(DocumentEvent e) {
                flag = true;
            }

            public void insertUpdate(DocumentEvent e) {
                flag = true;
            }

            public void removeUpdate(DocumentEvent e) {
                flag = true;
            }

        });

        // 窗口監聽器
        this.addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent w) {
                fileName = jishiben.this.getTitle();
                if (flag == true) {
                    int returnValue = JOptionPane.showConfirmDialog(null,
                            jishiben.this.getTitle() + "文件還沒有保存!需要保存?", "記事本",
                            JOptionPane.YES_NO_CANCEL_OPTION);
                    if (returnValue == JOptionPane.YES_OPTION) {
                        save_asMethod();

                    } else if (returnValue == JOptionPane.NO_OPTION) {
                        System.exit(0);

                    } else
                        setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);

                }
            }

            // 退出時用到的保存文件函數
            public void save_asMethod() {
                FileDialog fd = new FileDialog(jishiben.this, "另存為…",
                        FileDialog.SAVE);
                fd.setFile("*.txt");
                fd.setVisible(true);

                filePath = fd.getDirectory();
                fileName = fd.getFile();

                try {
                    FileWriter fw = new FileWriter(filePath + fileName);
                    BufferedWriter bw = new BufferedWriter(fw);
                    PrintWriter pw = new PrintWriter(bw);

                    pw.println(ja.getText());
                    pw.flush();
                    pw.close();
                } catch (Exception ex) {
                    ex.printStackTrace();
                }
            }
        });

        // 右鍵快捷方式
        final JPopupMenu jp = new JPopupMenu();
        final JMenuItem jcopy = new JMenuItem("復制");
        final JMenuItem jpaste = new JMenuItem("粘貼");
        final JMenuItem jcut = new JMenuItem("剪切");
        final JMenuItem jselectall = new JMenuItem("全選");
        final JMenuItem jfont = new JMenuItem("字體");

        jp.add(jcopy);
        jp.add(jpaste);
        jp.add(jcut);
        jp.addSeparator();
        jp.add(jselectall);
        jp.addSeparator();
        jp.add(jfont);

        // 右鍵菜單之字體的事件

        // 右鍵菜單之復制的事件
        jcopy.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                ja.copy();
            }

        });

        // 右鍵菜單之粘貼的事件
        jpaste.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                ja.paste();
            }

        });

        // 右鍵菜單之剪切的事件
        jcut.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                ja.cut();
            }

        });

        // 右鍵菜單之全選的事件
        jselectall.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                ja.selectAll();
            }

        });

        // 右鍵菜單之增加鼠標事件
        ja.addMouseListener(new MouseAdapter() {

            public void mouseReleased(MouseEvent e) {
                if (e.isPopupTrigger()) {
                    jp.show((Component) (e.getSource()), e.getX(), e.getY());

                    String temp = ja.getSelectedText();
                    if (temp != null) {
                        copy.setEnabled(true);
                        cut.setEnabled(true);
                        jcopy.setEnabled(true);
                        jcut.setEnabled(true);
                    } else if (temp == null) {
                        copy.setEnabled(false);
                        cut.setEnabled(false);
                        jcopy.setEnabled(false);
                        jcut.setEnabled(false);
                    }
                    String temp1 = ja.getText();
                    if (temp1 == null) {
                        selectall.setEnabled(false);
                        jselectall.setEnabled(false);
                    } else if (temp1 != null) {
                        selectall.setEnabled(true);
                        jselectall.setEnabled(true);
                    }
                }
            }
        });

        // 復制菜單項
        copy.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                ja.copy();
            }

        });

        // 粘貼菜單項
        paste.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {

                try {
                    ja.paste();
                } catch (Exception ex) {
                    ex.printStackTrace();
                }
            }

        });

        // 剪切菜單項
        cut.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                try {
                    ja.cut();
                } catch (Exception ex) {
                    ex.printStackTrace();
                }
            }

        });

        // 全選菜單項
        selectall.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                ja.selectAll();
            }

        });

        // 換行菜單項
        auto.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                if (a == 0) {
                    ja.setLineWrap(true);
                    a = 1;
                } else if (a == 1) {
                    ja.setLineWrap(false);
                    a = 0;
                }
            }

        });

        // 新建菜單項
        New.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                jishiben.this.setTitle("無標題——記事本.txt");
                ja.setText(" ");
            }

        });

        // 退出菜單項
        exit.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                fileName = jishiben.this.getTitle();
                if (flag == true) {
                    int returnValue = JOptionPane.showConfirmDialog(null,
                            jishiben.this.getTitle() + "文件還沒有保存!需要保存?", "記事本",
                            JOptionPane.YES_NO_CANCEL_OPTION);
                    if (returnValue == JOptionPane.YES_OPTION) {
                        save_asMethod();

                    } else if (returnValue == JOptionPane.NO_OPTION) {
                        System.exit(0);

                    } else
                        setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);

                }
            }

            public void save_asMethod() {
                FileDialog fd = new FileDialog(jishiben.this, "另存為…",
                        FileDialog.SAVE);
                fd.setFile("*.txt");
                fd.setVisible(true);

                filePath = fd.getDirectory();
                fileName = fd.getFile();

                try {
                    FileWriter fw = new FileWriter(filePath + fileName);
                    BufferedWriter bw = new BufferedWriter(fw);
                    PrintWriter pw = new PrintWriter(bw);

                    pw.println(ja.getText());
                    pw.flush();
                    pw.close();
                } catch (Exception ex) {
                    ex.printStackTrace();
                }
            }

        });

        // 保存菜單項
        save.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                fileName = jishiben.this.getTitle();
                try {
                    FileWriter fw = new FileWriter(fileName + ".txt");
                    String save = ja.getText();
                    fw.write(save);
                    fw.close();
                } catch (Exception ex) {
                    ex.printStackTrace();
                }
                JOptionPane.showMessageDialog(null, "文件已經成功保存了!", "文件保存",
                        JOptionPane.INFORMATION_MESSAGE);
            }

        });

        // 另存菜單項
        saveas.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent arg0) {
                FileDialog fd = new FileDialog(jishiben.this, "另存為…",
                        FileDialog.SAVE);
                fd.setFile("*.txt");
                fd.setVisible(true);

                filePath = fd.getDirectory();
                fileName = fd.getFile();

                try {
                    FileWriter fw = new FileWriter(filePath + fileName);
                    BufferedWriter bw = new BufferedWriter(fw);
                    PrintWriter pw = new PrintWriter(bw);

                    pw.println(ja.getText());
                    pw.flush();
                    pw.close();
                } catch (Exception ex) {
                    ex.printStackTrace();
                }
            }

        });

        // 打開菜單項
        open.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                FileDialog fd1 = new FileDialog(jishiben.this, "打開",
                        FileDialog.LOAD);
                fd1.setFile("*.txt");
                fd1.setVisible(true);

                fileName = fd1.getFile();
                filePath = fd1.getDirectory();
                jishiben.this.setTitle(fileName);
                try {
                    FileReader fr = new FileReader(filePath + fileName);
                    BufferedReader br = new BufferedReader(fr);
                    String sinput = "";
                    ja.setText("");
                    int lineNum = 0;
                    while ((sinput = br.readLine()) != null) {
                        // System.out.println(sinput);
                        ja.append(sinput + "\r\n");
                        lineNum++;
                    }
                } catch (Exception ex) {
                    ex.printStackTrace();
                }
            }

        });

        // 關於菜單項
        final JFrame jfm = new JFrame();
        JLabel lb1 = new JLabel("本記事本有zyw制作\n");
        JLabel lb2 = new JLabel("版權所有,盜版必究!\n");
        JLabel lb3 = new JLabel("謝謝使用!\n");

        jfm.setSize(200, 170);
        jfm.setTitle("關於本軟件");
        jfm.add(lb1);
        jfm.add(lb2);
        jfm.add(lb3);
        final JButton qd = new JButton("確定");
        qd.setSize(20, 17);
        jfm.add(qd);

        // 關於菜單項
        about.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                jfm.setLocationRelativeTo(jishiben.this);
                jfm.setVisible(true);
                jfm.setLayout(new GridLayout(4, 1, 5, 10));
                jfm.setLayout(new FlowLayout(FlowLayout.CENTER, 20, 10));

                qd.addActionListener(new ActionListener() {

                    public void actionPerformed(ActionEvent e) {
                        jfm.setVisible(false);
                    }

                });
            }

        });

        // 狀態欄菜單項
        final JLabel lb = new JLabel();
        ztl.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                if (a == 1) {
                    lb.setVisible(false);
                    a = 0;
                } else if (a == 0) {
                    jishiben.this.add(lb, BorderLayout.SOUTH);
                    lb.setVisible(true);
                    lb.setText("當前字數: "
                            + String.valueOf(ja.getText().trim().length())
                            + " " + "當前行數: "
                            + String.valueOf(ja.getLineCount()));
                    a = 1;
                }
            }

        });

        // 快捷鍵
        open.setAccelerator(KeyStroke.getKeyStroke('O', InputEvent.CTRL_MASK));
        New.setAccelerator(KeyStroke.getKeyStroke('N', InputEvent.CTRL_MASK));
        save.setAccelerator(KeyStroke.getKeyStroke('S', InputEvent.CTRL_MASK));
        exit.setAccelerator(KeyStroke.getKeyStroke('Q', InputEvent.CTRL_MASK));

        copy.setAccelerator(KeyStroke.getKeyStroke('C', InputEvent.CTRL_MASK));
        paste.setAccelerator(KeyStroke.getKeyStroke('V', InputEvent.CTRL_MASK));
        cut.setAccelerator(KeyStroke.getKeyStroke('X', InputEvent.CTRL_MASK));
        selectall.setAccelerator(KeyStroke.getKeyStroke('A',
                InputEvent.CTRL_MASK));

        auto.setAccelerator(KeyStroke.getKeyStroke('H', InputEvent.CTRL_MASK));
        font.setAccelerator(KeyStroke.getKeyStroke('F', InputEvent.ALT_MASK));

        // 添加滾動條
        JScrollPane jsp = new JScrollPane(ja);
        Container c = this.getContentPane();
        c.add(jsp);
        jsp.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED); // 需要的時候出現
        jsp.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS); // 總是存在

        this.setVisible(true);
    }

    private static String change(String s) {
        byte abyte0[] = s.getBytes();
        char ac[] = new char[s.length()];
        int i = 0;
        for (int k = abyte0.length; i < k; i++) {
            int j = abyte0[i];
            if (j >= 48 && j <= 57)
                j = ((j - 48) + 5) % 10 + 48;
            else if (j >= 65 && j <= 90)
                j = ((j - 65) + 13) % 26 + 65;
            else if (j >= 97 && j <= 122)
                j = ((j - 97) + 13) % 26 + 97;
            ac[i] = (char) j;
        }

        return String.valueOf(ac);
    }

    public static void main(String[] args) {

        jishiben jsb = new jishiben();
    }

}


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM