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

📄 main.java

📁 本程序不仅实现了记事本基本的功能
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
    }    private void copy() {        String str = text.getSelectedText();        clipboard.setData(str);    }    private void paste() {        String str = clipboard.getData();        text.replaceRange(str, text.getSelectionStart(), text.getSelectionEnd());    }    private void delete() {        text.replaceRange("", text.getSelectionStart(), text.getSelectionEnd());    }    public void actionPerformed(ActionEvent e) {        try {            if (e.getSource() == newfile || e.getSource() == newBn) {                newFile();            }            if (e.getSource() == openfile || e.getSource() == openBn) {                openFile();            }            if (e.getSource() == savefile || e.getSource() == saveBn) {                saveFile();            }            if (e.getSource() == saveas || e.getSource() == saveasBn) {                saveAs();            }            if (e.getSource() == exit || e.getSource() == exitBn) {                exit();            }            if (e.getSource() == undoItem || e.getSource() == undoBn || e.getSource() == popundoItem) {                if (Un_Re.canUndo()) {                    Un_Re.undo();                    redoItem.setEnabled(true);                }            }            if (e.getSource() == redoItem || e.getSource() == redoBn || e.getSource() == popredoItem) {                if (Un_Re.canRedo()) {                    Un_Re.redo();                    undoItem.setEnabled(true);                } else {                    redoItem.setEnabled(false);                }            }            if (e.getSource() == cutItem || e.getSource() == cutBn || e.getSource() == popcutItem) {                cut();            }            if (e.getSource() == copyItem || e.getSource() == copyBn || e.getSource() == popcopyItem) {                copy();            }            if (e.getSource() == pasteItem || e.getSource() == pasteBn || e.getSource() == poppasteItem) {                paste();            }            if (e.getSource() == deleteItem || e.getSource() == popdeleteItem) {                delete();            }            if (e.getSource() == selectall || e.getSource() == popselectall) {                text.setSelectionStart(0);                text.setSelectionEnd(text.getText().length());            }            if (e.getSource() == time_date) {                Date d = new Date();                String str = DateFormat.getDateTimeInstance().format(d);                text.replaceRange(str, text.getSelectionStart(), text.getSelectionStart());            }            if (e.getSource() == find) {                new JFindDialog(new Frame(), text);            }            if (e.getSource() == replace) {                new JReplaceDialog(new Frame(), text);            }            if (e.getSource() == font) {                Frame frame = new Frame();                frame.setBounds(getX() + 30, getY() + 80, 250, 200);                text.setFont((new FontDialog(frame, "字体选择", true)).showFontDialog());                newContent = text.getText();            }            if (e.getSource() == musicImport) {                music.openFile();                addMusicfile(music.file);            }            if (e.getSource() == mode[0]) {                music.int_mode = 0;            }            if (e.getSource() == mode[1]) {                music.int_mode = 1;            }            if (e.getSource() == mode[2]) {                music.int_mode = 2;            }            if (e.getSource() == bkcolor) {                Color rsltColor = JColorChooser.showDialog(this,                        "颜色选择",                        this.getBackground());//arg3 是默认颜色                text.setBackground(rsltColor);            }            if (e.getSource() == info) {                Frame frame = new Frame();                frame.setBounds(getX() + 30, getY() + 80, 330, 200);                new Information(frame);            }        } catch (IOException ex) {            Logger.getLogger(JNotepad.class.getName()).log(Level.SEVERE, null, ex);        }    }    private void addToolBar() {        toolbar.setFloatable(true);        this.add(toolbar, BorderLayout.NORTH);        toolbar.add(newBn);        newBn.setToolTipText("新建");        newBn.addActionListener(this);        toolbar.add(openBn);        openBn.setToolTipText("打开");        openBn.addActionListener(this);        toolbar.add(saveBn);        saveBn.setToolTipText("保存");        saveBn.addActionListener(this);        toolbar.add(saveasBn);        saveasBn.setToolTipText("另存为");        saveasBn.addActionListener(this);        toolbar.add(exitBn);        exitBn.setToolTipText("退出");        exitBn.addActionListener(this);        toolbar.add(cutBn);        cutBn.setToolTipText("剪切");        cutBn.addActionListener(this);        toolbar.add(copyBn);        copyBn.setToolTipText("复制");        copyBn.addActionListener(this);        toolbar.add(pasteBn);        pasteBn.setToolTipText("粘贴");        pasteBn.addActionListener(this);        toolbar.add(undoBn);        undoBn.setToolTipText("上一步");        undoBn.addActionListener(this);        toolbar.add(redoBn);        redoBn.setToolTipText("下一步");        redoBn.addActionListener(this);    }    private void addRecentfiles(String filename) {        int offset;        if ((offset = recentfileslist.indexOf(filename)) == -1) {            recentfileslist.insertElementAt(filename, 0);            if (recentfileslist.size() > nRecentfiles) {                recentfileslist.removeElementAt(nRecentfiles);            }        } else {            recentfileslist.removeElementAt(offset);            recentfileslist.insertElementAt(filename, 0);        }        buildRecentfiles();    }    private void buildRecentfiles() {        int size = recentfileslist.size();        recentfiles.removeAll();        ActionListener menuListener = new ActionListener() {            public void actionPerformed(ActionEvent e) {                try {                    int decision = decide();                    if (decision == JOptionPane.YES_OPTION) {                        if (check()) {                            saveFile();                        } else {                            saveAs();                        }                    } else if (decision == JOptionPane.CANCEL_OPTION) {                        return;                    }                    text.setText("");                    String filename = e.getActionCommand();                    currentFile = new File(filename);                    BufferedReader reader;                    reader = new BufferedReader(new FileReader(currentFile));                    String oneline = null;                    oldContent =                            "";                    do {                        oneline = reader.readLine();                        if (oneline != null) {                            text.append(oneline + "\n"); //text.append(oneline + "\n");                        }                    } while (oneline != null);                    text.setSelectionEnd(0);                    oldContent =                            text.getText();                    String title = currentFile.getName();                    JNotepad.this.setTitle(title + "-记事本   Time@");                    addRecentfiles(currentFile.getPath());                } catch (IOException ex) {                    Logger.getLogger(JNotepad.class.getName()).log(Level.SEVERE, null, ex);                }            }        };        for (int i = 0; i <                size; i++) {            String file = (String) recentfileslist.get(i);            JMenuItem mi = new JMenuItem(file);            recentfiles.add(mi);            mi.addActionListener(menuListener);        }    }    private void addMusicfile(File file) {        if (file == null) {            return;        }        String fname = file.getName();        String suffix = fname.substring(fname.lastIndexOf(".") + 1);        if (!suffix.equals("mp3")) {            return;        }        int offset;        if ((offset = music.VdirName.indexOf(file.getPath())) == -1) {            int size = music.VdirName.size();            music.VfileName.insertElementAt(file.getName(), size - 1);            music.VdirName.insertElementAt(file.getPath(), size - 1);        }        buildMusicfiles(file);    }    private void buildMusicfiles(File file) {        ActionListener musicListener = new ActionListener() {            public void actionPerformed(ActionEvent e) {                String filename = e.getActionCommand();                music.index = music.VfileName.indexOf(filename);                music.createPlayer2();            }        };        JMenuItem sing = new JMenuItem(file.getName());        musiclist.add(sing);        sing.addActionListener(musicListener);        this.validate();    }    private String title;    private JToolBar toolbar = new JToolBar("工具栏");    private JButton newBn = new JButton(new ImageIcon("new.gif"));    private JButton openBn = new JButton(new ImageIcon("open.gif"));    private JButton saveBn = new JButton(new ImageIcon("save.gif"));    private JButton saveasBn = new JButton(new ImageIcon("saveas.gif"));    private JButton exitBn = new JButton(new ImageIcon("exit.gif"));    private JButton cutBn = new JButton(new ImageIcon("cut.gif"));    private JButton copyBn = new JButton(new ImageIcon("copy.gif"));    private JButton pasteBn = new JButton(new ImageIcon("paste.gif"));    private JButton undoBn = new JButton(new ImageIcon("undo.gif"));    private JButton redoBn = new JButton(new ImageIcon("redo.gif"));    private JMenuBar menubar;    private JMenu menu;    private JMenuItem newfile;    private JMenuItem openfile;    private JMenuItem savefile;    private JMenuItem saveas;    private JMenu recentfiles;    static final int nRecentfiles = 5;    private Vector recentfileslist = new Vector(nRecentfiles);    private JMenuItem exit;    private JPopupMenu popmenu = new JPopupMenu();    private JMenuItem popundoItem = new JMenuItem("撤销(U)");    private JMenuItem popredoItem = new JMenuItem("重做(R)");    private JMenuItem popcutItem = new JMenuItem("剪切(T)");    private JMenuItem popcopyItem = new JMenuItem("复制(C)");    private JMenuItem poppasteItem = new JMenuItem("粘贴(P)");    private JMenuItem popdeleteItem = new JMenuItem("删除(D)");    private JMenuItem popselectall = new JMenuItem("全选(A)");    private JMenu edit;    private JMenuItem undoItem;    private JMenuItem redoItem;    private JClipboard clipboard = new JClipboard();    private JMenuItem cutItem;    private JMenuItem copyItem;    private JMenuItem pasteItem;    private JMenuItem deleteItem;    private JMenuItem find;    private JMenuItem replace;    private JMenuItem selectall;    private JMenuItem time_date;    private JMenu style;    private JCheckBoxMenuItem enter;//自动换行    private JMenuItem font;    private JMenu bkmusic;    private JPanel musicpanel = new JPanel();    private JMusic music;    private JMenuItem musicImport = new JMenuItem("添加背景音乐");    private JMenu musicmode = new JMenu("音乐播放模式");    private ButtonGroup group = new ButtonGroup();    private JRadioButtonMenuItem[] mode = new JRadioButtonMenuItem[3];    private String[] modeValues = {"随机播放", "顺序播放", "单曲循环"};    private JMenu musiclist = new JMenu("背景音乐列表");    private JMenuItem bkcolor;    private JCheckBoxMenuItem drag;    private UndoManager Un_Re = new UndoManager();    private JMenu view;    private JCheckBoxMenuItem tool;    private JMenu about;    private JMenuItem info;    private boolean candrag = true;    private MyJTextArea text = new MyJTextArea();    private JScrollPane scroll = new JScrollPane(text);    String filename = "";    private File currentFile = new File(filename);    String oldContent = "";    String newContent = "";    private Thread thread = new Thread(this);    public void run() {        while (true) {            Date d = new Date();            String str = DateFormat.getDateTimeInstance().format(d);            title =                    this.getTitle();            int index = title.lastIndexOf("@");            title =                    title.substring(0, index + 1);            this.setTitle(title + str);        }    }    public void itemStateChanged(ItemEvent e) {        if (e.getSource() == enter) {            if (enter.isSelected()) {                text.setLineWrap(true);            } else {                text.setLineWrap(false);            }        }        if (e.getSource() == drag) {            if (drag.isSelected()) {                text.candrag = true;            } else {                text.candrag = false;            }        }        if (e.getSource() == tool) {            if (tool.isSelected()) {                addToolBar();            } else {                this.remove(toolbar);            }            this.validate();        }    }    private boolean check() {        String str = "";        try {            str = title.substring(0, title.lastIndexOf("-记事本"));        } catch (Exception e) {            return false;        }        if (str.endsWith("无标题")) {            return false;        }        return true;    }}/** * * @author Administrator */public class Main {    /**     * @param args the command line arguments     */    public static void main(String[] args) {        new JNotepad();    // thread.start();    // TODO code application logic here    }}

⌨️ 快捷键说明

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