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

📄 editormessage.java

📁 1. 消息即时通信 2. 消息发送实现一对一、一对多等多种发送模式 3. 发送的消息实现多彩文本编辑
💻 JAVA
📖 第 1 页 / 共 2 页
字号:

  public void actionPerformed(ActionEvent e) {
    if (e.getSource() == selectFacebutton) {

      // System.out.println("<body>".length());

      // System.out.println(getMessage());
      setMessage();
      //System.out.println(strikeThrough);

    }
  }

  public JTextComponent getComponent() {
    return this.messageTextPane;
  }

  public String getMessage() {
    String message = messageTextPane.getText();
    if (message != null) {
      return message.substring(message.indexOf("<body>") + 6,
                               message.lastIndexOf("</body>"));

    }
    else {
      return null;
    }
  }

  public void setMessage() {
    messageTextPane.setText(null);
    messageTextPane.setCharacterAttributes(sas, false);
  }

  protected void makeAction() {
    Action a;

    a = getComponent().getActionMap().get("font-bold");

    a = getComponent().getActionMap().get("font-italic");

    a = getComponent().getActionMap().get("font-underLine");
  }

  public static void main(String[] args) {
    JFrame frame = new JFrame();
    EditorMessage editorMessage = new EditorMessage();
    frame.getContentPane().add(editorMessage);
    frame.setSize(new Dimension(600, 400));
    frame.setVisible(true);
  }

  /*protected void makeActionsPretty() {
    Action a;
    a = messageTextPane.getActionMap().get(DefaultEditorKit.cutAction);
    a.putValue(Action.SMALL_ICON, cutIcon);
    a.putValue(Action.NAME, "Cut");

    a = messageTextPane.getActionMap().get(DefaultEditorKit.copyAction);
    a.putValue(Action.SMALL_ICON, new ImageIcon("icons/copy.gif"));
    a.putValue(Action.NAME, "Copy");

    a = messageTextPane.getActionMap().get(DefaultEditorKit.pasteAction);
    a.putValue(Action.SMALL_ICON, new ImageIcon("icons/paste.gif"));
    a.putValue(Action.NAME, "Paste");
     }*/


  /* public void focusLost(FocusEvent focusEvent) {
     if (focusEvent.getSource() == messageTextPane) {
       messageTextPane.setCharacterAttributes(sas, false);
     }
   }*/
  /**
   *
   * <p>Title: 键盘事件内部类</p>
   * <p>Description: </p>
   * <p>Copyright: Copyright (c) 2003</p>
   * <p>Company: </p>
   * @author not attributable
   * @version 1.0
   */
  class keyBoardAction
      implements KeyListener {
    public void keyPressed(KeyEvent e) {
      if (e.getKeyCode() == KeyEvent.VK_DELETE ||
          e.getKeyCode() == KeyEvent.VK_CANCEL) {

        messageTextPane.setCharacterAttributes(sas, true);
      }
    }

    public void keyTyped(KeyEvent e) {

    }

    public void keyReleased(KeyEvent e) {

    }
  }
  /**
   *
   * <p>Title:右键菜单内部类 </p>
   * <p>Description: </p>
   * <p>Copyright: Copyright (c) 2003</p>
   * <p>Company: </p>
   * @author not attributable
   * @version 1.0
   */
  class MousePopupListener extends MouseAdapter {
       public void mousePressed(MouseEvent e) { checkPopup(e); }
       public void mouseClicked(MouseEvent e) { checkPopup(e); }
       public void mouseReleased(MouseEvent e) { checkPopup(e); }

       private void checkPopup(MouseEvent e) {
           if (e.isPopupTrigger()) {
               popupMenu.show(messageTextPane, e.getX(), e.getY());
           }
       }
   }

   /**
    *
    * <p>Title:字体加粗内部类 </p>
    * <p>Description: </p>
    * <p>Copyright: Copyright (c) 2003</p>
    * <p>Company: </p>
    * @author not attributable
    * @version 1.0
    */
  class boldAction
      extends StyledEditorKit.StyledTextAction {
    public boldAction() {
      super(StyleConstants.Bold.toString());
    }

    public void actionPerformed(ActionEvent ae) {
      JEditorPane editor = getEditor(ae);
      //System.out.println(messageTextPane.getText());
      if (editor != null) {
        StyledEditorKit kit = getStyledEditorKit(editor);
        attr = kit.getInputAttributes();
        bold = (StyleConstants.isBold(attr)) ? false : true;
        StyleConstants.setBold(sas, bold);
        setCharacterAttributes(editor, sas, false);
      }
    }
  }

  class italicAction
      extends StyledEditorKit.StyledTextAction {
    public italicAction() {
      super(StyleConstants.Italic.toString());
    }

    public void actionPerformed(ActionEvent ae) {
      JEditorPane editor = getEditor(ae);

      if (editor != null) {
        StyledEditorKit kit = getStyledEditorKit(editor);
        attr = kit.getInputAttributes();
        italic = (StyleConstants.isItalic(attr)) ? false : true;
        StyleConstants.setItalic(sas, italic);
        setCharacterAttributes(editor, sas, false);
      }
    }
  }

  class underlineAction
      extends StyledEditorKit.StyledTextAction {
    public underlineAction() {
      super(StyleConstants.Underline.toString());
    }

    public void actionPerformed(ActionEvent ae) {
      JEditorPane editor = getEditor(ae);
      if (editor != null) {
        StyledEditorKit kit = getStyledEditorKit(editor);
        attr = kit.getInputAttributes();
        underline = (StyleConstants.isUnderline(attr)) ? false : true;
        StyleConstants.setUnderline(sas, underline);
        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);
        attr = kit.getInputAttributes();
        superscript= (StyleConstants.isSuperscript(attr)) ? false : true;
        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);
       //System.out.println(messageTextPane.getText());
       if (editor != null) {
         StyledEditorKit kit = getStyledEditorKit(editor);
         attr = kit.getInputAttributes();
         strikeThrough = (StyleConstants.isStrikeThrough(attr)) ? false : true;
         StyleConstants.setStrikeThrough(sas, strikeThrough);
         setCharacterAttributes(editor, sas, false);
       }
     }
   }*/

  class fontFamilyAction
      extends StyledEditorKit.StyledTextAction {
    public fontFamilyAction() {
      super(StyleConstants.StrikeThrough.toString());
    }

    public void actionPerformed(ActionEvent ae) {
      JEditorPane editor = getEditor(ae);
      if (editor != null) {
        JComboBox source = (JComboBox) ae.getSource();
        fontFamily = (String) source.getSelectedItem();
        StyledEditorKit kit = getStyledEditorKit(editor);
        //MutableAttributeSet attr = kit.getInputAttributes();
        //sas = new SimpleAttributeSet();
        StyleConstants.setFontFamily(sas, fontFamily);
        setCharacterAttributes(editor, sas, false);
      }
    }
  }

  class fontSizeAction
      extends StyledEditorKit.StyledTextAction {
    public fontSizeAction() {
      super(StyleConstants.StrikeThrough.toString());
    }

    public void actionPerformed(ActionEvent ae) {
      JEditorPane editor = getEditor(ae);
      if (editor != null) {
        JComboBox source = (JComboBox) ae.getSource();
        fontSize = (Integer.valueOf( (String) source.getSelectedItem())).
            intValue();
        StyledEditorKit kit = getStyledEditorKit(editor);
        //MutableAttributeSet attr = kit.getInputAttributes();
        //sas = new SimpleAttributeSet();
        StyleConstants.setFontSize(sas, fontSize);
        setCharacterAttributes(editor, sas, false);
      }
    }
  }

  class colorAction
      extends StyledEditorKit.StyledTextAction {
    public colorAction() {
      super(StyleConstants.StrikeThrough.toString());
    }

    public void actionPerformed(ActionEvent ae) {
      JEditorPane editor = getEditor(ae);
      if (editor != null) {
        Color color = colorChooser.showDialog(null,
                                              Constant.getLang("EM_ysxzdhk"),
                                              selectedColor);
        if (color != null) {
          selectedColor = color;
          StyledEditorKit kit = getStyledEditorKit(editor);
          //MutableAttributeSet attr = kit.getInputAttributes();
          // sas = new SimpleAttributeSet();
          StyleConstants.setForeground(sas, selectedColor);
          setCharacterAttributes(editor, sas, false);
        }

      }
    }
  }

  class ImageAction
      extends StyledEditorKit.StyledTextAction {
    public ImageAction() {
      super("InsertIMG");
    }

    public void actionPerformed(ActionEvent ae) {
      JButton button2 = (JButton) ae.getSource();
      selectedIcon = button2.getIcon();
      faceWindow.setVisible(false);
      //JEditorPane editor = getEditor(ae);
      JEditorPane editor = (JEditorPane) messageTextPane;
      if (editor == null) {
        return;
      }
      HTMLEditorKit kit = (HTMLEditorKit) editor.getEditorKit();
      HTMLDocument doc = (HTMLDocument) editor.getDocument();
      try {
        kit.insertHTML(doc, editor.getCaretPosition(),
                       "<img src=" + selectedIcon + ">", 0, 0,
                       HTML.Tag.IMG);
      }
      catch (Exception e) {
        e.printStackTrace();
      }
      // }
    }
  }

  public void mousePressed(MouseEvent e) {
  }

  public void mouseReleased(MouseEvent e) {
  }

  public void mouseEntered(MouseEvent e) {
  }

  public void mouseExited(MouseEvent e) {
  }

  public void mouseClicked(MouseEvent e) {
    if (e.getSource() == selectFacebutton) {
      //System.out.println(button.getLocation());
      //System.out.println(e.getX()+""+e.getY());

      faceWindow.setLocation(400, 470);
      faceWindow.setVisible(true);
      faceWindow.setSize(210, 150);
      faceWindow.show();
    }
    else {
      faceWindow.setVisible(false);
    }
  }
}

⌨️ 快捷键说明

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