JTextPane例子,演示为文字设置字体、字号、样式、颜色、背景色和插入图片功能

JTextPane的例子,模拟聊天客户端,演示了为每段文字设置字体、字号、样式、颜色、背景色和插入图片功能

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;

import javax.swing.BorderFactory;
import javax.swing.Box;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
import javax.swing.JTextPane;
import javax.swing.UIManager;
import javax.swing.text.BadLocationException;
import javax.swing.text.SimpleAttributeSet;
import javax.swing.text.StyleConstants;
import javax.swing.text.StyledDocument;

/**
 * JTextPane的例子,模拟聊天客户端,演示了为每段文字设置字体、字号、样式、颜色、背景色和插入图片功能
 * @author 五斗米 <如转载请保留作者和出处>
 * @blog http://blog.csdn.net/mq612
 */

public class Test extends JFrame {

 private static final long serialVersionUID = -2397593626990759111L;

 private JScrollPane scrollPane = null; // 滚动

 private JTextPane text = null; // 不用说了,如果不认识的话就没必要往后看了

 private Box box = null; // 放输入组件的容器

 private JButton b_insert = null, b_remove = null, b_icon = null; // 插入按钮;清除按钮;插入图片按钮

 private JTextField addText = null; // 文字输入框

 private JComboBox fontName = null, fontSize = null, fontStyle = null, fontColor = null,
   fontBackColor = null; // 字体名称;字号大小;文字样式;文字颜色;文字背景颜色

 private StyledDocument doc = null; // 非常重要插入文字样式就靠它了

 public Test() {
  super("JTextPane Test");
  try { // 使用Windows的界面风格
   UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
  } catch (Exception e) {
   e.printStackTrace();
  }
  text = new JTextPane();
  text.setEditable(false); // 不可录入
  doc = text.getStyledDocument(); // 获得JTextPane的Document
  scrollPane = new JScrollPane(text);
  addText = ne

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值