Getting Your Feet Wet with the SWT

2008-02-23 09:16:03来源:互联网 阅读 ()

新老客户大回馈,云服务器低至5折

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
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有

上一篇:Tapestry的新logo恐怕还要重新考虑

下一篇:一个成功的Jsp程序员该怎样学习JSP呢?