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

📄 editormessage.java

📁 1. 消息即时通信 2. 消息发送实现一对一、一对多等多种发送模式 3. 发送的消息实现多彩文本编辑
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
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;
//import com.borland.jbcl.layout.*;
/**
 *
 * <p>Title:消息编辑器 </p>
 * <p>Description:实现html消息编辑器功能 </p>
 * <p>Copyright: Copyright (c) 2003</p>
 * <p>Company: </p>
 * @author not attributable
 * @version 1.0
 */

public class EditorMessage extends JPanel implements ActionListener,MouseListener{

  //FaceWindow faceWindow;

  //表情面板

  JWindow faceWindow;
  JPanel facePanel;
  JButton[] faceButton = new JButton[35];
  String facePath;
  ImageIcon icon;
  private Icon selectedIcon;

/////////////////////////////////////////

  Font font;
  Color bgcolor;
  Color fgcolor;
  Color facePanelColor;
  ImagePanel toolPanel;
  ImagePanel messagePanel;
  //JPanel rootPanel;
  JTextPane messageTextPane;
  JScrollPane messageScrollPane;
  JComboBox fontFamilyCB;
  JComboBox fontSizeCB;
  //JButton boldButton;
  //JButton italicButton;
  //JButton underLineButton;
  JButton colorButton;
  JButton boldButton;
  JButton italicButton;
  JButton underlineButton;
  JButton superscriptButton;
  JButton cutButton;
  JButton copyButton;
  JButton pasteButton;
  // JToggleButton strikeThroughButton;

  JButton selectFacebutton;

  JColorChooser colorChooser;

  JPopupMenu popupMenu;
  JMenuItem cutMenuItem;
  JMenuItem copyMenuItem;
  JMenuItem pasteMenuItem;

  String toolbarImagePath;
  ImageIcon boldIcon;
  ImageIcon italicIcon;
  ImageIcon underlineIcon;
  ImageIcon superscriptIcon;
  ImageIcon colorIcon;

  ImageIcon faceIcon;
  ImageIcon cutIcon;
  ImageIcon copyIcon;
  ImageIcon pasteIcon;

  Image toolbarImage;
  Image editormessageImage;

  static String fontFamily;
  int fontSize;
  static boolean strikeThrough;
  static boolean bold;
  static boolean italic;
  static boolean underline;
  static boolean superscript;
  String[] sizes;
  static Color selectedColor = null;

  //Color bgcolor=new Color(190,212,255);
  static SimpleAttributeSet sas = new SimpleAttributeSet();
  static MutableAttributeSet attr;

  public EditorMessage() {
    try {
      jbInit();
    }
    catch (Exception ex) {
      ex.printStackTrace();
    }
  }

