java设置字体颜色

package com.yidu.demo;

import java.awt.Color;
import java.awt.Font;
import java.awt.Image;
import java.awt.MenuItem;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.InputEvent;
import java.awt.event.KeyEvent;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.Enumeration;

import javax.swing.ImageIcon;
import javax.swing.JColorChooser;
import javax.swing.JEditorPane;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JTextPane;
import javax.swing.KeyStroke;
import javax.swing.text.AttributeSet;
import javax.swing.text.BadLocationException;
import javax.swing.text.Document;
import javax.swing.text.Element;
import javax.swing.text.SimpleAttributeSet;
import javax.swing.text.StyleConstants;
import javax.swing.text.StyleContext;
import javax.swing.text.StyledDocument;
import javax.swing.text.StyleConstants.ColorConstants;
import javax.swing.text.StyleConstants.FontConstants;

import sun.font.FontFamily;

public class MainFrame extends JFrame {
 JTextPane editorPane=new JTextPane();
 Socket socket;
 public MainFrame(){
  JMenuBar bar=new JMenuBar();
  this.setJMenuBar(bar);
  JMenu menuFile=new JMenu("编辑");
  bar.add(menuFile);
  JMenuItem itemSend=new JMenuItem("发送");
  menuFile.add(itemSend);
  try {
   socket=new Socket("127.0.0.1",1000);
  } catch (UnknownHostException e2) {
   // TODO Auto-generated catch block
   e2.printStackTrace();
  } catch (IOException e2) {
   // TODO Auto-generated catch block
   e2.printStackTrace();
  }
  itemSend.addActionListener(new ActionListener(){

   public void actionPerformed(ActionEvent e) {
    try {
     
     OutputStream os= socket.getOutputStream();
     ObjectOutputStream objectOutputStream=new ObjectOutputStream(os);
     objectOutputStream.writeObject(editorPane);
     objectOutputStream.flush();
    } catch (UnknownHostException e1) {
     // TODO Auto-generated catch block
     e1.printStackTrace();
    } catch (IOException e1) {
     // TODO Auto-generated catch block
     e1.printStackTrace();
    }
   }
   
  });
  JMenuItem itemGet=new JMenuItem("得到内容");
  menuFile.add(itemGet);
  itemGet.addActionListener(new ActionListener(){

   public void actionPerformed(ActionEvent e) {
    Document document= editorPane.getDocument();
    
     editorPane.insertIcon(new ImageIcon("MessageReading.gif"));
     
    
   }
   
  });
  JMenuItem itemColor=new JMenuItem("颜色");
  itemColor.addActionListener(new ActionListener(){

   public void actionPerformed(ActionEvent e) {
    JColorChooser colorChooser=new JColorChooser();
    Color color= colorChooser.showDialog(null, "字体颜色", Color.BLACK);
    Document document= editorPane.getDocument();
    try {
     StyleContext   sc   =   StyleContext.getDefaultStyleContext();
     AttributeSet   aset   =   sc.addAttribute(SimpleAttributeSet.EMPTY,StyleConstants.Foreground, color);
     Font font=new Font("隶书",Font.BOLD,30);
     aset=sc.addAttribute(aset, StyleConstants.Family, font.getFamily());
     aset=sc.addAttribute(aset, StyleConstants.FontSize, 30);
     int start= editorPane.getSelectionStart();
     int end=editorPane.getSelectionEnd();
     String str= document.getText(start,end-start);
     document.remove(start, end-start);
     document.insertString(start, str, aset);
    } catch (BadLocationException e1) {
     // TODO Auto-generated catch block
     e1.printStackTrace();
    }
   }
   
  });
  menuFile.add(itemColor);
  itemColor.setMnemonic('C');
  //设置快捷键
  itemColor.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_C,InputEvent.ALT_MASK));
  this.add(editorPane);
  editorPane.setText("测试");
  this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  
 }
 /**
  * @param args
  */
 public static void main(String[] args) {
  MainFrame mainFrame=new MainFrame();
  mainFrame.setSize(400, 300);
  mainFrame.setVisible(true);
 }

}

  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值