Java netbeans 中的代码清理

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/956900/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 21:34:22  来源:igfitidea点击:

Code cleanup in netbeans

javaidenetbeansformatting

提问by Kapsh

Is there something similar to the Eclipse cleanup rules (Preferences > Java > Code Style > Clean Up) in NetBeans?

是否有类似于 NetBeans 中的 Eclipse 清理规则(首选项 > Java > 代码样式 > 清理)的内容?

The cleanup rules in eclipse will allow you to clean things up like organizing imports, removing unnecessary casts, adding missing override annotations etc.

eclipse 中的清理规则将允许您清理诸如组织导入、删除不必要的强制转换、添加丢失的覆盖注释等。

Also can you do that on a whole set of classes/packages instead of individual classes?

您也可以在整套类/包而不是单个类上执行此操作吗?

回答by soldier.moth

I know that netbeans points out these things automatically, but beyond merely pointing them out I don't know.

我知道 netbeans 会自动指出这些东西,但除了仅仅指出它们之外,我不知道。

回答by James McMahon

I don't know a way to do this en mass, but if you delete all imports from a source file and then right click in the source editor, you can select fix imports. This will import all the classes for you alphabetically, asking when it encounters package ambiguities.

我不知道有什么方法可以批量执行此操作,但是如果您从源文件中删除所有导入,然后在源编辑器中右键单击,则可以选择修复导入。这将按字母顺序为您导入所有类,并在遇到包歧义时询问。

As Soldier.moth pointed out, Netbeans will point out other issues, like casts and override annotations, in line, by use of a light bulb to the left of the source.

正如Soldier.moth 指出的那样,Netbeans 将通过使用源左侧的灯泡来指出其他问题,例如强制转换和覆盖注释。

回答by DR.

The equivalent of Eclipse's "Code Cleanup" in NetBeans is "Format". It's under Source > Format, and the keyboard shortcut is Alt+Shift+F (on Windows). Unfortunately, unlike Eclipse, it doesn't seem like this can be configured in NetBeans.

NetBeans 中 Eclipse 的“代码清理”的等价物是“格式”。它位于 Source > Format 下,键盘快捷键是 Alt+Shift+F(在 Windows 上)。不幸的是,与 Eclipse 不同的是,这似乎不能在 NetBeans 中进行配置。

回答by mKorbel

tools -> options -> Tab "Editor" -> Tab "Hints" -> select Java (talking about) in JComboBox

工具 -> 选项 -> Tab "Editor" -> Tab "Hints" -> 在 JComboBox 中选择 Java (talking about)

and then you'd see yellow ocean ..., great and quick from ver 6.9

然后你会看到黄色的海洋......,从 6.9 版开始,速度很快

回答by Tim Sparg

There is an Organize Imports pluginRead the DZone articlefor more info.

有一个Organize Imports 插件阅读DZone 文章了解更多信息。

EDIT: I see there is a bug reportto get this as part of the standard distribution.

编辑:我看到有一个错误报告可以将其作为标准发行版的一部分。

回答by Jér?me Verstrynge

Refactor> Inspect and Transform

Refactor> Inspect and Transform

Is there something similar to the Eclipse cleanup rules ((Preferences > Java > Code Style > Clean Up) in NetBeans?

是否有类似于 NetBeans 中的 Eclipse 清理规则((首选项 > Java > 代码样式 > 清理)?

In NetBeans 8.0, the powerful batch tool is Refactor> Inspect and Transform. See the tutorial page, Refactoring with Inspect and Transform in the NetBeans IDE Java Editor.

在 NetBeans 8.0 中,强大的批处理工具是Refactor> Inspect and Transform。请参阅教程页面在 NetBeans IDE Java 编辑器中使用检查和转换进行重构

Individual tools are also available:

还提供单独的工具:

  • Fixing imports
    For a single file, namespace, or project (depending on what's selected in Project window or has the focus): Source > Organize Imports (to sort and remove unused imports) or Source > Fix Imports (to sort, remove unused imports, and add missing imports). Or during every save: Tools > Options > Editor > On Save > Organize Imports.

  • Removal of trailing spaces
    For a single file (place the carret in the code file): Source > Remove Trailing Spaces. Or during every save: Tools > Options > Editor > On Save > Remove Trailing Whitespace.

  • Code Format
    For a single file, namespace, or project: Source > Format. (Customize the rules in Tools > Editor > Formatting). There is also a plugin called Jindent you can install (I have not used it myself). Or during every save: Tools > Options > Editor > On Save > Reformat.

  • 修复导入
    对于单个文件、命名空间或项目(取决于在“项目”窗口中选择的内容或具有焦点的内容):源 > 组织导入(排序和删除未使用的导入)或源 > 修复导入(排序、删除未使用的导入,并添加缺失的导入)。或者在每次保存期间:工具 > 选项 > 编辑器 > 保存时 > 组织导入。

  • 删除尾随空格
    对于单个文件(将插入符号放在代码文件中):源 > 删除尾随空格。或者在每次保存期间:工具>选项>编辑器>保存时>删除尾随空格。

  • 代码格式
    对于单个文件、命名空间或项目:源 > 格式。(自定义 中的规则Tools > Editor > Formatting)。还有一个叫Jindent的插件可以安装(我自己没用过)。或者在每次保存期间:Tools > Options > Editor > On Save > Reformat

By default, Netbeans will display hint icons next to problematic lines of code and in the scrollbar, allowing you to perform an automatic fix if desired. These can be configured via Tools > Options > Editor > Hints. Netbeans can search all problems in the project using Source > Inspect. Or, to reiterate, many of these problems can be batch fixed with Refactor > Inspect and Transform.

默认情况下,Netbeans 将在有问题的代码行旁边和滚动条中显示提示图标,允许您根据需要执行自动修复。这些可以通过配置Tools > Options > Editor > Hints。Netbeans 可以使用 Source > Inspect 搜索项目中的所有问题。或者,重申一下,其中许多问题可以通过Refactor > Inspect and Transform.

回答by DarVar

NetBeans 7.2 has Inspect & Transform refactoring to do this:

NetBeans 7.2 具有 Inspect & Transform 重构来执行此操作:

http://netbeans.org/kb/docs/java/editor-inspect-transform.html

http://netbeans.org/kb/docs/java/editor-inspect-transform.html

回答by Anil Chahal

For Netbeans use Ctrl+Shift+Ito remove unused import from the file.

对于 Netbeans,使用Ctrl+Shift+I从文件中删除未使用的导入。

回答by Vinayak G

If you just want to make your code have proper indentations on a whole project, simply click the project name, go to "Source," and then click "Format." Hope this helps!

如果您只想让您的代码在整个项目中具有适当的缩进,只需单击项目名称,转到“源”,然后单击“格式”。希望这可以帮助!