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

📄 editormessage.java~3~

📁 1. 消息即时通信 2. 消息发送实现一对一、一对多等多种发送模式 3. 发送的消息实现多彩文本编辑
💻 JAVA~3~
字号:
package chat;

import java.awt.*;
import javax.swing.*;
import javax.swing.text.*;
//import javax.swing.event.*;
import java.awt.event.*;
import javax.swing.text.html.*;
import java.awt.event.FocusEvent;

public class EditorMessage extends JPanel implements ActionListener,FocusListener{
  Font font;
  JPanel toolPanel;
  JPanel messagePanel;
  JTextPane messageTextPane;
  JScrollPane messageScrollPane;
  JComboBox fontFamilyCB;
  JComboBox fontSizeCB;
  //JButton boldButton;
  //JButton italicButton;
  //JButton underLineButton;
  JButton colorButton;
  JToggleButton boldButton;
  JToggleButton italicButton;
  JToggleButton underlineButton;
 // JToggleButton strikeThroughButton;

  JButton button;

  JColorChooser colorChooser;

  String toolbarImagePath;

  static String fontFamily;
  int fontSize;
  static boolean strikeThrough;
  static boolean bold;
  static boolean italic;
  static boolean underline;
  String[] sizes;
  static Color selectedColor=null;
  static SimpleAttributeSet sas=new SimpleAttributeSet();
  static MutableAttributeSet attr;


  public EditorMessage() {
    try {
      jbInit();
    }
    catch(Exception ex) {
      ex.printStackTrace();
    }
  }
  void jbInit() throws Exception {
    colorChooser=new JColorChooser();


    toolbarImagePath="/images/toolbar";
    font=new Font("新宋体",Font.PLAIN,12);
    sizes=new String[]{"8","10","12","14","16","18","20","22","24","30","34"};
    toolPanel=new JPanel();
    messagePanel=new JPanel();
    messageTextPane=new JTextPane();
    messageTextPane.setContentType("text/html");
    messageScrollPane=new JScrollPane(messageTextPane);
    messageScrollPane.setPreferredSize(new Dimension(600,60));

    fontFamilyCB=new JComboBox(GraphicsEnvironment.getLocalGraphicsEnvironment().
                               getAvailableFontFamilyNames());

    fontFamilyCB.setFont(font);
    fontFamilyCB.setBackground(Color.white);
    fontFamilyCB.addActionListener(new fontFamilyAction());

    fontSizeCB=new JComboBox(sizes);
    fontSizeCB.setFont(font);
    fontSizeCB.setBackground(Color.white);
    fontSizeCB.addActionListener(new fontSizeAction());


    messageTextPane.addFocusListener(this);
   // messageTextPane.a



    boldButton=new JToggleButton("粗体");
    italicButton=new JToggleButton("斜体");
    underlineButton=new JToggleButton("下划线");
    //strikeThroughButton=new JToggleButton("删除线");
    //toggleButton=new JToggleButton();


    colorButton=new JButton("选择颜色");
    colorButton.addActionListener(new colorAction());
    button=new JButton("ok");
    button.addActionListener(this);

    //boldButton.setAction(getComponent().getActionMap().get("font-bold"));
    boldButton.addActionListener(new boldAction());
    italicButton.addActionListener(new italicAction());
    underlineButton.addActionListener(new underlineAction());
    //strikeThroughButton.addActionListener(new strikeThroughAction());

    //italicButton.setAction(getComponent().getActionMap().get("font-italic"));
    //underLineButton.setAction(getComponent().getActionMap().get("font-underline"));

    toolPanel.add(fontFamilyCB);
    toolPanel.add(fontSizeCB);
    toolPanel.add(boldButton);
    toolPanel.add(italicButton);
    toolPanel.add(underlineButton);
   // toolPanel.add(strikeThroughButton);
    toolPanel.add(colorButton);
    //toolPanel.add(toggleButton);
    toolPanel.add(button);
    messagePanel.add(messageScrollPane);
    this.setLayout(new BorderLayout());
    this.add(toolPanel,BorderLayout.NORTH);
    this.add(messagePanel,BorderLayout.CENTER);
  }
  public void focusGained(FocusEvent e){

  }

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

      // 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);
    //JEditorPane editor=(JEditorPane)getComponent();
    //setCharacterAttributes(editor, 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.setVisible(true);
  }

  public void focusLost(FocusEvent focusEvent) {
    if(focusEvent.getSource()==messageTextPane){
       messageTextPane.setCharacterAttributes(sas,false);
    }
  }

  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 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,"颜色选择对话框",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);
    }
  }
}
}





























⌨️ 快捷键说明

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