Java 在 Eclipse 中创建新类时如何更改自动生成的代码
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/501648/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to change auto-generated code when creating new class in Eclipse
提问by Click Upvote
Whenever I create a new Java file in Eclipse and check off the option to add public static void main(String args[]), this code is generated:
每当我在 Eclipse 中创建一个新的 Java 文件并选中添加选项时public static void main(String args[]),就会生成以下代码:
public class Test {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
}
}
How do i:
我如何能:
- Remove the
@param argscomment - Change the indentation so each { is on a line of its own
- Remove the TODO auto generated comment
- 删除
@param args评论 - 更改缩进,使每个 { 都在自己的一行上
- 删除 TODO 自动生成的注释
采纳答案by pek
The indention is a formatting issue while the comments is a template issue.
缩进是格式问题,而注释是模板问题。
The templates are in Window -> Preferences -> Java -> Code Style -> Code Templates. Browse all of them and look for the things you would like to change.
模板位于Window -> Preferences -> Java -> Code Style -> Code Templates。浏览所有这些并查找您想要更改的内容。
The Formatter is a little bit more complicated. You find it under Window -> Preferences -> Java -> Code Style -> Formatter. There are tons of options there but I'll just answer your question.
格式化程序稍微复杂一些。您可以在Window -> Preferences -> Java -> Code Style -> Formatter下找到它。那里有很多选择,但我只会回答你的问题。
- Templates -> Comments -> Methods -> Edit and delete everything
- Formatter -> Edit -> Braces Tab -> Change which situations you want
- Templates -> Code -> Method Body -> Edit and delete everything
- 模板 -> 评论 -> 方法 -> 编辑和删除所有内容
- 格式化程序 -> 编辑 -> 大括号选项卡 -> 更改您想要的情况
- 模板 -> 代码 -> 方法体 -> 编辑和删除所有内容
回答by Fortyrunner
Eclipse has a lotof configuration options. Take a look in the Windows | Preferences dialog (or the Eclipse Preferences pan on OSX).
Eclipse 有很多配置选项。看看在Windows | Preferences 对话框(或 OSX 上的 Eclipse Preferences pan)。
If you dig deep enough - you'll find the options under Java | Code Style.
如果你深入挖掘 - 你会在 Java | 下找到选项。代码风格。
回答by waney
To configure it go to the : window->preferences->java->code style->code templates
要对其进行配置,请转到:window->preferences->java->code style->code templates
回答by Miserable Variable
Also make sure you check "Use code formatter" in Java > Editor > Templates
还要确保在 Java > Editor > Templates 中选中“Use code formatter”
回答by Bhanuprakasha
- In Eclipse Go to Window->Preferences
- In Left Panel, Select Java->Code Style->Code Template
- Under "Configure generated code and comments", Expand Comments-> select Methods,Click Edit Remove or replace the pattern ( * @param args),Click OK
- Under "Configure generated code and comments", Expand Code-> select Method Body,Click Edit Remove or replace the pattern ( // TODO Auto-generated...),Click OK
- Click OK !
- 在 Eclipse 中转到 Window->Preferences
- 在左侧面板中,选择 Java->Code Style->Code Template
- 在“配置生成的代码和注释”下,展开注释->选择方法,单击编辑删除或替换模式(*@param args),单击确定
- 在“配置生成的代码和注释”下,展开代码->选择方法主体,单击编辑删除或替换模式(// TODO Auto-generated...),单击确定
- 单击确定!

