为了使 RFT 能够捕捉到 SWT 程序的对象,您必须将清单 2 的代码添加到 SWT 应用程序的源代码中:
清单 2.
try { com.rational.test.ft.bootstrap.Bootstrap.enableSwtUi(display); } catch (Throwable e){ } |
其中:Bootstrap
类是 RFT 的运行时环境中 rational_ft_bootstrap.jar
文件包中的类。以上节的 SWT 程序为例,修改后的代码如下:
清单 3.
public class Calculator extends Composite { public Calculator(Composite parent, int style) { super(parent, style); } private CalculatorModel model = new CalculatorModel(); private Text lcd = new Text(this, SWT.BORDER | SWT.RIGHT); //省略…… public static void main(String[] args) { Display display = Display.getDefault(); Shell shell = new Shell(display); try { Bootstrap.enableSwtUi(display); } catch(Throwable throwable) { } shell.setText("Calculator"); shell.setLayout(new FillLayout()); Calculator cal = new Calculator(shell, SWT.NONE); cal.init(); shell.pack(); shell.open(); while (!shell.isDisposed()) { if (!display.readAndDispatch()) { display.sleep(); } } display.dispose(); } } |
并且为了能够编译和运行,您还必须把 rational_ft_bootstrap.jar
添加到 class path
中,确保 SWT 应用程序运行时能够找到 Bootstrap
类。重新编译运行上面的例子,然后打开 RFT 的对象捕捉器,左键按住手形的对象选择器不放,将鼠标移到 SWT 应用程序的按钮对象上,如图 5 所示,SWT 对象被红框包围,并在下方显示出该对象的类型是:org.eclipse.swt.widgets.Button
,说明 RFT 可以成功捕捉到 SWT 的对象了。
图 5. 捕捉 SWT 对象