Java 如何在 Netbeans 中自动缩进?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1311912/
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 do I autoindent in Netbeans?
提问by Elazar Leibovich
In eclipse you can click Ctrl+Iat any line, and it'll automatically indent the line or group of lines according to the indentation scheme you chose in the settings.
在 Eclipse 中,您可以在任意行单击Ctrl+ I,它会根据您在设置中选择的缩进方案自动缩进一行或一组行。
I'm really missing this feature in Netbeans. Is there any equivalent feature?
我真的很想念 Netbeans 中的这个功能。有没有等效的功能?
I'm aware of Alt+Shift+Fbut it's not good enough. I want to indent a group of lines, and not all the file.
我所知道的Alt+ Shift+ F,但它不够好。我想缩进一组行,而不是所有文件。
采纳答案by varad
Open Tools-> Options-> Keymap, then look for the action called "Re-indent current line or selection" and set whatever shortcut you want.
打开 Tools-> Options-> Keymap,然后查找名为“重新缩进当前行或选择”的操作并设置您想要的任何快捷方式。
回答by Milhous
Ctrl
+Shift
+F
will do a format of all the code in the page.
Ctrl
+ Shift
+F
会做的所有代码的形式在页面中。
回答by Ensode
Shift
+ Alt
+ F
indents the whole file.
Shift
+ Alt
+F
缩进整个文件。
回答by OliBlogger
Select the lines you want to reformat (indenting), thenhit Alt+Shift+F. Only the selected lines will be reformatted.
选择要重新格式化(缩进)的行,然后按 Alt+Shift+F。只有选定的行会被重新格式化。
回答by rciafardone
I have netbeans 6.9.1 open right now and ALT+SHIFT+F indents only the lines you have selected.
我现在打开了 netbeans 6.9.1,ALT+SHIFT+F 只缩进您选择的行。
If no lines are selected then it will indent the whole document you are in.
如果未选择任何行,则它将缩进您所在的整个文档。
1 possibly unintended behavior is that if you have selected ONLY 1 line, it must be selected completely, otherwise it does nothing. But you don't have to completely select the last line of a group nor the first.
一种可能的意外行为是,如果您只选择了 1 行,则必须完全选择它,否则什么都不做。但是您不必完全选择组的最后一行或第一行。
I expected it to indent only one line by just selecting the first couple of chars but didn't work, yea i know i am lazy as hell...
我希望它只通过选择前几个字符来缩进一行,但没有用,是的,我知道我很懒惰......
回答by chenno
If you want auto-indent just like Emacs does it on TAB, i.e. indent the current line and move the cursor to the first non-whitespace character, do this:
如果您想像 Emacs 在 TAB 上那样自动缩进,即缩进当前行并将光标移动到第一个非空白字符,请执行以下操作:
- Go to Tools -> Options -> Editor -> Macros
- Create a new macro and call it something like "tabindent"
Insert the following macro code:
reindent-line caret-line-first-column caret-begin-line
Click "Set Shortcut" and press TAB
- 转到工具 -> 选项 -> 编辑器 -> 宏
- 创建一个新宏并将其命名为“tabindent”
插入以下宏代码:
reindent-line caret-line-first-column caret-begin-line
单击“设置快捷方式”并按 TAB
回答by Manu Manjunath
Pressing Alt+Shift+Findents your selection. If nothing's selected, it indents the whole file.
按Alt+ Shift+F缩进您的选择。如果未选择任何内容,则会缩进整个文件。
In newer versions of NetBeans, you can even format multiple files/folders at a time! In the Projects
window/sidebar, if you select one or more folders or files and press Alt+Shift+F. NetBeans asks "Recursively format selected files and folders?" and pressing OK will recursively format all the selected files/folders.
在较新版本的 NetBeans 中,您甚至可以一次格式化多个文件/文件夹!在Projects
窗口/边栏中,如果您选择一个或多个文件夹或文件并按Alt+ Shift+ F。NetBeans 询问“递归格式化选定的文件和文件夹?”,然后按 OK 将递归格式化所有选定的文件/文件夹。
On Mac OS X, shortcut is Ctrl+Shift+F. (note: it's Ctrland not ?)
在 Mac OS X 上,快捷方式是Ctrl+ Shift+ F。(注意:是Ctrl和不是?)
I've used above shortcuts on NetBeans 7 and 8.
我在 NetBeans 7 和 8 上使用了上述快捷方式。
回答by Mohammed Saqib Rajput
To format all the code in NetBeans, press Alt + Shift + F. If you want to indent lines, select the lines and press Alt + Shift + right arrow key, and to unindent, press Alt + Shift + left arrow key.
要在 NetBeans 中格式化所有代码,请按 Alt + Shift + F。如果要缩进行,请选择行并按 Alt + Shift + 右箭头键,要取消缩进,请按 Alt + Shift + 左箭头键。
回答by Jorgesys
for Java NetBeans 7.1 and later, even in NetBeans 8.0 (That i′m currently using) and later, the shortcut is:
对于 Java NetBeans 7.1 及更高版本,即使在 NetBeans 8.0(我目前使用的)及更高版本中,快捷方式是:
Alt+Shift+F
Alt+ Shift+F
if you look into the KeyMap
accessing from the menu: Tools
-> Options
-> Keymap
, the "action" is Formatdefined with the Shortcut : Alt+Shift+F
如果你看看KeyMap
从菜单中访问:Tools
- > Options
- > Keymap
,“动作”是格式与快捷键定义:Alt+ Shift+F
回答by Gabriel L.
Here's the complete procedure to auto-indent a file with Netbeans 8.
这是使用 Netbeans 8 自动缩进文件的完整过程。
First step is to go to Tools -> Options
and click on Editor button and Formatting tab as it is shown on the following image.
第一步是转到Tools -> Options
并单击编辑器按钮和格式选项卡,如下图所示。
When you have set your formatting options, click the Apply button and OK. Note that my example is with C++ language, but this also apply for Java as well.
设置格式选项后,单击应用按钮和确定。请注意,我的示例使用的是 C++ 语言,但这也适用于 Java。
The second step is to CTRL+ Aon the file where you want to apply your new formatting setting. Then, ALT+ SHIFT+ For click on the menu Source -> Format
.
第二步是在要应用新格式设置的文件上添加CTRL+ A。然后,ALT+ SHIFT+F或单击菜单上Source -> Format
。
Hope this will help.
希望这会有所帮助。