多样式输出文本至GUI之JTextPane实例代码

import java.awt.Color;

import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTextPane;
import javax.swing.text.AttributeSet;
import javax.swing.text.SimpleAttributeSet;
import javax.swing.text.StyleConstants;
import javax.swing.text.StyleContext;

public class ColorPane extends JTextPane {

  public void appendNaive(Color c, String s) { // naive implementation
    // bad: instiantiates a new AttributeSet object on each call
    SimpleAttributeSet aset = new SimpleAttributeSet();
    StyleConstants.setForeground(aset, c);

    int len = getText().length();
    setCaretPosition(len); // place caret at the end (with no selection)
    setCharacterAttributes(aset, false);
    replaceSelection(s); // there is no selection, so inserts at caret
  }

  public void append(Color c, String s) { // better implementation--uses
                      // StyleContext
    StyleContext sc = StyleContext.getDefaultStyleContext();
    AttributeSet aset = sc.addAttribute(SimpleAttributeSet.EMPTY,
        StyleConstants.Foreground, c);

    int len = getDocument().getLength(); // same value as
                       // getText().length();
    setCaretPosition(len); // place caret at the end (with no selection)
    setCharacterAttributes(aset, false);
    replaceSelection(s); // there is no selection, so inserts at caret
  }

  public static void main(String argv[]) {

    ColorPane pane = new ColorPane();
    for (int n = 1; n <= 400; n += 1) {
      if (isPrime(n)) {
        pane.append(Color.red, String.valueOf(n) + ' ');
      } else if (isPerfectSquare(n)) {
        pane.append(Color.blue, String.valueOf(n) + ' ');
      } else {
        pane.append(Color.black, String.valueOf(n) + ' ');
      }
    }

    JFrame f = new JFrame("ColorPane example");
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    f.setContentPane(new JScrollPane(pane));
    f.setSize(600, 400);
    f.setVisible(true);
  }

  public static boolean isPrime(int n) {
    if (n < 2)
      return false;
    double max = Math.sqrt(n);
    for (int j = 2; j <= max; j += 1)
      if (n % j == 0)
        return false; // j is a factor
    return true;
  }

  public static boolean isPerfectSquare(int n) {
    int j = 1;
    while (j * j < n && j * j > 0)
      j += 1;
    return (j * j == n);
  }

}

这里写图片描述

package JTextPane;  

import java.awt.Color;  
import java.awt.event.ActionEvent;  
import java.awt.event.ActionListener;  

import javax.swing.JButton;  
import javax.swing.JComboBox;  
import javax.swing.JFrame;  
import javax.swing.JScrollPane;  
import javax.swing.JTextPane;  
import javax.swing.text.BadLocationException;  
import javax.swing.text.Style;  
import javax.swing.text.StyleConstants;  

public class Test extends JFrame {  
    private static final long serialVersionUID = 1L;  

    /** 定义一个历史面板,用于显示已经发送的文字 */  
    private JTextPane _old = new JTextPane();  

    /** 定义一个输入面板,用于显示正在输入的内容 */  
    private JTextPane _new = new JTextPane();  

    /** 声明三组样式,具体的在方法public static void styleInit() {}中定义 */  
    private Style style1 = null;  
    private Style style2 = null;  
    private Style style3 = null;  

    /** 下拉列表,用于选择样式 */  
    private JComboBox<String> box = new JComboBox<String>();  

    /** 发送按钮,用于将消息提交到历史面板 */  
    private JButton send = new JButton("提交");  

    public static void main(String[] args) {  
        new Test();  
    }  

    /** 
     * 构造方法,需要完成所以初始化操作 鼠标放在方法名上,可以显示其内容 
     */  
    public Test() {  
        styleInit();  
        init();  
    }  

    /** 样式初始化 */  
    public void styleInit() {  

        Style style = _new.getStyledDocument().addStyle(null, null);// 获取组件空样式,addStyle(null,  
                                                                    // null)会返回一个空样式  

        StyleConstants.setFontFamily(style, "楷体");// 为style样式设置字体属性  
        StyleConstants.setFontSize(style, 18);// 为style样式设置字体大小  

        Style normal = _new.addStyle("normal", style);// 将style样式添加到组件,并命名为normal,返回一个样式由Style  
                                                        // normal变量接收  
        /** 这个时候,组件编辑器关联的模型中就添加了一个样式normal,这个样式是最基本的一个样式,其他样式可以根据他进行修改 */  

        style1 = _new.addStyle("style1", normal);// 基于normal样式,在添加三次,分别命名为style1,style2,style3  
        style2 = _new.addStyle("style2", normal);// 此时,style1,style2,style3三个样式和normal样式是一模一样的  
        style3 = _new.addStyle("style3", normal);// 如果修改,可以对每个变量单独修改,具体修改方式如下  

        StyleConstants.setForeground(style1, Color.GREEN);// 将style1的颜色设置为绿色  

        StyleConstants.setForeground(style2, Color.RED);// 将style2的颜色设置为红色  

        StyleConstants.setForeground(style3, Color.BLACK);// 将style3的颜色设置为黑色  
        StyleConstants.setFontSize(style3, 14);// 将style3的大小设置为14  
    }  

    /** 初始化布局 */  
    public void init() {  
        this.setBounds(200, 100, 420, 520);  
        this.setLayout(null);  

        this._old.setEditable(false);  
        // 定义滚动面板,放历史面板,以实现滚动条(有需要的时候显示)和换行  
        JScrollPane js_old = new JScrollPane(_old,  
                JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,  
                JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);  
        // 设置位置大小  
        js_old.setBounds(0, 0, 400, 300);  
        // 添加到窗体  
        this.add(js_old);  

        // 定义滚动面板,放输入面板,以实现滚动条(有需要的时候显示)和换行  
        JScrollPane js_new = new JScrollPane(_new,  
                JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,  
                JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);  
        // 设置位置大小  
        js_new.setBounds(0, 350, 400, 150);  
        // 添加到窗体  
        this.add(js_new);  

        this.box.addItem("style1");  
        this.box.addItem("style2");  
        this.box.addItem("style3");  
        this.box.setBounds(50, 315, 100, 20);  
        this.add(this.box);  

        this.send.setBounds(200, 315, 100, 20);  
        this.add(this.send);  

        this.send.addActionListener(new ActionListener() {  
            @Override  
            public void actionPerformed(ActionEvent e) {  
                inserMessage();  
            }  
        });  

        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);  
        this.setVisible(true);  
    }  

    /** 将文字插入到历史面板,并清空输入面板 */  
    public void inserMessage() {  
        try {  
            /** 判断下拉列表内容,确定使用哪种样式 */  
            Style style = (box.getSelectedItem().equals("style1")) ? style1  
                    : (box.getSelectedItem().equals("style2")) ? style2  
                            : style3;  
            /** 
             * 获取历史面板的insertString方法,将文字追加到历史面板上 
             *  
             * void insertString(int offset, String str, AttributeSet a) throws 
             * BadLocationException offset - 要插入内容的偏移量,该值 >= 
             * 0。跟踪给定的位置或其后位置的更改的所有位置都将移动。 str - 要插入的字符串 a - 
             * 要与插入的内容关联的属性。如果没有属性,它可能为 null。 
             *  
             * this._old.getStyledDocument().getLength()这一句是获取当前面板内容的总长度, 
             * 作为要插入内容的偏移量 this._new.getText()+"\n"这一句是获取输入面板内容 style这一句是使用的样式 
             */  
            this._old.getStyledDocument().insertString(  
                    this._old.getStyledDocument().getLength(),  
                    this._new.getText() + "\n", style);  
            /** 将输入面板置空 */  
            this._new.setText(null);  
        } catch (BadLocationException e) {  
            e.printStackTrace();  
        }  
    }  

}  

这里写图片描述

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值