Eclipse插件开发:SWT之TextExample

    该例子演示了SWT界面下的Text(文本域)构建及监听,限制了只能输入数字字符。


    import org.eclipse.swt.SWT;
    import org.eclipse.swt.events.VerifyEvent;
    import org.eclipse.swt.events.VerifyListener;
    import org.eclipse.swt.layout.FillLayout;
    import org.eclipse.swt.widgets.Display;
    import org.eclipse.swt.widgets.Shell;
    import org.eclipse.swt.widgets.Text;
    
    public class TextExample {
    
    	public static void main(String[] args) {
    		Display display = new Display();
    		Shell shell = new Shell(display);
    		shell.setText("Text Example");
    		shell.setBounds(100, 100, 200, 100);
    		shell.setLayout(new FillLayout());
    		final Text text = new Text(shell, SWT.MULTI);
    		text.addVerifyListener(new VerifyListener() {
    			public void verifyText(VerifyEvent event) {
    				event.doit = event.text.length() == 0
    						|| Character.isDigit(event.text.charAt(0));
    			}
    		});
    		shell.open();
    		while (!shell.isDisposed()) {
    			if (!display.readAndDispatch())
    				display.sleep();
    		}
    		display.dispose();
    	}
    }

    效果如下:

    本文为风林火山博客原创,转载请注明出处:www.flcoder.com
    validate