Java 有什么方法(或快捷方式)可以像在 Eclipse 中一样在 IntelliJ IDEA 中自动导入类?

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

Any way (or shortcut) to auto import the classes in IntelliJ IDEA like in Eclipse?

javaeclipseintellij-ideakeyboard-shortcutshotkeys

提问by bywronski

In Eclipse, while coding in Java and press Ctrl+ Shift+ Oauto import all the Classes automatically.
In NetBeans, this is done with Ctrl+ Shift+ I.

在Eclipse中,而编码的Java并按Ctrl+ Shift+O自动导入所有的类自动。
在NetBeans中,这是用做Ctrl+ Shift+ I

Is any way to do this in IntelliJ IDEA?

有什么办法可以在 IntelliJ IDEA 中做到这一点?

I searched an equivalent shortcut in google, StackOverflow, IntelliJ IDEA configuration and in the official IntelliJ website Keyboard Shortcuts You Cannot Miss

我在谷歌、StackOverflow、IntelliJ IDEA 配置和 IntelliJ 官方网站键盘快捷键中搜索了一个等效的快捷方式,你不能错过

采纳答案by Javaru

IntelliJ IDEA does not have an action to add imports. Rather it has the ability to do such as you type. If you enable the "Add unambiguous imports on the fly" in Settings > Editor > General > Auto Import, IntelliJ IDEA will add them as you type without the need for any shortcuts. You can also add classes and packages to exclude from auto importing to make a class you use heavily, that clashes with other classes of the same name, unambiguous.

IntelliJ IDEA 没有添加导入的操作。相反,它有能力像你打字那样做。如果您在Settings > Editor > General > Auto Import 中启用“即时添加明确的导入” ,IntelliJ IDEA 将在您键入时添加它们,而无需任何快捷方式。您还可以添加类和包以从自动导入中排除,以使您经常使用的类与同名的其他类发生冲突,明确无误。

For classes that are ambiguous (or is you prefer to have the "Add unambiguous imports on the fly" option turned off), just type the name of the class (just the name is OK, no need to fully qualify). Use code completion and select the particular class you want:

对于有歧义的类(或者您更喜欢关闭“动态添加无歧义的导入”选项),只需键入类的名称(只需名称即可,无需完全限定)。使用代码完成并选择您想要的特定类:

enter image description here

在此处输入图片说明

Notice the fully qualified names to the right. When I select the one I want and hit enter, IDEA will automatically add the import statement. This works the same if I was typing the name of a constructor. For static methods, you can even just keep typing the method you want. In the following screenshot, no "StringUtils" class is imported yet.

请注意右侧的完全限定名称。当我选择我想要的并按回车键时,IDEA 会自动添加导入语句。如果我输入构造函数的名称,这也是一样的。对于静态方法,您甚至可以继续输入所需的方法。在以下屏幕截图中,尚未导入“StringUtils”类。

enter image description here

在此处输入图片说明

Alternatively, type the class name and then hit Alt+Enteror ?+Enterto "Show intention actions and quick-fixes" and then select the import option.

或者,键入类名,然后点击Alt+Enter?+Enter以“显示意图操作和快速修复”,然后选择导入选项。

Although I've never used it, I thinkthe Eclipse Code Formatterthird party plug-in will do what you want. It lists "emulates Eclipse's imports optimizing" as a feature. See its instructionsfor more information. But in the end, I suspect you'll find the built in IDEA features work fine once you get use to their paradigm. In general, IDEA uses a "develop by intentions" concept. So rather than interrupting my development work to add an import statement, I just type the class I want (my intention) and IDEA automatically adds the import statement for the class for me.

虽然我从来没有使用过它,我Eclipse代码格式化第三方插件将做你想做的。它将“模拟 Eclipse 的导入优化”列为一项功能。有关更多信息,请参阅其说明。但最后,我怀疑一旦您习惯了它们的范例,您会发现内置的 IDEA 功能可以正常工作。一般来说,IDEA 使用“用意图开发”的概念。因此,与其中断我的开发工作来添加 import 语句,我只需键入我想要的类(我的意图),IDEA 就会自动为我添加该类的 import 语句。

回答by teknopaul

Not all at once. But you can press

不是一下子。但是你可以按

Alt+ Enter

Alt+ Enter

People assume it only works when you are at the particular item. But it actually works for "next missing type". So if you keep pressing Alt+ Enter, IDEA fixes one after another until all are fixed.

人们认为它仅在您使用特定项目时才有效。但它实际上适用于“下一个缺失类型”。因此,如果您一直按Alt+ Enter,IDEA 会一个接一个地修复,直到全部修复。

回答by Wuaner

Seems like IntelliJ IDEA will import missed class automatically, and you can import them by hit Alt+ Entermanually.

好像 IntelliJ IDEA 会自动导入丢失的类,你可以通过 hit Alt+Enter手动导入它们。

回答by comfytoday

I think the best solution, though not exactly the same as Eclipse/Netbeans, is to change the 'Optimize Imports' settings.

我认为最好的解决方案是更改“优化导入”设置,尽管与 Eclipse/Netbeans 不完全相同。

Under Preferences > Editor > General > Auto Import

在下面 Preferences > Editor > General > Auto Import

Set Add unambiguous imports on the fly

Add unambiguous imports on the fly

Edit: Using this method, when there areambiguous imports, IntelliJ will let you know, and you can then use Alt+ Entermethod outlined in the answer by Wuaner

编辑:使用这种方法,当有明确的导入时,IntelliJ 会让你知道,然后你可以使用Wuaner答案中概述的Alt+Enter方法

I find that, almost always, the most appropriate Import is at the top of the list.

我发现,几乎总是,最合适的导入位于列表的顶部。

回答by Dexter

Another option is to ask IDEA to behave like eclipse with eclipse shortcut keys. You can use all eclipse shortcutsby enabling this.

另一种选择是使用 eclipse 快捷键要求 IDEA 表现得像 eclipse。您可以通过启用此功能来使用所有 eclipse 快捷方式

Here are the steps:

以下是步骤:

1- With IDEA open, press Control + `. Following options will be popped up.

1- 在 IDEA 打开的情况下,按Control + `。将弹出以下选项。

enter image description here

在此处输入图片说明

2- Select Keymap. You will see another pop-up. Select Eclipse there.

2- 选择键盘映射。您将看到另一个弹出窗口。在那里选择 Eclipse。

enter image description here

在此处输入图片说明

3- Now press Ctrl + Shift + O. You are done!

3-现在按Ctrl + Shift + O你完成了!

回答by Lyncean Patel

Can't import all at once but can use following combination:

不能一次全部导入,但可以使用以下组合:

ALT+ Enter--> Show intention actions and quick-fixes.

ALT+ Enter--> 显示意图操作和快速修复。

F2--> Next highlighted error.

F2--> 下一个突出显示的错误。

回答by NarendraR

Use control+option+Lto auto import the package and auto remove unused packages on Mac

使用control+ option+L在Mac上自动导入的包和经销商删除未使用的包