既然已经为插件创建了项目、包和视图类,就可以研究一些代码了。以下是 HelloWorldView 中需要的所有内容。将下面的内容复制到您创建的类中,替换自动生成的内容。
package com.example.helloworld;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.SWT;
import org.eclipse.ui.part.ViewPart;
public class HelloWorldView extends ViewPart {
Label label;
public HelloWorldView() {
}
public void createPartControl(Composite parent) {
label = new Label(parent, SWT.WRAP);
label.setText("Hello World");
}
public void setFocus() {
// set focus to my widget. For a label, this doesn't
// make much sense, but for more complex sets of widgets
// you would decide which one gets the focus.
}
}
视图部件创建将在 createPartControl 方法中呈示的窗口小部件。在此示例中,我们将创建 SWT 标签,并在其中设置“Hello World”文本。这是可创建的最简单的视图。