Getting Your Feet Wet with the SWT
2008-02-23 09:16:03来源:互联网 阅读 ()
OvervIEw
The StyledText widget provides a fast and easy to use means to display and edit text. Within the StyledText widget, the following attributes can be specifIEd:
- text foreground color
- text background color
- text font style (i.e., normal, bold or italic)
- line background color
These attributes are referred to as "styles". In addition, the StyledText widget provides standard navigation/editing keyboard behavior, allows for user-defined key bindings, and will perform Word wrapping. Note that support for fonts with italic style was added as of version 3.0. However, the StyledText widget does not support mixing of multiple fonts.
The StyledText widget can be customized using pluggable objects to manage text content and styles. You can provide your own implementation of these objects in order to tailor the widget to your needs. Or if you have no need to customize the widget, easy to use API for managing styles and text content is available, making the pluggable nature of the widget completely transparent. How to customize the StyledText widget and why you might want to do so will be discussed in our other article, Into the Deep End of the StyledText Widget.
The StyledText widget is used by the JFace Text Framework, which is part of the Eclipse Platform. The JFace Text Framework provides a layer of abstraction on top of the StyledText widget. The layer is pluggable and supports text formatting, content code assist capabilitIEs, and a source viewer.
A Simple Example
The following is a simple example that shows how to create a StyledText widget and set text. This example will be built upon in subsequent sections of this article.
import org.Eclipse.swt.*; import org.eclipse.swt.custom.*; import org.eclipse.swt.graphics.*; import org.eclipse.swt.widgets.*; import org.eclipse.swt.layout.*; public class StyledTextExample { public static void main(String [] args) { // create the widget's shell Shell shell = new Shell(); shell.setLayout(new FillLayout()); shell.setSize(200, 100); Display display = shell.getDisplay(); // create the styled text widget StyledText widget = new StyledText(shell, SWT.BORDER); widget.setText("This is the StyledText widget."); shell.open(); while (!shell.isDisposed()) if (!display.readAndDispatch()) display.sleep(); } }
Character, Line and Caret Offsets
Within the StyledText widget, both character offsets and line indexes are zero based. For example, in the StyledText widget below, the line at index 0 is "abcde" and the character at offset 0 is "a".
widget.setText("abcde\r\nfghij");
When specifying offsets within the StyledText widget, line delimiter characters are included. In the above example, the line delimiter is CR/LF (i.e., "\r\n"). Therefore, the character at offset five is CR and the character at offset six is LF. Similarly, getLineAtOffset(6) returns 0 and getLineAtOffset(7) returns 1. If there was another CR/LF line break at the end of the second line, getLineAtOffset(14) would return 2.
Caret offsets are also zero based. Calling setCaretOffset(4) for the above widget places the caret between 'd' and 'e'. And, like character offsets, caret offsets take line delimiters into account. In order to place the caret at the beginning of the second line (i.e., in front of the 'f') you would call
标签:
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有
IDC资讯: 主机资讯 注册资讯 托管资讯 vps资讯 网站建设
网站运营: 建站经验 策划盈利 搜索优化 网站推广 免费资源
网络编程: Asp.Net编程 Asp编程 Php编程 Xml编程 Access Mssql Mysql 其它
服务器技术: Web服务器 Ftp服务器 Mail服务器 Dns服务器 安全防护
软件技巧: 其它软件 Word Excel Powerpoint Ghost Vista QQ空间 QQ FlashGet 迅雷
网页制作: FrontPages Dreamweaver Javascript css photoshop fireworks Flash