JTextpane 加入的行号
2015-10-11 13:00
369 查看
最近项目需求,在需求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();方法就可以了。
代码:
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();方法就可以了。
相关文章推荐
- php正则提取img所有属性值
- PHP第一季视频教程.李炎恢.学习笔记(四)(第2章 基本语法(3))
- PHP关闭Notice错误提示
- PHPSingleton模式的例子
- thinkPHP简单留言板
- PHP第一季视频教程.李炎恢.学习笔记(三)(第2章 基本语法(2))
- PostgreSQL+phpPgAdmin总是登录失败(login failed)
- PHP中字符串拆分和拼接
- php实现Mysql简易操作类
- PHP下的经纬度求距离
- Wampservice localhost和phpmyadmin打不开
- php用协程实现多任务调度(转载于风雪之隅)
- php 去掉字符串的最后一个字符
- PHPCMSV9文章页显示发布者
- yii中rights安装
- php利用header函数实现文件下载时直接提示保存
- php header的用法
- phpstorm集成phpunit
- 关于php规范psr-4
- Editplus快捷键大全