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

📄 javaword.java

📁 程序员在编程过程中
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
                || e.getSource() == selectAllPopup) {
            text.selectAll();
        }//全选
        if (e.getSource() == newline) {
            text.setLineWrap(true);
        }//自动换行
        if (e.getSource() ==about){
            Runtime currentRunTime = Runtime.getRuntime();
            Process newProcess = null;
            try { 
                newProcess = currentRunTime.exec("bangzhu.txt");
            } catch (Exception ex) {
                JOptionPane.showMessageDialog(MainWindow.this,
                        "不能加载文件,文件是否存在", "打开文件",
                        JOptionPane.ERROR_MESSAGE);
                ex.printStackTrace();
            }
        }
        if (e.getSource() == exit || e.getSource() == bexit) {
            if (true ==textAction) {
                saveDialog();
                setVisible(false);
                shutWindow();
            } else {
                setVisible(false);
                shutWindow();
            }
            
        }
    }// 各主件的行为响应完毕
    public void openDialog() {
        chooser.setCurrentDirectory(new File("."));
        // 文件过滤
        chooser.setFileFilter(new FileFilter() {
            public boolean accept(File f) {
                String fileName = f.getName().toLowerCase();
                return fileName.endsWith(".txt") || f.isDirectory();
            }
            public String getDescription() {
                return "文本文件";
            }
        });
        int openResult = chooser.showOpenDialog(MainWindow.this);
        if (openResult == JFileChooser.APPROVE_OPTION) {
            text.setText("");
            try {
                in = new BufferedReader(new FileReader(chooser
                        .getSelectedFile().getPath()));
                String line;
                while ((line = in.readLine()) != null) {
                    text.append(line);
                    text.append("\n");
                    text.validate();
                }
            } catch (Exception e) {
                JOptionPane.showMessageDialog(MainWindow.this, "文件不能打开",
                        "打开文件", JOptionPane.ERROR_MESSAGE);
                e.printStackTrace();
            }
        } else {
            JOptionPane.showMessageDialog(MainWindow.this, "请选要打开的文件",
                    "打开文件", JOptionPane.WARNING_MESSAGE);
        }
    } // 打开对话框
    // 保存对话框
    public void saveDialog() {
        chooser.setCurrentDirectory(new File("."));
        // 文件过滤
        chooser.setFileFilter(new FileFilter() {
            public boolean accept(File f) {
                String fileName = f.getName().toLowerCase();
                return fileName.endsWith(".txt") || f.isDirectory();
            }
            public String getDescription() {
                return "文本文件";
            }
        });
        int saveResult = chooser.showSaveDialog(MainWindow.this);
        if (saveResult == JFileChooser.APPROVE_OPTION) {
            try {
                write = new PrintWriter(new FileOutputStream(chooser
                        .getSelectedFile().getName()), true);
                write.println(text.getText());
            } catch (Exception e) {
                JOptionPane.showMessageDialog(MainWindow.this, "文件不能保存",
                        "保存文件", JOptionPane.ERROR_MESSAGE);
                e.printStackTrace();
            }
        } else {
            JOptionPane.showMessageDialog(MainWindow.this, "请保存文件",
                    "保存文件", JOptionPane.WARNING_MESSAGE);
        }
    }// 保存对话框
    public void shutWindow() {
        System.gc();
    }
    public void keyTyped(KeyEvent e) {
    }
    public void keyPressed(KeyEvent e) {
        textAction = true;
    }
    public void keyReleased(KeyEvent e) {
    }
    public void mouseClicked(MouseEvent e) {
    }
    public void mousePressed(MouseEvent me) {
        if (me.isPopupTrigger()) {
            textPopup.show(text, me.getX(), me.getY());
        }
    }
    public void mouseReleased(MouseEvent me) {
        if (me.isPopupTrigger()) {
            textPopup.show(text, me.getX(), me.getY());
        }
    }
    public void mouseEntered(MouseEvent e) {
    }
    public void mouseExited(MouseEvent e) {
    }
    // undo redo 判断是否可用
    public void undoableEditHappened(UndoableEditEvent parm1) {
        undoManger.addEdit(parm1.getEdit());
    }
    public void windowOpened(WindowEvent e) {
        scroll.setFocusable(true);
    }
    public void windowClosing(WindowEvent e) {
        if (true == textAction) {
            saveDialog();
            setVisible(false);
        }
    }
    public void windowClosed(WindowEvent e) {
    }
    public void windowIconified(WindowEvent e) {
    }
    public void windowDeiconified(WindowEvent e) {
    }
    public void windowActivated(WindowEvent e) {
    }
    public void windowDeactivated(WindowEvent e) {
    }
}
//查找实现
class FindDialog extends JDialog implements ActionListener, WindowListener,
        KeyListener {
    final JTextArea text;
    JLabel lfind, lreplace;
    JTextField tfind, treplace;
    JButton bfind, breplace, bnext, bcancel;
    JPanel fieldPane, buttonPane;
    String strFind, strReplace;
    int txtPlace, txtLen, strNext;
    FindDialog(final JTextArea text, JFrame findDialog) {
        super(findDialog, "查找/替换", false);
        this.text = text;
        Container findPane = getContentPane();
        lfind = new JLabel("查找内容");
        lreplace = new JLabel("替换");
        tfind = new JTextField();
        treplace = new JTextField();
        bfind = new JButton("查找", new ImageIcon("image/search.gif"));
        breplace = new JButton("替换", new ImageIcon("image/replace.gif"));
        bcancel = new JButton("取消", new ImageIcon("image/cancel.gif"));
        bnext = new JButton("下一个", new ImageIcon("image/findagain.gif"));
        fieldPane = new JPanel();
        buttonPane = new JPanel();
        fieldPane.setLayout(new GridLayout(2, 2));
        findPane.add(fieldPane, BorderLayout.CENTER);
        findPane.add(buttonPane, BorderLayout.SOUTH);
        fieldPane.add(lfind);
        fieldPane.add(tfind);
        fieldPane.add(lreplace);
        fieldPane.add(treplace);
        buttonPane.add(bfind);
        buttonPane.add(breplace);
        buttonPane.add(bnext);
        buttonPane.add(bcancel);
        bfind.addActionListener(this);
        breplace.addActionListener(this);
        bcancel.addActionListener(this);
        this.addWindowListener(this);
        tfind.addKeyListener(this);
        treplace.addKeyListener(this);
        bnext.addActionListener(this);
        
        setSize(400,300);
        setResizable(false);
    }
    public void actionPerformed(ActionEvent e) {
        if (e.getSource() == bfind) {
            find();
        }
        if (e.getSource() == breplace) {
            replace();
        }
        if (e.getSource() == bcancel) {
            setVisible(false);
        }
        if (e.getSource() == bnext) {
            findNext();
        }
    }
    //查找处理
    private void find() {
        strFind = tfind.getText();
        strNext = 0; //开始位置
        txtPlace = text.getText().indexOf(strFind, strNext);
        txtLen = txtPlace + strFind.length();
        text.select(txtPlace, txtLen);
        strNext = text.getSelectionEnd(); //选中内容的最后位置
        if (txtPlace == -1) {
            JOptionPane.showMessageDialog(null, "没有找到", "查找",
                    JOptionPane.INFORMATION_MESSAGE);
        }
        bnext.setEnabled(true);
    }
    //替换处理
    private void replace() {
        strReplace = treplace.getText();
        text.replaceRange(strReplace, txtPlace, txtPlace + strFind.length());
    }
    //查找下一个处理
    private void findNext() {
        bfind.setEnabled(false);
        txtPlace = text.getText().indexOf(strFind, strNext);
        txtLen = txtPlace + strFind.length();
        text.select(txtPlace, txtLen);
        strNext = text.getSelectionEnd(); //查找下一个内容的最后位置
        if (txtPlace == -1) /*没有找到的处理*/ {
            JOptionPane.showMessageDialog(null, "没有找到", "查找下一个",
                    JOptionPane.INFORMATION_MESSAGE);
            strNext = 0; //没有找到初始化位置,以变重新查找
            tfind.setText("");
            bnext.setEnabled(false);
            breplace.setEnabled(false);
        }
    }
    public void windowOpened(WindowEvent e) {
        bfind.setEnabled(false);
        breplace.setEnabled(false);
        bnext.setEnabled(false);
    }
    public void windowClosing(WindowEvent e) {
        // TODO 自动生成方法存根
    }
    public void windowClosed(WindowEvent e) {
        // TODO 自动生成方法存根
    }
    public void windowIconified(WindowEvent e) {
        // TODO 自动生成方法存根
    }
    public void windowDeiconified(WindowEvent e) {
        // TODO 自动生成方法存根
    }
    public void windowActivated(WindowEvent e) {
        // TODO 自动生成方法存根
    }
    public void windowDeactivated(WindowEvent e) {
        // TODO 自动生成方法存根
    }
    public void keyTyped(KeyEvent e) {
        // TODO 自动生成方法存根
    }
    public void keyPressed(KeyEvent ke) {
        if (ke.getSource() == tfind)
            bfind.setEnabled(true);
        if (ke.getSource() == treplace)
            breplace.setEnabled(true);
    }
    public void keyReleased(KeyEvent e) {
        // TODO 自动生成方法存根
    }
}
// 帮助的关于对话框行为
class ActionAbout extends JDialog {
    public ActionAbout(JFrame dialog) {
        super(dialog, "关于", true);
        Container contentPane = getContentPane();
        contentPane.add(new JLabel("欢迎使用JAVA文本编辑器"
                + "日期2007.12"),
                BorderLayout.CENTER);
        JPanel p = new JPanel();
        JButton ok = new JButton("确定", new ImageIcon("image/ok.gif"));
        setResizable(false);
        ok.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                setVisible(false);
            }
        });
        p.add(ok);
        contentPane.add(p, BorderLayout.SOUTH);
        contentPane.add(new JLabel(new ImageIcon("image/java.gif")),
                BorderLayout.WEST);
        setSize(350,270);
    }
}
    
    /** Creates a new instance of JavaWord */

    
    /**
     * @param args the command line arguments
     */
   

⌨️ 快捷键说明

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