FORTUNE

THE WAY TO THE MASTER...
posts - 49, comments - 18, trackbacks - 0, articles - 1
  BlogJava :: 首页 :: 新随笔 :: 联系 :: 聚合  :: 管理

SWT Basic Controls -- Text

Posted on 2006-03-13 16:30 fortune 阅读(1068) 评论(1)  编辑  收藏 所属分类: 我的学习笔记
Text Hierarchy


graphics/07inf07.gif

Text Styles

Style

Description

SWT.SINGLE

Allow a single line to be edited  单行

SWT.MULTI

Allow multiple lines to be edited  多行

SWT.READ_ONLY

Make the control noneditable     不可编辑

SWT.WRAP

Allow strings to wrap instead of scrolling 自动换行

SWT.LEFT

Left-align the contents of the control    左对齐

SWT.CENTER

Center-align the contents of the control  中间对齐

SWT.RIGHT

Right-align the contents of the control   右对齐



Text Events

Event

     Description

SWT.DefaultSelection

     Default selection occurred (user pressed <Enter>)

SWT.Modify

     Text has changed in the control  控件中的文本内容发生了改变

SWT.Verify

     Text is to be validated in the control  文本内容需要验证


text控件支持"plain"text,这意味着text中的字符必须都是同样的字体和颜色,如果需要 更多的功能就使用org.eclipse.swt.custom.StyledText(eclipse为用户定制的)注意StyledText不是本地(native)控件

一共有2种类型的text控件:单行的和多行的


Single-Line and Multiline Text Controls

SWT.SINGLE

Text text = new Text(parent, SWT.SINGLE | SWT.BORDER);
text.setText("Texan");


 SWT.MULTI
与单行的text不同它可以含有scroll bar  (通过设置SWT.H_SCROLL or SWT.V_SCROLL )

int style =
    SWT.MULTI | SWT.BORDER | SWT.V_SCROLL | SWT.H_SCROLL;
Text text = new Text(parent, style);


String Operations

setText(String string)

getText()

getText(int start, int end)  获取text的文本内容从start到end
getCharCount()  返回字符的数目


Passwords and the Echo Character

int style = SWT.SINGLE | SWT.BORDER | SWT.PASSWORD;

Text text = new Text(parent, style);

text.setText("fred54"); //在text上不会显示"fred54",而是以echo字符代替

注意在不同的平台上echo字符是不同的,我们常见的密码echo字符是“*”,可以自己设置echo字符

setEchoChar(char echo)如果设置的字符是'\0'则不再隐藏字符,当前的字符被显示

getEchoChar() 返回setEchoChar函数设置的echo,如果未设置则返回'\0',如果使用了SWT.PASSWORD 则返回的字符不确定
通常来说自己设置echo字符是不明智的,因为这是平台look and feel的一部分。


 Lines and Line Height

getLineCount() 返回行数
getLineHeight() 返回每行高度(像素)该值与字符的高度并不相同,因为行间有空隙

public static void main(String[] args) {

    Display display = new Display();

    Shell shell = new Shell(display);

    Text text = new Text(shell, SWT.H_SCROLL|SWT.V_SCROLL);

    int rows = 5, columns = 10;

    GC gc = new GC(text);

    FontMetrics fm = gc.getFontMetrics();

    gc.dispose();

    int height = rows * text.getLineHeight();

    int width = columns * fm.getAverageCharWidth();

    text.setSize(text.computeSize (width, height));

    shell.pack();

    shell.open();

    while (!shell.isDisposed()) {

        if (!display.readAndDispatch()) display.sleep();

    }

    display.dispose();

  }


 Line Delimiters
行定义符号










评论

# re: SWT Basic Controls -- Text  回复  更多评论   

2006-03-15 13:16 by 余灵
恩``写的太好了我草!

只有注册用户登录后才能发表评论。


网站导航: