• JTextpane 加入的行号


    最近项目需求,在需求JTextPane加入行号等信息,网上找了半天才发现JTextArea加入行号信息。copy正在研究在线程序。他发现自己能够做出改变来改变JTextPane显示行号。

    代码:

    package com.cml.line;


    import java.awt.Color;
    import java.awt.FontMetrics;
    import java.awt.Component;
    import java.awt.Graphics;
    import java.awt.Insets;


    import javax.swing.JTextArea;
    import javax.swing.JTextPane;
    import javax.swing.border.AbstractBorder;


    public class LineNumberBorder extends AbstractBorder
    {
    public LineNumberBorder()
    {


    }


    /*
    * Insets 对象是容器边界的表示形式。 它指定容器必须在其各个边缘留出的空间。
    */
    // 此方法在实例化时自己主动调用
    // 此方法关系到边框是否占用组件的空间
    public Insets getBorderInsets(Component c)
    {
    return getBorderInsets(c, new Insets(0, 0, 0, 0));
    }


    public Insets getBorderInsets(Component c, Insets insets)
    {
    if (c instanceof JTextPane)
    {
    //这里设置行号左边边距
    insets.left = 20;

    return insets;


    }


    public boolean isBorderOpaque()
    {
    return false;
    }


    // 边框的绘制方法
    // 此方法必须实现
    public void paintBorder(Component c, Graphics g, int x, int y, int width,
    int height)
    {
    // 获得当前剪贴区域的边界矩形。
    java.awt.Rectangle clip = g.getClipBounds();
    FontMetrics fm = g.getFontMetrics();
    int fontHeight = fm.getHeight();


    // starting location at the "top" of the page...
    // y is the starting baseline for the font...
    int ybaseline = y + fm.getAscent();


    // now determine if it is the "top" of the page...or somewhere
    // else
    int startingLineNumber = (clip.y / fontHeight) + 1;


    if (startingLineNumber != 1)
    {
    ybaseline = y + startingLineNumber * fontHeight
    - (fontHeight - fm.getAscent());
    }


    int yend = ybaseline + height;
    if (yend > (y + height))
    {
    yend = y + height;
    }
    g.setColor(Color.blue);
    // 绘制行号
    while (ybaseline < yend)
    {
    String label = padLabel(startingLineNumber, 0, true);


    g.drawString(label, 0, ybaseline);
    ybaseline += fontHeight;
    startingLineNumber++;
    }
    }


    // 寻找适合的数字宽度
    private int lineNumberWidth(JTextArea jta)
    {
    int lineCount = Math.max(jta.getRows(), jta.getLineCount());
    return jta.getFontMetrics(jta.getFont()).stringWidth(lineCount + " ");
    }


    private String padLabel(int lineNumber, int length, boolean addSpace)
    {
    StringBuffer buffer = new StringBuffer();
    buffer.append(lineNumber);
    for (int count = (length - buffer.length()); count > 0; count--)
    {
    buffer.insert(0, ' ');
    }
    if (addSpace)
    {
    buffer.append(' ');
    }
    return buffer.toString();
    }


    }

    源码是网上下的。下了好久了,忘了是谁的了。

    使用时仅只有哟呼jtextpane的setBorder();方法就可以了。

    版权声明:本文博主原创文章。博客,未经同意不得转载。

  • 相关阅读:
    动态代理Dynamic Proxy
    ORM SQLOBJECT SIMPLE
    python mysql desc
    How to use AKBusGpsParser
    AT&T ASSEMBLY FOR LINUX AND MAC (SYS_FORK)
    How to install ZeroMQ on Ubuntu14.04
    [LeetCode]208. 实现 Trie (前缀树)
    [LeetCode]438. 找到字符串中所有字母异位词、76. 最小覆盖子串(滑动窗口解决子串问题系列)
    【二叉树-最长路径系列(任意路径)】直径、最长同值路径、 最大路径和(DFS、树形DP)
    [LeetCode]146. LRU缓存机制
  • 原文地址:https://www.cnblogs.com/gcczhongduan/p/4869118.html
Copyright © 2020-2023  润新知