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

📄 editormessage.java~2~

📁 1. 消息即时通信 2. 消息发送实现一对一、一对多等多种发送模式 3. 发送的消息实现多彩文本编辑
💻 JAVA~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.*;public class EditorMessage extends JPanel implements ActionListener {  Font font;  JPanel toolPanel;  JPanel messagePanel;  JTextPane messageTextPane;  JScrollPane messageScrollPane;  JComboBox fontFamilyCB;  JButton boldButton;  JButton italicButton;  JButton underLineButton;  static String fontFamily;  public EditorMessage() {    try {      jbInit();    }    catch(Exception ex) {      ex.printStackTrace();    }  }  void jbInit() throws Exception {    font=new Font("新宋体",Font.PLAIN,12);    toolPanel=new JPanel();    messagePanel=new JPanel();    messageTextPane=new JTextPane();    messageTextPane.setContentType("text/html");    messageScrollPane=new JScrollPane(messageTextPane);    messageScrollPane.setPreferredSize(new Dimension(400,400));    fontFamilyCB=new JComboBox(GraphicsEnvironment.getLocalGraphicsEnvironment().                               getAvailableFontFamilyNames());    fontFamilyCB.setFont(font);    fontFamilyCB.setBackground(Color.white);    fontFamilyCB.addActionListener(this);    boldButton=new JButton();    italicButton=new JButton();    underLineButton=new JButton();    //boldButton.setAction(getComponent().getActionMap().get("font-bold"));    italicButton.setAction(getComponent().getActionMap().get("font-italic"));    underLineButton.setAction(getComponent().getActionMap().get("font-underline"));    boldButton.setAction(new StyledEditorKit.FontFamilyAction(fontFamily,fontFamily));    //System.out.println(fontFamily);    toolPanel.add(fontFamilyCB);    toolPanel.add(boldButton);    toolPanel.add(italicButton);    toolPanel.add(underLineButton);    messagePanel.add(messageScrollPane);    this.setLayout(new GridLayout(2,1));    this.add(toolPanel);    this.add(messagePanel);  }  public void actionPerformed(ActionEvent evt) {    JComboBox source = (JComboBox) evt.getSource();    fontFamily = (String) source.getSelectedItem();    fontFamilyCB.setAction(new StyledEditorKit.FontFamilyAction(fontFamily,fontFamily));    //Document doc=getComponent().getDocument();    //getComponent().setFont(new Font(fontFamily,Font.BOLD,12));    System.out.println(fontFamily);    //fontLabel.setFont(new Font(item, Font.PLAIN, 12));  }  public JTextComponent getComponent(){    return this.messageTextPane;  }  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);  }}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);      }    }  }

⌨️ 快捷键说明

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