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

📄 jnotepad.java

📁 一个模拟文字编辑器
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
import java.awt.*;
import java.awt.event.*;
import java.awt.print.*;
import java.awt.datatransfer.*;
import java.io.*;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.text.*;

public class JNotepad extends JPanel
{
    JTextArea jta = new JTextArea("", 24, 40);

    JScrollPane jsp = new JScrollPane(jta);

    JMenuBar jmb = new JMenuBar();
    JMenu file = new JMenu("File");
    JMenu edit = new JMenu("Edit");
    JMenu help = new JMenu("Help");
    JMenu search = new JMenu("Search");

    JToolBar toolBar = new JToolBar();

    JMenuItem jmi;

    Clipboard clipbd = getToolkit().getSystemClipboard();

    PrinterJob prtMe = PrinterJob.getPrinterJob();

    String text = "";

    public JNotepad()
    {
        class newL implements ActionListener
        {
            public void actionPerformed(ActionEvent e)
            {
                jta.setDocument(new PlainDocument());
            }
        }

        class openL implements ActionListener
        {
            public void actionPerformed(ActionEvent e)
            {
                JFileChooser fc = new JFileChooser();
                int returnVal = fc.showDialog(JNotepad.this, "Open file");
                if(returnVal == JFileChooser.APPROVE_OPTION)
                {
                    String file = fc.getSelectedFile().getPath();
                    if(file == null)
                    {
                        return;
                    }
                    try
                    {
                        Reader in = new FileReader(file);
                        char[] buff = new char[4096];
                        int nch;
                        while((nch = in.read(buff, 0, buff.length)) != -1)
                        {
                            jta.setDocument(new PlainDocument());
                            jta.append(new String(buff, 0, nch));
                        }
                    }
                    catch (IOException io)
                    {
                        System.err.println("IOException: " + io.getMessage());
                    }
                }
                else
                {
                    return;
                }
            }
        }

        class saveL implements ActionListener
        {
            public void actionPerformed(ActionEvent e)
            {
                JFileChooser fc = new JFileChooser();
                int returnVal = fc.showSaveDialog(JNotepad.this);
                if(returnVal == JFileChooser.APPROVE_OPTION)
                {
                    String savefile = fc.getSelectedFile().getPath();
                    if(savefile == null)
                    {
                        return;
                    }
                    else
                    {
                        String docToSave = jta.getText();
                        if(docToSave != null)
                        {
                            FileOutputStream fstrm = null;
                            BufferedOutputStream ostrm = null;
                            try
                            {
                                fstrm = new FileOutputStream(savefile);
                                ostrm = new BufferedOutputStream(fstrm);
                                byte[] bytes = null;
                                try
                                {
                                    bytes = docToSave.getBytes();
                                }
                                catch(Exception e1)
                                {
                                    e1.printStackTrace();
                                }
                                ostrm.write(bytes);
                            }
                           catch(IOException io)
                           {
                               System.err.println("IOException: " +
                                       io.getMessage());
                           }
                           finally
                           {
                                try
                                {
                                    ostrm.flush();
                                    fstrm.close();
                                    ostrm.close();
                                }
                                catch(IOException ioe)
                                {
                                    System.err.println("IOException: " +
                                            ioe.getMessage());
                                }
                            }
                        }
                    }
                }
                else
                {
                    return;
                }
            }
        }

        class pageSetupL implements ActionListener
        {
            public void actionPerformed(ActionEvent e)
            {
                prtMe.printDialog();
            }
        }

        class printL implements ActionListener
        {
            public void actionPerformed(ActionEvent e)
            {
                try
                {
                    prtMe.print();
                }
                catch(Exception ew)
                {
                }
            }
        }

        class exitL implements ActionListener
        {
            public void actionPerformed(ActionEvent e)
            {
                System.exit(0);
            }
        }

        class copyL implements ActionListener
        {
            public void actionPerformed(ActionEvent e)
            {
                String selection = jta.getSelectedText();
                StringSelection clipString = new StringSelection(selection);
                clipbd.setContents(clipString, clipString);
            }
        }

        class cutL implements ActionListener
        {
            public void actionPerformed(ActionEvent e)
            {
                String selection = jta.getSelectedText();
                StringSelection clipString = new StringSelection(selection);
                clipbd.setContents(clipString, clipString);
                jta.replaceRange("", jta.getSelectionStart(),
                        jta.getSelectionEnd());
            }
        }

        class pasteL implements ActionListener
        {
            public void actionPerformed(ActionEvent e)
            {
                Transferable clipData = clipbd.getContents(JNotepad.this);
                try
                {
                    String clipString =
                            (String)clipData.getTransferData(
                            DataFlavor.stringFlavor);
                    jta.replaceRange(clipString,
                            jta.getSelectionStart(), jta.getSelectionEnd());
                }
                catch(Exception ex)
                {
                }
            }
        }

        class deleteL implements ActionListener
        {
            public void actionPerformed(ActionEvent e)
            {
                String selection = jta.getSelectedText();
                jta.replaceRange("", jta.getSelectionStart(),
                        jta.getSelectionEnd());
            }
        }

        class selectAllL implements ActionListener
        {
            public void actionPerformed(ActionEvent e)
            {
                jta.selectAll();
            }
        }

        class findL implements ActionListener
        {
            public void actionPerformed(ActionEvent e)
            {
                String find = "";
                find = JOptionPane.showInputDialog(
                        "Find what: ");
            }
        }

        class findNextL implements ActionListener
        {
            public void actionPerformed(ActionEvent e)
            {
            }
        }

        class aboutL implements ActionListener
        {
            public void actionPerformed(ActionEvent e)
            {
                JOptionPane.showMessageDialog(null,
                    "ABOUT.");
            }
        }

        class jtaL implements ActionListener
        {
            public void actionPerformed(ActionEvent e)
            {
            }
        }

⌨️ 快捷键说明

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