自动将注释附加到 Netbeans 中自动插入的大括号
我真的很欣赏 NetBeans 如何自动在我正在处理的代码中输入右括号。它是否有可能进一步简化如下代码的开发?:
public class Test
{
public static void main(String[] Args)
{
//Some code here
while(true)
{
//Some more code here
}//end while
}//end main
}//end class
I really appreciate how NetBeans automatically enters closing brackets to the code I'm working on. Would it happen to be possible for it to further streamline the development of code that looks like this?:
public class Test
{
public static void main(String[] Args)
{
//Some code here
while(true)
{
//Some more code here
}//end while
}//end main
}//end class
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
不幸的是,似乎没有办法自定义 NetBeans 添加右括号时生成的内容:在“工具”->“选项”->“编辑器”->“代码完成”中只有一个复选框用于指示是否执行此操作。
这是下一个最佳解决方案:
NetBeans 具有“代码模板”功能,它是预定义代码片段,当您键入一些预定义缩写然后按 Tab 键时会自动生成。例如,如果您键入
if
并点击 Tab,您的编辑器应生成以下代码:您可以根据需要修改这些代码模板或创建自己的新代码模板。转到工具->选项->编辑器->代码模板。您将看到一个广泛的模板列表,每个模板都匹配一个缩写,以及一个用于更改它们的小编辑框。如果向下滚动到“if”,您将看到这个定义:
您所需要做的就是修改它以生成您想要的内容:
您可以对其他语句(例如
while
)执行类似的操作。您会注意到,每个语句通常有多个基于不同模式的模板,因此这可能会变得乏味,具体取决于您想要的彻底程度。该解决方案也适用于
main
方法,因为为其定义了一个模板 - 输入psvm
并按 Tab 键生成它。对于其他任意方法,有一个带有缩写m
的模板。对于顶级班级来说,达到这种效果会有所不同。转到工具->模板。在这里您将看到另一组可以探索和修改的模板。不同之处在于这些是针对文件而不是代码片段。展开 Java 并打开 Java 类模板文件。您将看到以下内容:
您可以再次根据需要修改此模板:
甚至:
这将为名为
Foo
的类添加//end Foo
Unfortunately there doesn't seem to be a way to customize what is generated when NetBeans adds the closing bracket: there is just a checkbox for whether to do this or not in Tools->Options->Editor->Code Completion.
Here is the next best solution:
NetBeans features "code templates", which are snippets of predefined code that are automatically generated when you type some predefined abbreviation and then hit tab. For example, if you type
if
and hit tab, your editor should generate the following code:You can modify these code templates to your needs or create new ones of your own. Go to Tools->Options->Editor->Code Templates. You will see an extensive list of templates, each matching an abbreviation, and a small edit box to change them. If you scroll down to "if" you'll see this definition:
All you need to do is modify it to generate what you desire:
You can do something similar for other statements such as
while
. You'll notice there often are multiple templates per statement based on different patterns, so this could get tedious depending on how thorough you want to be.This solution also works well for the
main
method, since there is a template defined for it - typingpsvm
and tabbing generates it. For other arbitrary methods, there is a template with the abbreviationm
.Achieving this effect for a top-level class will be different. Go to Tools->Templates. Here you'll see another set of templates which you can explore and modify. The difference is that these are for files rather than code snippets. Expand Java and open the Java Class template file. You'll see the following:
Once again, you can modify this template to your needs:
Or even:
which will put
//end Foo
for a class namedFoo