  void jbInit() throws Exception {


    this.addMouseListener(this);

    colorChooser = new JColorChooser();
    font = new Font("新宋体", Font.PLAIN, 12);
    bgcolor = new Color(190, 212, 255);
    fgcolor = new Color(0, 102, 204);
    facePanelColor = new Color(1, 87, 173);

    UIManager.put("OptionPane.messageFont",font);
    UIManager.put("OptionPane.buttonFont",font);
    UIManager.put("TabbedPane.font",font);
    UIManager.put("Button.font",font);


    faceWindow = new JWindow(new JFrame());
    facePath = "/images/face/";
    facePanel = new JPanel();
    facePanel.setLayout(new GridLayout(5, 7));
    facePanel.setBackground(bgcolor);
    facePanel.setBorder(BorderFactory.createLineBorder(facePanelColor, 3));
    for (int i = 0; i < 35; i++) {
      icon = new ImageIcon(getClass().getResource(facePath + i + ".gif"));
      //System.out.println(icon);
      faceButton[i] = new JButton(icon);
      faceButton[i].setPreferredSize(new Dimension(30, 30));
      faceButton[i].setRequestFocusEnabled(false);
      faceButton[i].setContentAreaFilled(false);
      //faceButton[i].setBackground(bgcolor);
      faceButton[i].setBorder(BorderFactory.createLineBorder(Color.white));
      facePanel.add(faceButton[i]);
      faceButton[i].addActionListener(new ImageAction());
    }

    toolbarImagePath = "/images/toolbar/";
    boldIcon = new ImageIcon(getClass().getResource(toolbarImagePath +
        "bold.png"));
    italicIcon = new ImageIcon(getClass().getResource(toolbarImagePath +
        "italic.png"));
    underlineIcon = new ImageIcon(getClass().getResource(toolbarImagePath +
        "underline.png"));
    colorIcon = new ImageIcon(getClass().getResource(toolbarImagePath +
        "color.png"));
    faceIcon = new ImageIcon(getClass().getResource(toolbarImagePath +
        "face.png"));
    cutIcon = new ImageIcon(getClass().getResource(toolbarImagePath + "cut.gif"));
    copyIcon = new ImageIcon(getClass().getResource(toolbarImagePath +
        "copy.gif"));
    pasteIcon = new ImageIcon(getClass().getResource(toolbarImagePath +
        "paste.gif"));

    toolbarImage = Toolkit.getDefaultToolkit().getImage(getClass().getResource(
        toolbarImagePath + "toolbar.png"));
    editormessageImage = Toolkit.getDefaultToolkit().getImage(getClass().
        getResource(toolbarImagePath + "editormessage.png"));

    sizes = new String[] {
        "8", "10", "12", "14", "16", "18", "20", "22", "24", "30", "34"};
    toolPanel = new ImagePanel();
    toolPanel.setImage(toolbarImage);
    //toolPanel.setBackground(new Color(102,153,255));
    //Image i=Toolkit.getDefaultToolkit().getImage("rootimage.png");
    //toolPanel.setImage(i);
    //this.setImage(i);
    messagePanel = new ImagePanel();
    messagePanel.setImage(editormessageImage);
    //messagePanel.setBackground(new Color(102,153,255));
    //messagePanel.setBackground(new Color(134,208,130));
    //rootPanel=new JPanel();
    //rootPanel.setBackground(new Color(134,208,130));

    messageTextPane = new JTextPane();
    messageTextPane.setContentType("text/html");

    //messageTextPane.setBorder(new OvalBorder(5,5,Color.black,Color.black));
    messageTextPane.addKeyListener(new keyBoardAction());
    messageTextPane.setBorder(null);
    messageTextPane.addMouseListener(this);
    messageScrollPane = new JScrollPane(messageTextPane);
    messageScrollPane.setPreferredSize(new Dimension(480, 60));
    messageScrollPane.setBorder(null);
    //messageScrollPane.setBackground(new Color(134,208,130));
    // messageScrollPane.setBorder(new OvalBorder(5,5,new Color(134,208,130),new Color(134,208,130)));

    fontFamilyCB = new JComboBox(GraphicsEnvironment.
                                 getLocalGraphicsEnvironment().
                                 getAvailableFontFamilyNames());
    fontFamilyCB.setSelectedItem(Constant.getLang("EM_st"));

    fontFamilyCB.setRequestFocusEnabled(false);
    fontFamilyCB.setPreferredSize(new Dimension(100, 25));
    fontFamilyCB.setFont(font);
    fontFamilyCB.setBackground(bgcolor);
    fontFamilyCB.setForeground(fgcolor);
    fontFamilyCB.addActionListener(new fontFamilyAction());

    fontSizeCB = new JComboBox(sizes);
    fontSizeCB.setRequestFocusEnabled(false);
    fontSizeCB.setPreferredSize(new Dimension(50, 25));
    fontSizeCB.setFont(font);
    fontSizeCB.setBackground(bgcolor);
    fontSizeCB.setForeground(fgcolor);
    fontSizeCB.addActionListener(new fontSizeAction());

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

    boldButton = new JButton(boldIcon);
    boldButton.setContentAreaFilled(false);
    boldButton.setRequestFocusEnabled(false);
    boldButton.setPreferredSize(new Dimension(25, 25));

    italicButton = new JButton(italicIcon);
    italicButton.setContentAreaFilled(false);
    italicButton.setRequestFocusEnabled(false);
    italicButton.setPreferredSize(new Dimension(25, 25));

    underlineButton = new JButton(underlineIcon);
    underlineButton.setContentAreaFilled(false);
    underlineButton.setRequestFocusEnabled(false);
    underlineButton.setPreferredSize(new Dimension(25, 25));


    superscriptButton=new JButton("字体");
    superscriptButton.setContentAreaFilled(false);
    superscriptButton.setRequestFocusEnabled(false);
    superscriptButton.setPreferredSize(new Dimension(25,25));


    colorButton = new JButton(colorIcon);
    colorButton.setContentAreaFilled(false);
    colorButton.setRequestFocusEnabled(false);
    colorButton.setPreferredSize(new Dimension(25, 25));
    colorButton.addActionListener(new colorAction());
    selectFacebutton = new JButton(faceIcon);
    // button.addActionListener(new ImageAction());
    selectFacebutton.addMouseListener(this);
    selectFacebutton.setContentAreaFilled(false);
    selectFacebutton.setRequestFocusEnabled(false);
    selectFacebutton.setPreferredSize(new Dimension(25, 25));

    cutButton = new JButton(cutIcon);
    cutButton.setContentAreaFilled(false);
    cutButton.setRequestFocusEnabled(false);
    cutButton.setPreferredSize(new Dimension(25, 25));
    cutButton.setToolTipText(Constant.getLang("EM_jq"));

    copyButton = new JButton(copyIcon);
    copyButton.setContentAreaFilled(false);
    copyButton.setRequestFocusEnabled(false);
    copyButton.setPreferredSize(new Dimension(25, 25));
    copyButton.setToolTipText(Constant.getLang("EM_kb"));


    pasteButton = new JButton(pasteIcon);
    pasteButton.setContentAreaFilled(false);
    pasteButton.setRequestFocusEnabled(false);
    pasteButton.setPreferredSize(new Dimension(25, 25));
    pasteButton.setToolTipText(Constant.getLang("EM_nt"));

    popupMenu=new JPopupMenu();
    cutMenuItem=new JMenuItem(Constant.getLang("EM_jq"));
    cutMenuItem.addActionListener(messageTextPane.getActionMap().get(DefaultEditorKit.cutAction));
    copyMenuItem=new JMenuItem(Constant.getLang("EM_kb"));
    copyMenuItem.addActionListener(messageTextPane.getActionMap().get(DefaultEditorKit.copyAction));
    pasteMenuItem=new JMenuItem(Constant.getLang("EM_nt"));
    pasteMenuItem.addActionListener(messageTextPane.getActionMap().get(DefaultEditorKit.pasteAction));
    popupMenu.add(cutMenuItem);
    popupMenu.add(copyMenuItem);
    popupMenu.add(pasteMenuItem);
    messageTextPane.addMouseListener(new MousePopupListener());




    //boldButton.setAction(getComponent().getActionMap().get("font-bold"));
    boldButton.addActionListener(new boldAction());
    italicButton.addActionListener(new italicAction());
    underlineButton.addActionListener(new underlineAction());
    cutButton.addActionListener(messageTextPane.getActionMap().get(
        DefaultEditorKit.cutAction));
    copyButton.addActionListener(messageTextPane.getActionMap().get(
        DefaultEditorKit.copyAction));
    pasteButton.addActionListener(messageTextPane.getActionMap().get(
        DefaultEditorKit.pasteAction));
    //strikeThroughButton.addActionListener(new strikeThroughAction());

    //italicButton.setAction(getComponent().getActionMap().get("font-italic"));
    //underLineButton.setAction(getComponent().getActionMap().get("font-underline"));
    //toolPanel.setLayout(new GridLayout(1,7));

    colorButton.setBounds(new Rectangle(290, 5, 25, 25));
    underlineButton.setBounds(new Rectangle(260, 5, 25, 25));
    italicButton.setBounds(new Rectangle(230, 5, 25, 25));
    boldButton.setBounds(new Rectangle(200, 5, 25, 25));
    fontSizeCB.setBounds(new Rectangle(130, 5, 61, 25));
    fontFamilyCB.setBounds(new Rectangle(20, 5, 98, 25));
    messageScrollPane.setBounds(new Rectangle(9, 6, 480, 60));
    selectFacebutton.setBounds(new Rectangle(320, 5, 25, 25));
    cutButton.setBounds(new Rectangle(350, 5, 25, 25));
    copyButton.setBounds(new Rectangle(380, 5, 25, 25));
    pasteButton.setBounds(new Rectangle(410, 5, 25, 25));
    superscriptButton.setBounds(new Rectangle(440, 5, 25, 25));
    faceWindow.getContentPane().add(facePanel);
    faceWindow.setVisible(false);

    toolPanel.setBorder(null);
    toolPanel.setLayout(null);
    messagePanel.setLayout(null);
    toolPanel.add(fontFamilyCB, null);
    toolPanel.add(fontSizeCB, null);
    toolPanel.add(boldButton, null);
    toolPanel.add(italicButton, null);
    toolPanel.add(underlineButton, null);
    // toolPanel.add(strikeThroughButton);
    toolPanel.add(colorButton, null);
    //toolPanel.add(toggleButton);
    toolPanel.add(selectFacebutton, null);
    toolPanel.add(cutButton, null);
    toolPanel.add(copyButton, null);
    toolPanel.add(pasteButton, null);

    //toolPanel.add(messageScrollPane);
    //rootPanel.setLayout(new BorderLayout());
    //rootPanel.add(toolPanel,BorderLayout.NORTH) ;
    //rootPanel.add(messagePanel, BorderLayout.CENTER);
    this.setBackground(Color.red);
    this.setLayout(new BorderLayout());
    this.add(toolPanel, BorderLayout.NORTH);
    this.add(messagePanel, BorderLayout.CENTER);
    messagePanel.add(messageScrollPane, null);

    //this.add(rootPanel);
  }

  /*  public void focusGained(FocusEvent e) {
    }*/

⌨️ 快捷键说明

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