转自:http://chenghy28.iteye.com/blog/547489
- import javax.swing.text.AttributeSet;
- import javax.swing.text.BadLocationException;
- import javax.swing.text.DefaultStyledDocument;
- import javax.swing.text.MutableAttributeSet;
- import javax.swing.text.StyleConstants;
-
-
-
-
-
- public class CustomizeStyledDocument extends DefaultStyledDocument {
- private int type = -1;
-
- AttributeSet myAttributeSet = null;
-
- public CustomizeStyledDocument(int type) {
- this.type = type;
- }
-
-
-
-
- public void insertString(int offset, String str, AttributeSet a)
- throws BadLocationException {
- this.myAttributeSet = a;
- super.insertString(offset, str, a);
- setSyntaxColor(offset, str.length());
- }
-
-
-
-
- public void remove(int offs, int len) throws BadLocationException {
- super.remove(offs, len);
- setSyntaxColor(offs);
- }
-
-
-
-
- private String getPositionChar(int offset) {
- String str = "";
- try {
- str = getText(offset, 1);
- } catch (BadLocationException ex) {
-
- }
- return str;
- }
-
-
-
-
- private String getBeforeBlankString(int offset) {
- String str = "";
- if (offset < 0)
- return "";
-
- str = getPositionChar(offset);
- if (SyntaxMgr.isSpaceChar(str))
- return "";
-
- String r = getBeforeBlankString(offset - 1);
- return r + str;
- }
-
-
-
-
- private String getAfterBlankString(int offset) {
- String str = "";
- if (offset > getLength())
- return "";
- str = getPositionChar(offset);
- if (SyntaxMgr.isSpaceChar(str))
- return "";
- String r = getAfterBlankString(offset + 1);
- return str + r;
- }
-
-
-
-
- private int setSyntaxColor(int offset) {
- if (offset < 0)
- return offset;
-
- if (myAttributeSet == null)
- return offset;
-
- String ifSyntax = "";
-
- String before = getBeforeBlankString(offset - 1);
- String after = getAfterBlankString(offset);
-
- ifSyntax = (before + after).trim();
-
- int start = offset - before.length();
-
- int tmp_len = ifSyntax.length();
-
- if (start < 0 || tmp_len <= 0)
- return offset;
-
-
- StyleConstants.setForeground((MutableAttributeSet) myAttributeSet,
- SyntaxMgr.isSyntax(type, ifSyntax));
-
- setCharacterAttributes(start, tmp_len, myAttributeSet, true);
-
- return start + tmp_len;
- }
-
-
-
-
- private int setSyntaxColor(int offset, int len) throws
分享到:
相关推荐
NULL 博文链接:https://icarusliu.iteye.com/blog/1412007
JAVA-----JTextPane实例 代码
JTextPane使用,可以传图片,文字等
SWT JTextPane 可以把控件中需要的字符串的颜色修改成自己想要的颜色
演示、说明程序 博文链接:https://bps.iteye.com/blog/136627
课程设计期间做的Java的桌面端小程序,可以发送Emoji表情的聊天室,另外在文件夹中放置了自己的关键代码部分解析,关于emoji部分可查看笔者博客...
本源码实现了 文本区里加图片和插入艺术字的功能 窗口使用Windows窗体风格
NULL 博文链接:https://icarusliu.iteye.com/blog/2098986
Java制作的网页浏览器和源代码查看器,仿IE的网页浏览功能,不支持HTML5,窗口采用上下分屏显示的方式,上面显示网页浏览器,下面显示该网页的源代码,JTextField jtfAddress; //输入html文件地址或网址 ...
JTextPane jtpShow; //显示文件 JLabel jlInfo; //提示信息 public HTTPBrowserDemo() { super("韩公达制作的html浏览器"); //调用父类构造函数 jtfAddress=new JTextField(20); //实例化地址输入框 jbGo...
这位是我刚做的一个windows记事本;使用的是JTextPane做的,已经实现全部的功能;还就有语法高亮和插入图片的功能
NULL 博文链接:https://gh0769.iteye.com/blog/598839
JTextPane jtpShow; //显示文件 JLabel jlInfo; //提示信息 public HTTPBrowserDemo(){ super("html浏览器"); //调用父类构造函数 jtfAddress=new JTextField(20); //实例化地址输入框 jbGo=new...
7. JTextPane:JTextPane 是 Java 的一个文本域组件,负责显示和编辑文本内容。 8. JScrollPane:JScrollPane 是 Java 的一个滚动窗格组件,负责显示文本域的滚动条。 事件处理 该程序使用了多个事件处理器,负责...
用JTextPanel实现文本带下划线功能