📄 book.java
字号:
p3.add(cancel); jf.add(p1,BorderLayout.NORTH); jf.add(p2,BorderLayout.CENTER); jf.add(p3,BorderLayout.SOUTH); jf.setSize(360,200); jf.setLocation(300,200); jf.setVisible(true); jf.setResizable(false); addEventHandler(); } private void addEventHandler(){ ok.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e){ String n1 = (String)fontName.getSelectedItem(); int n2 = fontStyle.getSelectedIndex(); String n3 = (String)fontSize.getSelectedItem(); textArea.setFont(new Font(n1,n2,Integer.parseInt(n3))); } }); cancel.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e){ jf.setVisible(false);// } } ); } }//GEN-LAST:event_FontActionPerformed private void SaveActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_SaveActionPerformed if(!hasChange()) return; FileWriter output=null; if(fileName!=null&&fileName.exists()){ try{ output=new FileWriter(fileName); output.write(Text.getText()); } catch(IOException e){ JOptionPane.showMessageDialog(this, "文件写入错误,没有保存"); } finally{ try{ output.close(); } catch(IOException ee){} output=null; } isChange=false; } else SaveAsActionPerformed(evt); // TODO add your handling code here: }//GEN-LAST:event_SaveActionPerformed private void SaveAsActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_SaveAsActionPerformed if(!hasChange()) return; FileWriter output=null; int ret=fileChooser.showSaveDialog(this); if(ret!=JFileChooser.APPROVE_OPTION){ isChange=true; return; } fileName=fileChooser.getSelectedFile(); try{ output=new FileWriter(fileName); output.write(Text.getText()); } catch(IOException e){ JOptionPane.showMessageDialog(this, "文件写入错误,没有保存"); } finally{ try{ output.close(); } catch(IOException ee){} output=null; } isChange=false; return; // TODO add your handling code here: }//GEN-LAST:event_SaveAsActionPerformed private void bRedoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bRedoActionPerformed RedoActionPerformed(evt); // TODO add your handling code here: }//GEN-LAST:event_bRedoActionPerformed private void BackColorActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_BackColorActionPerformed Color color=JColorChooser.showDialog(Text, "选择颜色", Color.white); Text.setBackground(color); // TODO add your handling code here: }//GEN-LAST:event_BackColorActionPerformed //显示时间: private void TimeActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_TimeActionPerformed SimpleDateFormat dateFormat = new SimpleDateFormat( "yyyy'年'MM'月'dd'日' HH:mm:ss"); Calendar today = Calendar.getInstance(); String timeDate = dateFormat.format(today.getTime()); jLabel1.setText(timeDate); // TODO add your handling code here: }//GEN-LAST:event_TimeActionPerformed //弹出式菜单1: private void TextMousePressed(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_TextMousePressed if(evt.getModifiers()==InputEvent.BUTTON3_MASK) menu.show(Text,evt.getX(),evt.getY()); // TODO add your handling code here: }//GEN-LAST:event_TextMousePressed //打印: private void PrintActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_PrintActionPerformed try { attributes = new HashPrintRequestAttributeSet(); PrinterJob job = PrinterJob.getPrinterJob(); if (job.printDialog()) job.print(attributes); } catch (Exception ex) { JOptionPane.showMessageDialog(this, evt); } // TODO add your handling code here: }//GEN-LAST:event_PrintActionPerformed //弹出式菜单2: private void itemUndoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_itemUndoActionPerformed UndoActionPerformed(evt); // TODO add your handling code here: }//GEN-LAST:event_itemUndoActionPerformed private void itemRedoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_itemRedoActionPerformed RedoActionPerformed(evt); // TODO add your handling code here: }//GEN-LAST:event_itemRedoActionPerformed private void itemCopyActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_itemCopyActionPerformed CopyActionPerformed(evt); // TODO add your handling code here: }//GEN-LAST:event_itemCopyActionPerformed private void itemCutActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_itemCutActionPerformed CutActionPerformed(evt); // TODO add your handling code here: }//GEN-LAST:event_itemCutActionPerformed private void itemPasteActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_itemPasteActionPerformed PasteActionPerformed(evt); // TODO add your handling code here: }//GEN-LAST:event_itemPasteActionPerformed private void itemDeleteActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_itemDeleteActionPerformed DeleteActionPerformed(evt); // TODO add your handling code here: }//GEN-LAST:event_itemDeleteActionPerformed private void itemSelectActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_itemSelectActionPerformed SelectAllActionPerformed(evt); // TODO add your handling code here: }//GEN-LAST:event_itemSelectActionPerformed private StyledDocument styledDoc=new DefaultStyledDocument(); private void InsertActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_InsertActionPerformed JFileChooser open = new JFileChooser(); // 查找文件 open.addChoosableFileFilter(new JpgFileFilter()); open.addChoosableFileFilter(new GifFileFilter()); int state=open.showOpenDialog(Print); File file=open.getSelectedFile(); if(file!=null&&state==JFileChooser.APPROVE_OPTION) insertIcon(file); // 插入图片 // TODO add your handling code here: }//GEN-LAST:event_InsertActionPerformed public void insertIcon(File file) { //插入图片的方法 Text.setCaretPosition(styledDoc.getLength()); // 设置插入位置 //Text.insertIcon(new ImageIcon(file.getPath())); // 插入图片 } public void insertDoc(StyledDocument styledDoc, String content,String currentStyle) { try { int len=Text.getSelectedText().length(); styledDoc.insertString(Text.getCaretPosition(),content,styledDoc.getStyle(currentStyle)); Text.replaceSelection(""); Text.select(Text.getCaretPosition()-len,Text.getCaretPosition()); } catch (Exception e) {} }/*********用干文件过滤的外部类***********/ class GifFileFilter extends FileFilter{ public String getDescription(){ return "gif源文件(*.gif)"; } public boolean accept(File file) { return file.getName().toLowerCase().endsWith(".gif"); } } class JpgFileFilter extends FileFilter{ public String getDescription(){ return "jpg源文件(*.jpg)"; } public boolean accept(File file) { return file.getName().toLowerCase().endsWith(".jpg"); } } class JavaFileFilter extends FileFilter{ public String getDescription() { return "java源文件(*.java)"; } public boolean accept(File file) { return file.getName().toLowerCase().endsWith(".java"); } } class TxtFileFilter extends FileFilter{ public String getDescription() { return "文本文档(*.txt)"; } public boolean accept(File file) { return file.getName().toLowerCase().endsWith(".txt"); } } public static void main(String args[]) { java.awt.EventQueue.invokeLater(new Runnable() { public void run() { new Book().setVisible(true); } }); } // Variables declaration - do not modify//GEN-BEGIN:variables private javax.swing.JMenuItem About; private javax.swing.JMenuItem BackColor; private javax.swing.JMenuItem Copy; private javax.swing.JMenuItem Cut; private javax.swing.JMenuItem Delete; private javax.swing.JMenu Edit; private javax.swing.JMenu File; private javax.swing.JMenuItem Font; private javax.swing.JMenu Form; private javax.swing.JMenu Help; private javax.swing.JMenuItem Insert; private javax.swing.JLabel Label; private javax.swing.JMenuItem New; private javax.swing.JMenuItem Open; private javax.swing.JMenuItem Paste; private javax.swing.JMenuItem Print; private javax.swing.JMenuItem Quit; private javax.swing.JMenuItem Redo; private javax.swing.JMenuItem Save; private javax.swing.JMenuItem SaveAs; private javax.swing.JMenuItem Search; private javax.swing.JMenuItem SelectAll; private javax.swing.JMenuItem SetColor; private javax.swing.JTextArea Text; private javax.swing.JMenuItem Time; private javax.swing.JMenuItem Undo; private javax.swing.JButton bCopy; private javax.swing.JButton bCut; private javax.swing.JButton bDelete; private javax.swing.JButton bNew; private javax.swing.JButton bOpen; private javax.swing.JButton bPaste; private javax.swing.JButton bRedo; private javax.swing.JButton bSave; private javax.swing.JButton bUndo; private javax.swing.JMenuItem itemCopy; private javax.swing.JMenuItem itemCut; private javax.swing.JMenuItem itemDelete; private javax.swing.JMenuItem itemPaste; private javax.swing.JMenuItem itemRedo; private javax.swing.JMenuItem itemSelect; private javax.swing.JMenuItem itemUndo; private javax.swing.JLabel jLabel1; private javax.swing.JMenuBar jMenuBar1; private javax.swing.JPanel jPanel1; private javax.swing.JScrollPane jScrollPane1; private javax.swing.JSeparator jSeparator1; private javax.swing.JSeparator jSeparator2; private javax.swing.JSeparator jSeparator3; private javax.swing.JSeparator jSeparator4; private javax.swing.JSeparator jSeparator5; private javax.swing.JToolBar jToolBar1; private javax.swing.JToolBar jToolBar2; private javax.swing.JPopupMenu menu; // End of variables declaration//GEN-END:variables }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -