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

📄 javaword.java

📁 程序员在编程过程中
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/*
 * JavaWord.java
 *
 * Created on 2007年12月25日, 下午2:40
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package javaword;
import java.awt.*;
import java.awt.event.*;
import java.awt.print.PrinterJob;
import java.io.*;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Vector;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.filechooser.FileFilter;
import javax.swing.undo.UndoManager;

/**
 *
 * @author 钟晓媛   计算机05-1   200507111007
 */
public class JavaWord {
    public static void main(String[] args) {
        JavaWord.start(); }
    public static void start() {
        JFrame mainWindow = new MainWindow();
        mainWindow.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        mainWindow.setVisible(true);
    }
}
class MainWindow extends JFrame implements ActionListener, KeyListener,
        MouseListener, UndoableEditListener, WindowListener {
  private    JMenuBar menuBar;
   private    JMenu file, edit, style, help;
   private    JMenuItem new1, open, save, exit,
              redo, undo, cut, copy, paste, selectAll,
               newline, helpContent, about, 
              redoPopup, undoPopup, cutPopup, copyPopup, pastePopup,
           selectAllPopup, timeDate, find;
    private    JToolBar bar;
    private    JTextArea text; 
    private   JScrollPane scroll;
    private    JFileChooser chooser;
    private    boolean textAction = false; 
// 定义记录文本区第一次按下的行为窗口关闭时,提示保存文件
   MainWindow exitWindow;
     ActionAbout dialog;
    BufferedReader in;
    PrintWriter write;
    JButton bnew, bopen, bsave, bexit, bredo, bundo, bcut, bcopy, bpaste,
            bfind;
    UndoManager undoManger;
// 定义出undo/redo对象
    JPopupMenu textPopup;
    MainWindow() {
        setTitle("文本编辑器");
        setSize(500,300);
        setLayout(new BorderLayout());
        // 设置窗口响应事件
        this.addWindowListener(this);
        text = new JTextArea();
        scroll = new JScrollPane(text);
        getContentPane().add(scroll, BorderLayout.CENTER);
        // 设置文本区响应事件
        text.addMouseListener(this);
        text.addKeyListener(this);
        // 设置菜单栏
        menuBar = new JMenuBar();
        setJMenuBar(menuBar);
        // 设置文件菜单
        file = new JMenu("文件(F)");
        file.setMnemonic('F');
        menuBar.add(file);
        new1 = new JMenuItem("新建", new ImageIcon("image/new.gif"));
        new1.setHorizontalTextPosition(SwingConstants.RIGHT);
        new1.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_N,
                InputEvent.CTRL_MASK));//新建文件
        open = new JMenuItem("打开", new ImageIcon("image/open.gif"));
        open.setHorizontalTextPosition(SwingConstants.RIGHT);
        open.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_O,
                InputEvent.CTRL_MASK));//打开文件
        save = new JMenuItem("保存", new ImageIcon("image/save.gif"));
        save.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_S,
                InputEvent.CTRL_MASK));//保存文件
        exit = new JMenuItem("退出", new ImageIcon("image/close.gif"));
        exit.setHorizontalTextPosition(SwingConstants.RIGHT);
        exit.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_Q,
                InputEvent.CTRL_MASK));//退出
        //把各项加到菜单里
        file.add(new1);
        file.add(open);
        file.add(save);
        file.add(exit);
        // 增加文件菜单事件监听
        new1.addActionListener(this);
        open.addActionListener(this);
        save.addActionListener(this);
        exit.addActionListener(this);
        // 设置编辑菜单
        edit = new JMenu("编辑(E)");
        edit.setMnemonic('E');
        menuBar.add(edit);//编辑
        redo = new JMenuItem("重做", new ImageIcon("image/redo.gif"));
        redo.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_R,
                InputEvent.CTRL_MASK));//重做
        undo = new JMenuItem("撤消", new ImageIcon("image/undo.gif"));
        undo.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_Z,
                InputEvent.CTRL_MASK));//撤消
        cut = new JMenuItem("剪切", new ImageIcon("image/cut.gif"));
        cut.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_X,
                InputEvent.CTRL_MASK));//剪切
        copy = new JMenuItem("复制", new ImageIcon("image/copy.gif"));
        copy.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_C,
                InputEvent.CTRL_MASK));//复制
        paste = new JMenuItem("粘贴", new ImageIcon("image/paste.gif"));
        paste.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_V,
                InputEvent.CTRL_MASK));//粘贴
        timeDate = new JMenuItem("插入日期时间", new ImageIcon("image/date.gif"));
        timeDate.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_T,
                InputEvent.CTRL_MASK));//插入日期时间
        find = new JMenuItem("查找/替换", new ImageIcon("image/search.gif"));
        find.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_F,
                InputEvent.CTRL_MASK));//查找替换
        selectAll = new JMenuItem("全选", new ImageIcon("image/selectall.gif"));
        selectAll.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_A,
                InputEvent.CTRL_MASK));//全选
        //把各项加入到菜单里
        edit.add(redo);
        edit.add(undo);
        edit.add(cut);
        edit.add(copy);
        edit.add(paste);
        edit.add(timeDate);
        edit.add(find);
        edit.add(selectAll);
        // 增加编辑菜单事响应
        redo.addActionListener(this);
        undo.addActionListener(this);
        cut.addActionListener(this);
        copy.addActionListener(this);
        paste.addActionListener(this);
        selectAll.addActionListener(this);
        timeDate.addActionListener(this);
        find.addActionListener(this);
        // 设置格式菜单
        style = new JMenu("格式(S)");
        style.setMnemonic('S');
        newline = new JMenuItem("自动换行");//换行
        menuBar.add(style);
        style.add(newline);
        // 增加工具事件响应
        newline.addActionListener(this);
        // 设置帮助菜单
        help = new JMenu("帮助(H)");
        help.setMnemonic('H');
        menuBar.add(help);
        about = new JMenuItem("关于", new ImageIcon("image/about.gif"));
        help.add(about);//关于
        about.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                dialog = new ActionAbout(MainWindow.this);
// 调用自定义关于对话框
                dialog.setVisible(true);
            }
        });
        // 工具栏按钮定义
        bnew = new JButton(new ImageIcon("image/new.gif"));
        bopen = new JButton(new ImageIcon("image/open.gif"));
        bsave = new JButton(new ImageIcon("image/save.gif"));
        bredo = new JButton(new ImageIcon("image/redo.gif"));
        bundo = new JButton(new ImageIcon("image/undo.gif"));
        bcut = new JButton(new ImageIcon("image/cut.gif"));
        bcopy = new JButton(new ImageIcon("image/copy.gif"));
        bpaste = new JButton(new ImageIcon("image/paste.gif"));
        bfind = new JButton(new ImageIcon("image/search.gif"));
        bexit = new JButton(new ImageIcon("image/close.gif"));
        // 设置提示文本
        bnew.setToolTipText("新建");
        bopen.setToolTipText("打开");
        bsave.setToolTipText("保存");
        bredo.setToolTipText("重做");
        bundo.setToolTipText("撤消");
        bcut.setToolTipText("剪切");
        bcopy.setToolTipText("复制");
        bpaste.setToolTipText("粘贴");
        bfind.setToolTipText("查找/替换");
        bexit.setToolTipText("退出");
        bar = new JToolBar();
        getContentPane().add(bar, BorderLayout.NORTH);
        bar.add(bnew);
        bar.add(bopen);
        bar.add(bsave);
        bar.add(bredo);
        bar.add(bundo);
        bar.add(bcut);
        bar.add(bcopy);
        bar.add(bpaste);
        bar.add(bfind);
        bar.add(bexit);
        // 增加工具栏按钮响应
        bnew.addActionListener(this);
        bopen.addActionListener(this);
        bsave.addActionListener(this);
        bredo.addActionListener(this);
        bundo.addActionListener(this);
        bcut.addActionListener(this);
        bcopy.addActionListener(this);
        bpaste.addActionListener(this);
        bfind.addActionListener(this);
        bexit.addActionListener(this);
        // 设置弹出菜单  快捷键
        textPopup = new JPopupMenu();
        text.add(textPopup);
        redoPopup = new JMenuItem("重做", new ImageIcon("image/redo.jpg"));
        redoPopup.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_R,
                InputEvent.CTRL_MASK));//重做
        undoPopup = new JMenuItem("撤消", new ImageIcon("image/undo.jpg"));
        undoPopup.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_Z,
                InputEvent.CTRL_MASK));//撤消
        cutPopup = new JMenuItem("剪切", new ImageIcon("image/cut.gif"));
        cutPopup.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_X,
                InputEvent.CTRL_MASK));//剪切
        copyPopup = new JMenuItem("复制", new ImageIcon("image/copy.gif"));
        copyPopup.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_C,
                InputEvent.CTRL_MASK));//复制
        pastePopup = new JMenuItem("粘贴", new ImageIcon("image/paste.gif"));
        pastePopup.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_V,
                InputEvent.CTRL_MASK));//粘贴
        selectAllPopup = new JMenuItem("全选", new ImageIcon("image/selectall.gif"));
        selectAllPopup.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_A,
                InputEvent.CTRL_MASK));//全选
        
        textPopup.add(redoPopup);
        textPopup.add(undoPopup);
        textPopup.addSeparator();
        textPopup.add(cutPopup);
        textPopup.add(copyPopup);
        textPopup.add(pastePopup);
        textPopup.addSeparator();
        textPopup.add(selectAllPopup);
        // 设置弹出菜单响应
        redoPopup.addActionListener(this);
        undoPopup.addActionListener(this);
        cutPopup.addActionListener(this);
        copyPopup.addActionListener(this);
        pastePopup.addActionListener(this);
        selectAllPopup.addActionListener(this);
        // 设置文件对话
        chooser = new JFileChooser();
        // 设置撤消、重复对象
        undoManger = new UndoManager();
        
        text.getDocument().addUndoableEditListener(this);
    }
    // 各行为响应
    public void actionPerformed(ActionEvent e) {
        if (e.getSource() == new1 || e.getSource() == bnew) {
            if (true ==textAction) {
                saveDialog();
                JavaWord.start();
            } else {
                JavaWord.start();
            }
        }
        if (e.getSource() == open || e.getSource() == bopen) {
            openDialog();
        }//新建
        if (e.getSource() == save || e.getSource() == bsave) {
            saveDialog();
        }//保存
        if (e.getSource() == redo || e.getSource() == bredo
                || e.getSource() == redoPopup) {
             undoManger.redo(); 
        }//重做
        if (e.getSource() == undo || e.getSource() == bundo
                || e.getSource() == undoPopup) {
              undoManger.undo();
        }//撤消
        if (e.getSource() == cut || e.getSource() == bcut
                || e.getSource() == cutPopup) {
            text.cut();
        }
        if (e.getSource() == copy || e.getSource() == bcopy
                || e.getSource() == copyPopup) {
            text.copy();
        }//剪切
        if (e.getSource() == paste || e.getSource() == bpaste
                || e.getSource() == pastePopup) {
            text.paste();
        }//粘贴
        if (e.getSource() == find || e.getSource() == bfind) {
            FindDialog dFind = new FindDialog(text, MainWindow.this);
            dFind.setVisible(true);
        }//查找替换
        if (e.getSource() == timeDate) {
            SimpleDateFormat dateFormat = new SimpleDateFormat(
                    "yyyy'年'MM'月'dd'日'+ hh:mm:ss");
            Calendar today = Calendar.getInstance();
            String timeDate = dateFormat.format(today.getTime());
            text.append(timeDate);
        }//日期时间
        if (e.getSource() == selectAll 

⌨️ 快捷键说明

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