Eclipse大本营


SWT开发 - Text的使用(1)

2008-01-26 00:29Update

eclipse

本文介绍SWT Text(文本框)的创建方法。

范例代码


TextSample01.java
package com.test.swt;

import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;

public class TextSample01 {

    public static void main(String[] args) {
        Display display = new Display();
        Shell shell = new Shell(display);
        shell.setLayout(new GridLayout(1,true));
        
        Text t1 = new Text(shell, SWT.BORDER | SWT.SINGLE);
        t1.setText ("Line1\r\nLine2");
        GridData gridData1 = new GridData();
        gridData1.horizontalAlignment = GridData.FILL;
        gridData1.grabExcessHorizontalSpace = true;
        t1.setLayoutData(gridData1);
        
        Text t2 = new Text(shell, SWT.BORDER | SWT.MULTI);
        t2.setText ("Line1\r\nLine2");
        GridData gridData2 = new GridData();
        gridData2.horizontalAlignment = GridData.FILL;
        gridData1.grabExcessHorizontalSpace = true;
        gridData2.verticalAlignment = GridData.FILL;
        gridData2.grabExcessVerticalSpace = true;
        t2.setLayoutData(gridData2);
        
        Text t3 = new Text(shell, SWT.BORDER | SWT.MULTI | SWT.V_SCROLL);
        t3.setText ("Line1\nLine2\nLine3\nLine4\nLine5");
        t3.setLayoutData(gridData2);
        
        shell.setSize(200, 180);
        shell.open();
        while (!shell.isDisposed()) {
            if (!display.readAndDispatch())
                display.sleep();
        }
        display.dispose();
    }

}


解说


Text控件的创建:
可以通过类似Text t1 = new Text(shell, SWT.BORDER | SWT.SINGLE);的语句创建Text控件。第2个参数为空间的风格,多个风格可以使用 “|” 符号联结。
Text控件的风格:
主要可以指定为以下几种:
SWT.BORDER:指定边框
SWT.SINGLE:单行Text
SWT.MULTI :多行显示
SWT.V_SCROLL:显示垂直滚动条
SWT.H_SCROLL:显示水平滚动条
另外还可以为其指定边框颜色,背景颜色等其他风格,这里不一一介绍了。

多行显示时,可以使用\n(换行)或\r\n(回车换行)将数据显示在不同的行。

排版控制GridData 以后再作介绍。

画面显示


 
Copyright ©2006-2010 lifevv.com. All Rights Reserved
POWERED BY @pmplat.syboos.com