📄 book.java
字号:
/* * Book.java * * Created on 2008年12月12日, 上午9:46 */import javax.swing.*;import java.awt.*;import java.awt.Font;import java.awt.event.*;import java.util.*;import java.io.*;import javax.swing.event.DocumentEvent;import javax.swing.event.DocumentListener;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import java.awt.print.PrinterJob;import java.io.File;import java.io.FileReader;import java.io.FileWriter;import javax.swing.JOptionPane;import javax.swing.event.DocumentEvent;import javax.swing.event.DocumentListener;import javax.swing.event.UndoableEditEvent;import javax.swing.event.UndoableEditListener;import javax.swing.undo.UndoManager;import java.text.SimpleDateFormat;import javax.print.attribute.HashPrintRequestAttributeSet;import javax.print.attribute.PrintRequestAttributeSet;import javax.swing.*;import javax.swing.text.DefaultStyledDocument;import javax.swing.text.StyledDocument;import javax.swing.filechooser.FileFilter; /** * * @author Admin */public class Book extends javax.swing.JFrame { boolean isChange=false; private JFileChooser fileChooser= new JFileChooser(); private final UndoManager Manager = new UndoManager(); private UndoableEditListener ue = new UndoHander(); private File fileName; PrintRequestAttributeSet attributes; /** Creates new form Book */ public Book() { initComponents(); Text.getDocument().addUndoableEditListener(ue); Text.getDocument().addDocumentListener( new DocumentListener(){ public void changedUpdate(DocumentEvent de){ isChange=true; } public void insertUpdate(DocumentEvent de){ isChange=true; } public void removeUpdate(DocumentEvent de){ isChange=true; } } ); } private boolean hasChange(){ return isChange; } private void CopyPerformend(ActionEvent e){ Text.copy(); } private void CutPerformed(ActionEvent e){ Text.cut(); } private void PastePerformed(ActionEvent e){ Text.paste(); } class UndoHander implements UndoableEditListener { public void undoableEditHappened(UndoableEditEvent eundo) { Manager.addEdit(eundo.getEdit()); } } /** This method is called from within the constructor to * initialize the form. * WARNING: Do NOT modify this code. The content of this method is * always regenerated by the Form Editor. */ // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents private void initComponents() { menu = new javax.swing.JPopupMenu(); itemUndo = new javax.swing.JMenuItem(); itemRedo = new javax.swing.JMenuItem(); itemCopy = new javax.swing.JMenuItem(); itemCut = new javax.swing.JMenuItem(); itemPaste = new javax.swing.JMenuItem(); itemDelete = new javax.swing.JMenuItem(); itemSelect = new javax.swing.JMenuItem(); jScrollPane1 = new javax.swing.JScrollPane(); Text = new javax.swing.JTextArea(); Label = new javax.swing.JLabel(); jPanel1 = new javax.swing.JPanel(); jToolBar1 = new javax.swing.JToolBar(); bNew = new javax.swing.JButton(); bOpen = new javax.swing.JButton(); bSave = new javax.swing.JButton(); jToolBar2 = new javax.swing.JToolBar(); bCut = new javax.swing.JButton(); bCopy = new javax.swing.JButton(); bPaste = new javax.swing.JButton(); bDelete = new javax.swing.JButton(); bUndo = new javax.swing.JButton(); bRedo = new javax.swing.JButton(); jLabel1 = new javax.swing.JLabel(); jMenuBar1 = new javax.swing.JMenuBar(); File = new javax.swing.JMenu(); New = new javax.swing.JMenuItem(); jSeparator1 = new javax.swing.JSeparator(); Open = new javax.swing.JMenuItem(); jSeparator2 = new javax.swing.JSeparator(); Save = new javax.swing.JMenuItem(); SaveAs = new javax.swing.JMenuItem(); Print = new javax.swing.JMenuItem(); jSeparator3 = new javax.swing.JSeparator(); Quit = new javax.swing.JMenuItem(); Edit = new javax.swing.JMenu(); Undo = new javax.swing.JMenuItem(); Redo = new javax.swing.JMenuItem(); jSeparator4 = new javax.swing.JSeparator(); Copy = new javax.swing.JMenuItem(); Cut = new javax.swing.JMenuItem(); Paste = new javax.swing.JMenuItem(); Delete = new javax.swing.JMenuItem(); jSeparator5 = new javax.swing.JSeparator(); Search = new javax.swing.JMenuItem(); Insert = new javax.swing.JMenuItem(); Time = new javax.swing.JMenuItem(); SelectAll = new javax.swing.JMenuItem(); Form = new javax.swing.JMenu(); Font = new javax.swing.JMenuItem(); SetColor = new javax.swing.JMenuItem(); BackColor = new javax.swing.JMenuItem(); Help = new javax.swing.JMenu(); About = new javax.swing.JMenuItem(); itemUndo.setText("撤消"); itemUndo.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { itemUndoActionPerformed(evt); } }); menu.add(itemUndo); itemRedo.setText("恢复"); itemRedo.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { itemRedoActionPerformed(evt); } }); menu.add(itemRedo); itemCopy.setText("复制"); itemCopy.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { itemCopyActionPerformed(evt); } }); menu.add(itemCopy); itemCut.setText("剪切"); itemCut.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { itemCutActionPerformed(evt); } }); menu.add(itemCut); itemPaste.setText("粘贴"); itemPaste.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { itemPasteActionPerformed(evt); } }); menu.add(itemPaste); itemDelete.setText("删除"); itemDelete.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { itemDeleteActionPerformed(evt); } }); menu.add(itemDelete); itemSelect.setText("全选"); itemSelect.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { itemSelectActionPerformed(evt); } }); menu.add(itemSelect); setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); setTitle("Java记事本----易华容"); setResizable(false); Text.setColumns(20); Text.setRows(5); Text.addMouseListener(new java.awt.event.MouseAdapter() { public void mousePressed(java.awt.event.MouseEvent evt) { TextMousePressed(evt); } }); Text.addCaretListener(new javax.swing.event.CaretListener() { public void caretUpdate(javax.swing.event.CaretEvent evt) { TextCaretUpdate(evt); } }); jScrollPane1.setViewportView(Text); jToolBar1.setRollover(true); bNew.setIcon(new javax.swing.ImageIcon(getClass().getResource("/new.gif"))); // NOI18N bNew.setToolTipText("新建"); bNew.setFocusable(false); bNew.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER); bNew.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM); bNew.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { bNewActionPerformed(evt); } }); jToolBar1.add(bNew); bNew.getAccessibleContext().setAccessibleDescription(""); bOpen.setIcon(new javax.swing.ImageIcon(getClass().getResource("/open.gif"))); // NOI18N bOpen.setToolTipText("打开"); bOpen.setFocusable(false); bOpen.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER); bOpen.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM); bOpen.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { bOpenActionPerformed(evt); } }); jToolBar1.add(bOpen); bSave.setIcon(new javax.swing.ImageIcon(getClass().getResource("/save.gif"))); // NOI18N bSave.setToolTipText("保存"); bSave.setFocusable(false); bSave.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER); bSave.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM); bSave.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { bSaveActionPerformed(evt); } }); jToolBar1.add(bSave); jToolBar2.setRollover(true); bCut.setIcon(new javax.swing.ImageIcon(getClass().getResource("/cut.gif"))); // NOI18N bCut.setToolTipText("剪切"); bCut.setFocusable(false); bCut.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER); bCut.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM); bCut.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { bCutActionPerformed(evt); } }); jToolBar2.add(bCut); bCopy.setIcon(new javax.swing.ImageIcon(getClass().getResource("/copy.gif"))); // NOI18N bCopy.setToolTipText("复制"); bCopy.setFocusable(false); bCopy.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER); bCopy.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM); bCopy.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { bCopyActionPerformed(evt); } }); jToolBar2.add(bCopy); bPaste.setIcon(new javax.swing.ImageIcon(getClass().getResource("/paste.gif"))); // NOI18N bPaste.setToolTipText("粘贴"); bPaste.setFocusable(false); bPaste.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER); bPaste.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM); bPaste.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { bPasteActionPerformed(evt); } }); jToolBar2.add(bPaste);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -