📄 javaword.java~2~
字号:
redoPopup.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_R, InputEvent.CTRL_MASK)); undoPopup = new JMenuItem("撤消", new ImageIcon("ico/undo.jpg")); undoPopup.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_Z, InputEvent.CTRL_MASK)); cutPopup = new JMenuItem("剪贴", new ImageIcon("ico/Cut16.gif")); cutPopup.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_X, InputEvent.CTRL_MASK)); copyPopup = new JMenuItem("复制", new ImageIcon("ico/copy.gif")); copyPopup.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_C, InputEvent.CTRL_MASK)); pastePopup = new JMenuItem("粘贴", new ImageIcon("ico/paste.gif")); pastePopup.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_V, InputEvent.CTRL_MASK)); selectAllPopup = new JMenuItem("全选", new ImageIcon("ico/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() == print || e.getSource() == bprint) { try { attributes = new HashPrintRequestAttributeSet(); PrinterJob job = PrinterJob.getPrinterJob(); if (job.printDialog()) job.print(attributes); } catch (Exception ex) { JOptionPane.showMessageDialog(MainWindow.this, e); } } 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 || e.getSource() == bselectAll || e.getSource() == selectAllPopup) { text.selectAll(); } if (e.getSource() == newline) { text.setLineWrap(true); } if (e.getSource() == toolBar) { if (toolBar.isSelected()) bar.setVisible(true); else bar.setVisible(false); } if (e.getSource() == helpContent || e.getSource() == bhelp) { Runtime currentRunTime = Runtime.getRuntime(); Process newProcess = null; try { // c:\winnt\hh.exe 为*.chm帮助文件的启动文件 newProcess = currentRunTime.exec("hh.exe JavaWord.chm"); } catch (Exception ex) { JOptionPane.showMessageDialog(MainWindow.this, "不能正确地加载帮助文件,请检查您的c:\\winnt\\hh.exe文件是否存在", "打开文件", JOptionPane.ERROR_MESSAGE); ex.printStackTrace(); } } if (e.getSource() == exit || e.getSource() == bexit) { if (true ==textAction) { saveDialog(); setVisible(false); shutWindow(); } else { setVisible(false); shutWindow(); } } if (e.getSource() == metal) { setFace("javax.swing.plaf.metal.MetalLookAndFeel"); } if (e.getSource() == motif) { setFace("com.sun.java.swing.plaf.motif.MotifLookAndFeel"); } if (e.getSource() == windows) { setFace("com.sun.java.swing.plaf.windows.WindowsLookAndFeel"); } }// 各主件的行为响应完毕 // 外观设置 public void setFace(String plaf) { try { UIManager.setLookAndFeel(plaf); SwingUtilities.updateComponentTreeUI(this); } catch (Exception ex) { ex.printStackTrace(); } } // 打开对话框 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 "Text Files"; } }); 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 "Text Files"; } }); 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()); // System.out.println(uedit); /*if (undoManger.canUndo()) { undo.setEnabled(true); bredo.setEnabled(true); // undoPopupItem.setEnabled(true); } else { undo.setEnabled(false); bundo.setEnabled(false); // undoPopupItem.setEnabled(false); } if (undoManger.canRedo()) { redo.setEnabled(true); bredo.setEnabled(true); // redoPopupItem.setEnabled(true); } else { redo.setEnabled(false); bredo.setEnabled(false); // redoPopupItem.setEnabled(false); }*/ } 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 Database extends JDialog implements KeyListener, WindowListener { JLabel lServer, lDbName, lSql, lUsername, lPassword; JTextField tServer, tDbName, tSql, tUsername; JPasswordField password; JButton bConection, bCancel; String strServer, strDbName, strSql, strUsername, strPassword; JPanel inputPane, buttonPane; public Database(JFrame databaseDialog) { super(databaseDialog, "连接数据库", true); Container contentPane = getContentPane(); buttonPane = new JPanel(); inputPane = new JPanel(); contentPane.add(inputPane, BorderLayout.CENTER); inputPane.setLayout(new GridLayout(6, 2)); // contentPane.setLayout(); lServer = new JLabel("数据库服务器"); lDbName = new JLabel("数据库名字"); lSql = new JLabel("SQL语句"); lUsername = new JLabel("数据库的用户名"); lPassword = new JLabel("数据库的密码"); tServer = new JTextField(); tDbName = new JTextField(); tSql = new JTextField("SELECT * FROM "); tUsername = new JTextField(); password = new JPasswordField();
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -