import java.util.Hashtable; import java.util.Iterator; import java.util.Set; import org.eclipse.swt.SWT; import org.eclipse.swt.custom.StyleRange; import org.eclipse.swt.custom.StyledText; import org.eclipse.swt.events.MouseAdapter; import org.eclipse.swt.events.MouseEvent; import org.eclipse.swt.graphics.Color; import org.eclipse.swt.graphics.Point; import org.eclipse.swt.layout.FillLayout; import org.eclipse.swt.widgets.Display; import org.eclipse.swt.widgets.Shell; /** * 给StyleText加链接 * @author Administrator * */ public class StyleTextLink { public static Hashtable<Point, String> link = new Hashtable<Point, String>(); private static Color BLUE;//链接颜色 public static void ReplaceLink(StyledText st, String line) {//给StyleText加链接样式 Display display = st.getDisplay(); BLUE = display.getSystemColor(SWT.COLOR_BLUE); StringBuffer text = new StringBuffer(line); int startLink = text.indexOf("http");//判断是否是http协议开头 int i = 0; int offset = 0; while (startLink != -1) {//循环找出文本内所有连接 int endLink = isChinese(text.toString(), startLink, text.length());//判断结束位置 int length = 0; if (endLink != -1) {//判断是否是文本的结尾 length = endLink - startLink; } else { endLink = text.length(); length = text.length() - startLink; } int start = startLink + offset; int end = endLink + offset; Point p = new Point(start, end); link.put(p, line.subSequence(start, end).toString());//将连接的坐标和链接地址加入缓存 StyleRange style1 = new StyleRange();//生成链接的样式 style1.start = startLink + offset;//连接开始坐标 style1.length = length;//长度 style1.underline = true;//下划线 style1.foreground = BLUE;//颜色 st.setStyleRange(style1); text.delete(startLink, endLink); int s = text.indexOf("http"); offset += endLink - startLink; startLink = s; i++; } } /** * 判断是否是中文如果是中文则是连接的结束位置 * @param strName * @param start * @param end * @return */ public static int isChinese(String strName, int start, int end) { char[] ch = strName.toCharArray(); for (int i = start; i < end; i++) { char c = ch[i]; if (isChinese(c) == true) { return i; } } return -1; } /** * 判断是否是中文字符 * @param c * @return */ public static boolean isChinese(char c) { Character.UnicodeBlock ub = Character.UnicodeBlock.of(c); if (ub == Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS || ub == Character.UnicodeBlock.CJK_COMPATIBILITY_IDEOGRAPHS || ub == Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS_EXTENSION_A || ub == Character.UnicodeBlock.GENERAL_PUNCTUATION || ub == Character.UnicodeBlock.CJK_SYMBOLS_AND_PUNCTUATION || ub == Character.UnicodeBlock.HALFWIDTH_AND_FULLWIDTH_FORMS) { return true; } return false; } public static void main(String[] args) { Display display = new Display(); Shell shell = new Shell(display); shell.setText("给StyleText加链接"); shell.setLayout(new FillLayout()); final StyledText text = new StyledText(shell, SWT.BORDER); text .setText("http://www.baidu.com这是百度的连接http://www.tom.com这是Tom连接http://localhost"); StyleTextLink.ReplaceLink(text, text.getText()); text.addMouseListener(new MouseAdapter() {//加入鼠标点击链接监听事件 public void mouseDown(MouseEvent e) { Point p = new Point(e.x, e.y);//鼠标按下的坐标 try { int n = text.getOffsetAtLocation(p);//找出相对文本的位置 StyleRange style = text.getStyleRangeAtOffset(n); if (style != null && style.underline && style.foreground.equals(BLUE)) {//判断样式是否是连接的样式 Set key = link.keySet(); Iterator<Point> iterator = key.iterator(); String linked = ""; while (iterator.hasNext()) {//循环从缓存中取出连接坐标 Point pt = iterator.next(); if (pt.x <= n && pt.y >= n) {//对比坐标位置是否在连接文本范围内 linked = link.get(pt); System.out.println(linked); String cmd = "rundll32 url.dll,FileProtocolHandler " + linked; Process pe = Runtime.getRuntime().exec( cmd);//打开浏览器 } } } } catch (Exception e1) { } } }); shell.pack(); shell.open(); while (!shell.isDisposed()) { if (!display.readAndDispatch()) display.sleep(); } display.dispose(); } } 找了很久都没找到怎样加链接,最后只好自己写了。