<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    JAVA進行式
    讓我們把JAVA進行到底!
    posts - 13,comments - 21,trackbacks - 0

    /* HTMLDocumentEditor.java
    * @author: Charles Bell
    * @version: May 27, 2002
    */

    import java.awt.*;
    import java.awt.event.*;
    import java.io.*;

    import javax.swing.*;
    import javax.swing.event.*;
    import javax.swing.filechooser.*;
    import javax.swing.text.*;
    import javax.swing.text.html.*;
    import javax.swing.undo.*;

    /**HTML文件文檔編輯器*/
    public class HTMLDocumentEditor extends JFrame implements ActionListener
    {
     /** 聲明一個網頁文檔對象變量*/
     private HTMLDocument document;
     /** 創建一個文本編輯板*/
     private JTextPane textPane = new JTextPane();
     private boolean debug = false;
     /** 聲明一個文件對象變量*/
     private File currentFile;
     
     /** 偵聽在當前文檔上的編輯器 */
     protected UndoableEditListener undoHandler = new UndoHandler();
     
     /** 添加撤消管理器 */
     protected UndoManager undo = new UndoManager();
     
     /** 添加撤消偵聽器*/
     private UndoAction undoAction = new UndoAction();
     /** 添加恢復偵聽器*/
     private RedoAction redoAction = new RedoAction();
     
     /** 添加剪切偵聽器*/
     private Action cutAction = new DefaultEditorKit.CutAction();
     /** 添加復制偵聽器*/
     private Action copyAction = new DefaultEditorKit.CopyAction();
     /** 添加粘貼偵聽器*/
     private Action pasteAction = new DefaultEditorKit.PasteAction();
     
     /** 添加加粗偵聽器*/
     private Action boldAction = new StyledEditorKit.BoldAction();
     /** 添加加下劃線偵聽器*/
     private Action underlineAction = new StyledEditorKit.UnderlineAction();
     /** 添加傾斜偵聽器*/
     private Action italicAction = new StyledEditorKit.ItalicAction();
     
     private Action insertBreakAction = new DefaultEditorKit.InsertBreakAction();
     private HTMLEditorKit.InsertHTMLTextAction unorderedListAction = new HTMLEditorKit.InsertHTMLTextAction("Bullets", "<ul><li> </li></ul>",HTML.Tag.P,HTML.Tag.UL);
     private HTMLEditorKit.InsertHTMLTextAction bulletAction = new HTMLEditorKit.InsertHTMLTextAction("Bullets", "<li> </li>",HTML.Tag.UL,HTML.Tag.LI);
     
     /** 構造方法*/
     public HTMLDocumentEditor()
     {
      /** 設置主窗體標題*/
      super("HTMLDocumentEditor");
      HTMLEditorKit editorKit = new HTMLEditorKit();
      /** 創建默認文檔指向網頁引用document*/
      document = (HTMLDocument)editorKit.createDefaultDocument();
      
      // 強制SWINGSET實現跨平臺,不改變風格
      try
      {
       UIManager.setLookAndFeel(UIManager.getCrossPlatformLookAndFeelClassName());
       // 如果你想用系統的界面風格替代,請注釋掉上一行代碼,而取消下一行代碼的注釋:
       //UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
      }
      catch (Exception exc)
      {
       //產生異常,則顯示錯誤消息:加載L&F錯誤
       System.err.println("Error loading L&F: " + exc);
      }
      
      //調用初始化方法
      init();
     }
     
     /**主方法,起動程序*/
     public static void main(String[] args)
     {
      //創建一個類的實例,即創建一個網頁編輯器
      HTMLDocumentEditor editor = new HTMLDocumentEditor();
     }
     
     /**初始化各組件的方法*/
     public void init()
     {
      //調用自定義繼承WindowListener的偵聽器FrameListener,給主窗體添加WindowListener
      addWindowListener(new FrameListener());
      
      JMenuBar menuBar = new JMenuBar();
      getContentPane().add(menuBar, BorderLayout.NORTH);
      JMenu fileMenu = new JMenu("File");  //文件
      JMenu editMenu = new JMenu("Edit");  //編輯
      JMenu colorMenu = new JMenu("Color");  //顏色
      JMenu fontMenu = new JMenu("Font");  //字體
      JMenu styleMenu = new JMenu("Style");  //樣式
      JMenu alignMenu = new JMenu("Align");  //對齊
      JMenu helpMenu = new JMenu("Help");  //幫助
      
      menuBar.add(fileMenu);
      menuBar.add(editMenu);
      menuBar.add(colorMenu);
      menuBar.add(fontMenu);
      menuBar.add(styleMenu);
      menuBar.add(alignMenu);
      menuBar.add(helpMenu);
      
      JMenuItem newItem = new JMenuItem("New", new ImageIcon("whatsnew-bang.gif"));  //新建
      JMenuItem openItem = new JMenuItem("Open",new ImageIcon("open.gif"));    //打開
      JMenuItem saveItem = new JMenuItem("Save",new ImageIcon("save.gif"));    //保存
      JMenuItem saveAsItem = new JMenuItem("Save As");         //另存
      JMenuItem exitItem = new JMenuItem("Exit",new ImageIcon("exit.gif"));    //退出
      
      newItem.addActionListener(this);
      openItem.addActionListener(this);
      saveItem.addActionListener(this);
      saveAsItem.addActionListener(this);
      exitItem.addActionListener(this);
      
      fileMenu.add(newItem);
      fileMenu.add(openItem);
      fileMenu.add(saveItem);
      fileMenu.add(saveAsItem);
      fileMenu.add(exitItem);
      
      //給菜單項添加偵聽器
      JMenuItem undoItem = new JMenuItem(undoAction);      //撤消
      JMenuItem redoItem = new JMenuItem(redoAction);      //恢復
      JMenuItem cutItem = new JMenuItem(cutAction);       //剪切
      JMenuItem copyItem = new JMenuItem(copyAction);      //復制
      JMenuItem pasteItem = new JMenuItem(pasteAction);      //粘貼
      JMenuItem clearItem = new JMenuItem("Clear");       //清除
      JMenuItem selectAllItem = new JMenuItem("Select All");     //全選
      JMenuItem insertBreaKItem = new JMenuItem(insertBreakAction);   
      JMenuItem unorderedListItem = new JMenuItem(unorderedListAction);
      JMenuItem bulletItem = new JMenuItem(bulletAction);     //項目符號
      
      cutItem.setText("Cut");
      copyItem.setText("Copy");
      pasteItem.setText("Paste");
      insertBreaKItem.setText("Break");
      cutItem.setIcon(new ImageIcon("cut.gif"));
      copyItem.setIcon(new ImageIcon("copy.gif"));
      pasteItem.setIcon(new ImageIcon("paste.gif"));
      insertBreaKItem.setIcon(new ImageIcon("break.gif"));
      unorderedListItem.setIcon(new ImageIcon("bullets.gif"));
      
      clearItem.addActionListener(this);
      selectAllItem.addActionListener(this);
      
      editMenu.add(undoItem);
      editMenu.add(redoItem);
      editMenu.add(cutItem);
      editMenu.add(copyItem);
      editMenu.add(pasteItem);
      editMenu.add(clearItem);
      editMenu.add(selectAllItem);
      editMenu.add(insertBreaKItem);
      editMenu.add(unorderedListItem);
      editMenu.add(bulletItem);
      
      JMenuItem redTextItem = new JMenuItem(new StyledEditorKit.ForegroundAction("Red",Color.red));
      JMenuItem orangeTextItem = new JMenuItem(new StyledEditorKit.ForegroundAction("Orange",Color.orange));
      JMenuItem yellowTextItem = new JMenuItem(new StyledEditorKit.ForegroundAction("Yellow",Color.yellow));
      JMenuItem greenTextItem = new JMenuItem(new StyledEditorKit.ForegroundAction("Green",Color.green));
      JMenuItem blueTextItem = new JMenuItem(new StyledEditorKit.ForegroundAction("Blue",Color.blue));
      JMenuItem cyanTextItem = new JMenuItem(new StyledEditorKit.ForegroundAction("Cyan",Color.cyan));
      JMenuItem magentaTextItem = new JMenuItem(new StyledEditorKit.ForegroundAction("Magenta",Color.magenta));
      JMenuItem blackTextItem = new JMenuItem(new StyledEditorKit.ForegroundAction("Black",Color.black));
      
      redTextItem.setIcon(new ImageIcon("red.gif"));
      orangeTextItem.setIcon(new ImageIcon("orange.gif"));
      yellowTextItem.setIcon(new ImageIcon("yellow.gif"));
      greenTextItem.setIcon(new ImageIcon("green.gif"));
      blueTextItem.setIcon(new ImageIcon("blue.gif"));
      cyanTextItem.setIcon(new ImageIcon("cyan.gif"));
      magentaTextItem.setIcon(new ImageIcon("magenta.gif"));
      blackTextItem.setIcon(new ImageIcon("black.gif"));
      
      colorMenu.add(redTextItem);
      colorMenu.add(orangeTextItem);
      colorMenu.add(yellowTextItem);
      colorMenu.add(greenTextItem);
      colorMenu.add(blueTextItem);
      colorMenu.add(cyanTextItem);
      colorMenu.add(magentaTextItem);
      colorMenu.add(blackTextItem);
      
      JMenu fontTypeMenu = new JMenu("Font Type");
      fontMenu.add(fontTypeMenu);
      
      String[] fontTypes = {"SansSerif", "Serif", "Monospaced", "Dialog", "DialogInput"};
      for (int i = 0; i < fontTypes.length;i++)
      {
       if (debug) System.out.println(fontTypes[i]);
       JMenuItem nextTypeItem = new JMenuItem(fontTypes[i]);
       nextTypeItem.setAction(new StyledEditorKit.FontFamilyAction(fontTypes[i], fontTypes[i]));
       fontTypeMenu.add(nextTypeItem);
      }
     
      JMenu fontSizeMenu = new JMenu("Font Size");
      fontMenu.add(fontSizeMenu);
      
      int[] fontSizes = {6, 8,10,12,14, 16, 20,24, 32,36,48,72};
      for (int i = 0; i < fontSizes.length;i++)
      {
       if (debug) System.out.println(fontSizes[i]);
       JMenuItem nextSizeItem = new JMenuItem(String.valueOf(fontSizes[i]));
       nextSizeItem.setAction(new StyledEditorKit.FontSizeAction(String.valueOf(fontSizes[i]), fontSizes[i]));
       fontSizeMenu.add(nextSizeItem);
      }
      
      
      JMenuItem boldMenuItem = new JMenuItem(boldAction);
      JMenuItem underlineMenuItem = new JMenuItem(underlineAction);
      JMenuItem italicMenuItem = new JMenuItem(italicAction);
     
      boldMenuItem.setText("Bold");
      underlineMenuItem.setText("Underline");
      italicMenuItem.setText("Italic");
      
      boldMenuItem.setIcon(new ImageIcon("bold.gif"));
      underlineMenuItem.setIcon(new ImageIcon("underline.gif"));
      italicMenuItem.setIcon(new ImageIcon("italic.gif"));
      
      styleMenu.add(boldMenuItem);
      styleMenu.add(underlineMenuItem);
      styleMenu.add(italicMenuItem);
      
      JMenuItem subscriptMenuItem = new JMenuItem(new SubscriptAction());
      JMenuItem superscriptMenuItem = new JMenuItem(new SuperscriptAction());
      JMenuItem strikeThroughMenuItem = new JMenuItem(new StrikeThroughAction());
      
      subscriptMenuItem.setText("Subscript");
      superscriptMenuItem.setText("Superscript");
      strikeThroughMenuItem.setText("StrikeThrough");
      
      subscriptMenuItem.setIcon(new ImageIcon("subscript.gif"));
      superscriptMenuItem.setIcon(new ImageIcon("superscript.gif"));
      strikeThroughMenuItem.setIcon(new ImageIcon("strikethough.gif"));
      
      styleMenu.add(subscriptMenuItem);
      styleMenu.add(superscriptMenuItem);
      styleMenu.add(strikeThroughMenuItem);
      
      JMenuItem leftAlignMenuItem = new JMenuItem(new StyledEditorKit.AlignmentAction("Left Align",StyleConstants.ALIGN_LEFT));
      JMenuItem centerMenuItem = new JMenuItem(new StyledEditorKit.AlignmentAction("Center",StyleConstants.ALIGN_CENTER));
      JMenuItem rightAlignMenuItem = new JMenuItem(new StyledEditorKit.AlignmentAction ("Right Align",StyleConstants.ALIGN_RIGHT));
      
      leftAlignMenuItem.setText("Left Align");
      centerMenuItem.setText("Center");
      rightAlignMenuItem.setText("Right Align");
      
      leftAlignMenuItem.setIcon(new ImageIcon("left.gif"));
      centerMenuItem.setIcon(new ImageIcon("center.gif"));
      rightAlignMenuItem.setIcon(new ImageIcon("right.gif"));
      
      alignMenu.add(leftAlignMenuItem);
      alignMenu.add(centerMenuItem);
      alignMenu.add(rightAlignMenuItem);
      
      JMenuItem helpItem = new JMenuItem("幫助");
      helpItem.addActionListener(this);
      helpMenu.add(helpItem);
      
      JMenuItem shortcutsItem = new JMenuItem("Keyboard Shortcuts");
      shortcutsItem.addActionListener(this);
      helpMenu.add(shortcutsItem);
      
      JMenuItem aboutItem = new JMenuItem("About QuantumHyperSpace");
      aboutItem.addActionListener(this);
      helpMenu.add(aboutItem);
      
      
      JPanel editorControlPanel = new JPanel();
      //editorControlPanel.setLayout(new GridLayout(3,3));
      editorControlPanel.setLayout(new FlowLayout());
      
      /* 按鈕 */
      JButton cutButton = new JButton(cutAction);    //創建“剪切”按鈕,添加剪切偵聽
      JButton copyButton = new JButton(copyAction);    //創建“復制”按鈕,添加復制偵
      JButton pasteButton = new JButton(pasteAction);   //創建“粘貼”按鈕,添加粘貼偵
      
      JButton boldButton = new JButton(boldAction);    //創建“加粗”按鈕,添加加粗偵聽
      JButton underlineButton = new JButton(underlineAction); //創建“下劃線”按鈕,添加下劃線偵聽
      JButton italicButton = new JButton(italicAction);   //創建“斜體”按鈕,添加斜體偵聽
      
      
      //JButton insertButton = new JButton(insertAction);
      //JButton insertBreakButton = new JButton(insertBreakAction);
      //JButton tabButton = new JButton(tabAction);
      
      cutButton.setText("Cut");
      copyButton.setText("Copy");
      pasteButton.setText("Paste");
      
      boldButton.setText("Bold");
      underlineButton.setText("Underline");
      italicButton.setText("Italic");
      
      //insertButton.setText("Insert");
      //insertBreakButton.setText("Insert Break");
      //tabButton.setText("Tab");
      
      cutButton.setIcon(new ImageIcon("cut.gif"));
      copyButton.setIcon(new ImageIcon("copy.gif"));
      pasteButton.setIcon(new ImageIcon("paste.gif"));
      
      boldButton.setIcon(new ImageIcon("bold.gif"));
      underlineButton.setIcon(new ImageIcon("underline.gif"));
      italicButton.setIcon(new ImageIcon("italic.gif"));
      
      editorControlPanel.add(cutButton);
      editorControlPanel.add(copyButton);
      editorControlPanel.add(pasteButton);
      
      editorControlPanel.add(boldButton);
      editorControlPanel.add(underlineButton);
      editorControlPanel.add(italicButton);
      
      
      //editorControlPanel.add(insertButton);
      //editorControlPanel.add(insertBreakButton);
      //editorControlPanel.add(tabButton);
      
      JButton subscriptButton = new JButton(new SubscriptAction());
      JButton superscriptButton = new JButton(new SuperscriptAction());
      JButton strikeThroughButton = new JButton(new StrikeThroughAction());
      
      subscriptButton.setIcon(new ImageIcon("subscript.gif"));
      superscriptButton.setIcon(new ImageIcon("superscript.gif"));
      strikeThroughButton.setIcon(new ImageIcon("strikethough.gif"));
      
      
      JPanel specialPanel = new JPanel();
      specialPanel.setLayout(new FlowLayout());
      
      specialPanel.add(subscriptButton);
      specialPanel.add(superscriptButton);
      specialPanel.add(strikeThroughButton);
      
      //JButton leftAlignButton = new JButton(new AlignLeftAction());
      //JButton centerButton = new JButton(new CenterAction());
      //JButton rightAlignButton = new JButton(new AlignRightAction());
      
      JButton leftAlignButton = new JButton(new StyledEditorKit.AlignmentAction("Left Align",StyleConstants.ALIGN_LEFT));
      JButton centerButton = new JButton(new StyledEditorKit.AlignmentAction("Center",StyleConstants.ALIGN_CENTER));
      JButton rightAlignButton = new JButton(new StyledEditorKit.AlignmentAction ("Right Align",StyleConstants.ALIGN_RIGHT));
      JButton colorButton = new JButton(new StyledEditorKit.AlignmentAction ("Right Align",StyleConstants.ALIGN_RIGHT));
      
      leftAlignButton.setIcon(new ImageIcon("left.gif"));
      centerButton.setIcon(new ImageIcon("center.gif"));
      rightAlignButton.setIcon(new ImageIcon("right.gif"));
      colorButton.setIcon(new ImageIcon("color.gif"));
      
      leftAlignButton.setText("Left Align");
      centerButton.setText("Center");
      rightAlignButton.setText("Right Align");
      
      JPanel alignPanel = new JPanel();
      alignPanel.setLayout(new FlowLayout());
      alignPanel.add(leftAlignButton);
      alignPanel.add(centerButton);
      alignPanel.add(rightAlignButton);
      
      document.addUndoableEditListener(undoHandler);
      resetUndoManager();
      
      textPane = new JTextPane(document);
      textPane.setContentType("text/html");
      JScrollPane scrollPane = new JScrollPane(textPane);
      Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
      Dimension scrollPaneSize = new Dimension(5*screenSize.width/8,5*screenSize.height/8);
      scrollPane.setPreferredSize(scrollPaneSize);
      
      //創建工具欄面板,并設置面板布局管理器,添加子面板
      JPanel toolPanel = new JPanel();
      toolPanel.setLayout(new BorderLayout());
      toolPanel.add(editorControlPanel, BorderLayout.NORTH);
      toolPanel.add(specialPanel, BorderLayout.CENTER);
      toolPanel.add(alignPanel, BorderLayout.SOUTH);
      
      //向主窗體添加菜單欄
      getContentPane().add(menuBar, BorderLayout.NORTH);
      //向主窗體添加工具欄
      getContentPane().add(toolPanel, BorderLayout.CENTER);
      //向主窗體添加滾動面板
      getContentPane().add(scrollPane, BorderLayout.SOUTH);
      pack();
      setLocationRelativeTo(null);
      startNewDocument();
      show();
     }
     
     public void actionPerformed(ActionEvent ae)
     {
      String actionCommand = ae.getActionCommand();
      if (debug)
      {
       int modifier = ae.getModifiers();
       long when = ae.getWhen();
       String parameter = ae.paramString();
       System.out.println("actionCommand: " + actionCommand);
       System.out.println("modifier: " + modifier);
       System.out.println("when: " + when);
       System.out.println("parameter: " + parameter);
      }
      if (actionCommand.compareTo("New") == 0)
      {
       startNewDocument();
      }
      else if (actionCommand.compareTo("Open") == 0)
      {
       openDocument();
      }
      else if (actionCommand.compareTo("Save") == 0)
      {
       saveDocument();
      }
      else if (actionCommand.compareTo("Save As") == 0)
      {
       saveDocumentAs();
      }
      else if (actionCommand.compareTo("Exit") == 0)
      {
       exit();
      }
      else if (actionCommand.compareTo("Clear") == 0)
      {
       clear();
      }
      else if (actionCommand.compareTo("Select All") == 0)
      {
       selectAll();
      }
      else if (actionCommand.compareTo("幫助") == 0)
      {
       help();
      }
      else if (actionCommand.compareTo("Keyboard Shortcuts") == 0)
      {
       showShortcuts();
      }
      else if (actionCommand.compareTo("About QuantumHyperSpace") == 0)
      {
       aboutQuantumHyperSpace();
      }
     }
     
     public void startNewDocument()
     {
      Document oldDoc = textPane.getDocument();
      if(oldDoc != null)
      oldDoc.removeUndoableEditListener(undoHandler);
      HTMLEditorKit editorKit = new HTMLEditorKit();
      document = (HTMLDocument)editorKit.createDefaultDocument();
      textPane.setDocument(document);
      currentFile = null;
      setTitle("HTMLDocumentEditor");
      textPane.getDocument().addUndoableEditListener(undoHandler);
      resetUndoManager();
     }
     
     public void openDocument()
     {
      try
      {
       File current = new File(".");
       JFileChooser chooser = new JFileChooser(current);
       chooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
       chooser.setFileFilter(new HTMLFileFilter());
       int approval = chooser.showSaveDialog(this);
       if (approval == JFileChooser.APPROVE_OPTION)
       {
        currentFile = chooser.getSelectedFile();
        setTitle(currentFile.getName());
        FileReader fr = new FileReader(currentFile);
        Document oldDoc = textPane.getDocument();
        if(oldDoc != null)
        oldDoc.removeUndoableEditListener(undoHandler);
        HTMLEditorKit editorKit = new HTMLEditorKit();
        document = (HTMLDocument)editorKit.createDefaultDocument();
        editorKit.read(fr,document,0);
        document.addUndoableEditListener(undoHandler);
        textPane.setDocument(document);
        resetUndoManager();
       }
      }
      catch(BadLocationException ble)
      {
       System.err.println("BadLocationException: " + ble.getMessage());
      }
      catch(FileNotFoundException fnfe)
      {
       System.err.println("FileNotFoundException: " + fnfe.getMessage());
      }
      catch(IOException ioe)
      {
       System.err.println("IOException: " + ioe.getMessage());
      }
     }
     
     public void saveDocument()
     {
      if (currentFile != null)
      {
       try
       {
        FileWriter fw = new FileWriter(currentFile);
        fw.write(textPane.getText());
        fw.close();
       }
       catch(FileNotFoundException fnfe)
       {
        System.err.println("FileNotFoundException: " + fnfe.getMessage());
       }
       catch(IOException ioe)
       {
        System.err.println("IOException: " + ioe.getMessage());
       }
      }
      else
      {
       saveDocumentAs();
      }
     }
     
     public void saveDocumentAs()
     {
      try
      {
       File current = new File(".");
       JFileChooser chooser = new JFileChooser(current);
       chooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
       chooser.setFileFilter(new HTMLFileFilter());
       int approval = chooser.showSaveDialog(this);
       if (approval == JFileChooser.APPROVE_OPTION)
       {
        File newFile = chooser.getSelectedFile();
        if (newFile.exists())
        {
         String message = newFile.getAbsolutePath() + " already exists. \n" + "Do you want to replace it?";
         if (JOptionPane.showConfirmDialog(this, message) == JOptionPane.YES_OPTION)
         {
          currentFile = newFile;
          setTitle(currentFile.getName());
          FileWriter fw = new FileWriter(currentFile);
          fw.write(textPane.getText());
          fw.close();
          if (debug) System.out.println("Saved " + currentFile.getAbsolutePath());
         }
        }
        else
        {
         currentFile = new File(newFile.getAbsolutePath());
         setTitle(currentFile.getName());
         FileWriter fw = new FileWriter(currentFile);
         fw.write(textPane.getText());
         fw.close();
         if (debug) System.out.println("Saved " + currentFile.getAbsolutePath());
        }
       }
      }
      catch(FileNotFoundException fnfe)
      {
       System.err.println("FileNotFoundException: " + fnfe.getMessage());
      }
      catch(IOException ioe)
      {
       System.err.println("IOException: " + ioe.getMessage());
      }
     }
     
     public void exit()
     {
      String exitMessage = "Are you sure you want to exit?";
      if (JOptionPane.showConfirmDialog(this, exitMessage) == JOptionPane.YES_OPTION)
      {
       System.exit(0);
      }
     }
     
     /**調用startNewDocument()方法,清除當前文本,開始一個新文檔*/
     public void clear()
     {
      startNewDocument();
     }
     
     /**調用JTextPane的全選方法*/
     public void selectAll()
     {
      textPane.selectAll();
     }
     
     /**利用消息框顯示幫助信息*/
     public void help()
     {
      JOptionPane.showMessageDialog(this,"DocumentEditor.java\n" + "Author: Charles Bell\n" + "Version: May 25, 2002\n" +
               " }
     
     /**利用消息框顯示快捷鍵*/
     public void showShortcuts()
     {
      String shortcuts = "Navigate in | Tab\n" + "Navigate out | Ctrl+Tab\n" + "Navigate out backwards | Shift+Ctrl+Tab\n" +
      "Move up/down a line | Up/Down Arrown\n" + "Move left/right a component or char | Left/Right Arrow\n" +
      "Move up/down one vertical block | PgUp/PgDn\n" + "Move to start/end of line | Home/End\n" +
      "Move to previous/next word | Ctrl+Left/Right Arrow\n" + "Move to start/end of data | Ctrl+Home/End\n" +
      "Move left/right one block | Ctrl+PgUp/PgDn\n" + "Select All | Ctrl+A\n" +
      "Extend selection up one line | Shift+Up Arrow\n" + "Extend selection down one line | Shift+Down Arrow\n" +
      "Extend selection to beginning of line | Shift+Home\n" + "Extend selection to end of line | Shift+End\n" +
      "Extend selection to beginning of data | Ctrl+Shift+Home\n" + "Extend selection to end of data | Ctrl+Shift+End\n" +
      "Extend selection left | Shift+Right Arrow\n" + "Extend selection right | Shift+Right Arrow\n" +
      "Extend selection up one vertical block | Shift+PgUp\n" + "Extend selection down one vertical block | Shift+PgDn\n" +
      "Extend selection left one block | Ctrl+Shift+PgUp\n" + "Extend selection right one block | Ctrl+Shift+PgDn\n" +
      "Extend selection left one word | Ctrl+Shift+Left Arrow\n" + "Extend selection right one word | Ctrl+Shift+Right Arrow\n";
      JOptionPane.showMessageDialog(this,shortcuts);
     }
     
     public void aboutQuantumHyperSpace()
     {
      JOptionPane.showMessageDialog(this,"QuantumHyperSpace Programming Services\n" + "
      "email: support@quantumhyperspace.com\n" +  " or \n" + "email: charles@quantumhyperspace.com\n",
      "QuantumHyperSpace",JOptionPane.INFORMATION_MESSAGE, new ImageIcon("quantumhyperspace.gif"));
     }
     
     /**內部類:自定義繼承WindowListener的偵聽器FrameListener*/
     class FrameListener extends WindowAdapter
     {
      /**處理點擊窗體關閉按鈕事件,實現程序的關閉停止*/
      public void windowClosing(WindowEvent we)
      {
       exit();
      }
     }
     
     class SubscriptAction extends StyledEditorKit.StyledTextAction
     {
     
      public SubscriptAction()
      {
       super(StyleConstants.Subscript.toString());
      }
      public void actionPerformed(ActionEvent ae)
      {
       JEditorPane editor = getEditor(ae);
       if (editor != null)
       {
        StyledEditorKit kit = getStyledEditorKit(editor);
        MutableAttributeSet attr = kit.getInputAttributes();
        boolean subscript = (StyleConstants.isSubscript(attr)) ? false : true;
        SimpleAttributeSet sas = new SimpleAttributeSet();
        StyleConstants.setSubscript(sas, subscript);
        setCharacterAttributes(editor, sas, false);
       }
      }
     }

     class SuperscriptAction extends StyledEditorKit.StyledTextAction
     {
     
      public SuperscriptAction()
      {
       super(StyleConstants.Superscript.toString());
      }
      public void actionPerformed(ActionEvent ae)
      {
       JEditorPane editor = getEditor(ae);
       if (editor != null)
       {
        StyledEditorKit kit = getStyledEditorKit(editor);
        MutableAttributeSet attr = kit.getInputAttributes();
        boolean superscript = (StyleConstants.isSuperscript(attr)) ? false : true;
        SimpleAttributeSet sas = new SimpleAttributeSet();
        StyleConstants.setSuperscript(sas, superscript);
        setCharacterAttributes(editor, sas, false);
       }
      }
     }

     class StrikeThroughAction extends StyledEditorKit.StyledTextAction
     {
      public StrikeThroughAction()
      {
       super(StyleConstants.StrikeThrough.toString());
      }
      
      public void actionPerformed(ActionEvent ae)
      {
       JEditorPane editor = getEditor(ae);
       if (editor != null)
       {
        StyledEditorKit kit = getStyledEditorKit(editor);
        MutableAttributeSet attr = kit.getInputAttributes();
        boolean strikeThrough = (StyleConstants.isStrikeThrough(attr)) ? false : true;
        SimpleAttributeSet sas = new SimpleAttributeSet();
        StyleConstants.setStrikeThrough(sas, strikeThrough);
        setCharacterAttributes(editor, sas, false);
       }
      }
     }

     class HTMLFileFilter extends javax.swing.filechooser.FileFilter
     {
      public boolean accept(File f)
      {
       return ((f.isDirectory()) ||(f.getName().toLowerCase().indexOf(".htm") > 0));
      }
      
      public String getDescription()
      {
       return "html";
      }
     }
     
     protected void resetUndoManager()
     {
      undo.discardAllEdits();
      undoAction.update();
      redoAction.update();
     }

     class UndoHandler implements UndoableEditListener
     {
      /**
      * Messaged when the Document has created an edit, the edit is
      * added to <code>undo</code>, an instance of UndoManager.
      */
      public void undoableEditHappened(UndoableEditEvent e)
      {
       undo.addEdit(e.getEdit());
       undoAction.update();
       redoAction.update();
      }
     }

     class UndoAction extends AbstractAction
     {
      public UndoAction()
      {
       super("Undo");
       setEnabled(false);
      }
      
      public void actionPerformed(ActionEvent e)
      {
       try
       {
        undo.undo();
       }
       catch (CannotUndoException ex)
       {
        System.out.println("Unable to undo: " + ex);
        ex.printStackTrace();
       }
       update();
       redoAction.update();
      }
     
      protected void update()
      {
       if(undo.canUndo())
       {
        setEnabled(true);
        putValue(Action.NAME, undo.getUndoPresentationName());
       }
       else
       {
        setEnabled(false);
        putValue(Action.NAME, "Undo");
       }
      }
     }

     class RedoAction extends AbstractAction
     {
     
      public RedoAction()
      {
       super("Redo");
       setEnabled(false);
      }
      
      public void actionPerformed(ActionEvent e)
      {
       try
       {
        undo.redo();
       }
       catch (CannotRedoException ex)
       {
        System.err.println("Unable to redo: " + ex);
        ex.printStackTrace();
       }
       update();
       undoAction.update();
      }
      
      protected void update()
      {
       if(undo.canRedo())
       {
        setEnabled(true);
        putValue(Action.NAME, undo.getRedoPresentationName());
       }
       else
       {
        setEnabled(false);
        putValue(Action.NAME, "Redo");
       }
      }
     }
    }

    posted on 2005-11-29 22:10 水秀清靈 閱讀(1560) 評論(1)  編輯  收藏 所屬分類: 傳遞經典

    FeedBack:
    # re: 操作JTextPane對象的代碼實例
    2007-04-05 10:33 | 千年老二
    能夠讓關鍵字自動變色嗎?  回復  更多評論
      
    主站蜘蛛池模板: 免费人成黄页在线观看日本| 美女被爆羞羞网站免费| 中国一级全黄的免费观看| 亚洲精品麻豆av| 一个人看的www视频免费在线观看 一个人看的免费观看日本视频www | 久久永久免费人妻精品下载| 久久夜色精品国产嚕嚕亚洲av| 在线看片免费人成视频福利| 亚洲2022国产成人精品无码区| 18未年禁止免费观看| 亚洲啪啪免费视频| 日韩成人在线免费视频| 国产成人亚洲综合a∨| 亚洲一区二区三区在线播放| CAOPORN国产精品免费视频| 久久夜色精品国产亚洲| xx视频在线永久免费观看| 亚洲欧洲日韩国产一区二区三区| 在线观看视频免费国语| 日本一区二区在线免费观看| 国产亚洲免费的视频看| 人与禽交免费网站视频| 亚洲GV天堂无码男同在线观看| 亚洲第一区精品日韩在线播放| 两个人日本免费完整版在线观看1| 国产aⅴ无码专区亚洲av| 久久久久久精品免费免费自慰| 亚洲sm另类一区二区三区| 久久亚洲中文字幕精品一区| 久久九九AV免费精品| 亚洲中文字幕无码久久| 亚洲中文字幕视频国产| 亚洲视频免费播放| 最新亚洲人成网站在线观看| 亚洲AV无码久久精品蜜桃| 午夜dj免费在线观看| a级在线观看免费| 亚洲日本一线产区和二线| 国产亚洲一区二区精品| 好吊妞视频免费视频| 免费观看91视频|