Eclipse 简明教程

Eclipse - Code Templates

Using Code Templates

代码模板通过在编辑器中插入代码来帮助减少键入时间。每个代码模板都给定了一个简短的文字。在编辑器窗口中键入文字并按下 Ctrl + 空格键,会出现一个对话框,从中可以选择与此文字相关联的代码模板。

Code templates help reduce typing time by inserting code into the editor. Each code template is given a short literal. Typing the literal into the editor window and press Ctrl + Space brings up a dialog box from which a code template associated with that literal can be selected.

在 java 编辑器窗口中某个方法的主体中键入 for 并单击 Ctrl + 空格键会显示与 for 关联的代码模板。

Typing for within the body of a method in the java editor window and clicking Ctrl + space shows the code templates associated with for.

code templates 1

选择 for - 在数组上迭代会为 method 主体中早先声明的 depArray 遍历插入代码。

Selecting for - iterate over array inserts code for iterating through depArray which was declared earlier in the method body.

另一个有用的代码模板文字是 sysout ,它可用于插入 System.out.println() 方法调用。

Another useful code template literal is sysout which can be used to insert the System.out.println() method call.

code templates 2

Modifying/Adding code templates

您可以使用“首选项”对话框添加新的代码模板。通过单击“窗口”菜单并选择“首选项”菜单项,打开“首选项”对话框。要在“过滤器”文本框中找到“模板”页面,请键入“模板”。

You can add new code templates using the Preferences dialog box. Bring up the Preferences dialog box by clicking on the Windows menu and selecting the Preferences menu item. To locate the Templates page type Templates into the filter text box.

code templates 3

您可以通过阅读 Eclipse 帮助来了解有关可用于自定义所插入代码的变量的更多信息。要打开帮助窗口,请单击“帮助”菜单并选择“帮助内容”。在搜索文本框中输入 Java Editor Template Variables。在左侧列表中选择 Java Editor Template Variables。

You can read more about the variables that can be used to customize the code that is inserted by reading the Eclipse help. To bring up the help window click on the Help menu and select Help Contents. In the search text box enter Java Editor Template Variables. From the list on the left hand side select Java Editor Template Variables.

code templates 4