问题

IDEA如何自动导入包

详细问题

项目开发中,笔者常常先将光标瞄准所需导入的类,后使用快捷键Alt+Enter进行导包操作。这种方式需要逐个处理,相对费事费力,IDEA如何自动导入所有程序所涉及的包呢

解决方案

1、左上角

F

i

l

e

File

File

\rightarrow

S

e

t

t

i

n

g

s

Settings

Settings(或快捷键

C

t

r

l

+

A

l

t

+

S

Ctrl+Alt+S

Ctrl+Alt+S)

2、依次点击

E

d

i

t

o

r

Editor

Editor

\rightarrow

G

e

n

e

r

a

l

General

General

\rightarrow

A

u

t

o

I

m

p

o

r

t

Auto Import

AutoImport(或搜索框搜索

A

u

t

o

I

m

p

o

r

t

Auto Import

AutoImport),点击

A

d

d

u

n

a

m

b

i

g

u

o

u

s

i

m

p

o

r

t

s

o

n

t

h

e

f

l

y

(必须)

Add unambiguous imports on the fly(必须)

Addunambiguousimportsonthefly(必须)和

O

p

t

i

m

i

z

e

i

m

p

o

r

t

s

o

n

t

h

e

f

l

y

Optimize imports on the fly

Optimizeimportsonthefly(非必须,建议勾选),具体操作如下

问题产生原因

笔者对于IDEA配置了解有限,不清楚上述配置

解决原因

通过对IDEA的配置即可解决,在此,笔者将配置信息进行解释 “Add unambiguous imports on the fly” 表示会在编写代码时自动添加唯一的导入语句。 “Optimize imports on the fly” 会在保存文件时自动优化导入语句,删除未使用的导入并按需添加导入。 因此,对于非唯一的包的导入,需要开发者自行指定,毕竟,编辑器仅仅通过你的类名无法唯一确定你所需要的包具体是哪一个。事实上,若读者仅仅需要IDEA自动导入包,仅仅勾选"Add unambiguous imports on the fly" 即可,但是为使代码更加简洁,笔者建议诸位读者可以将"Optimize imports on the fly" 也进行勾选。

原创不易 转载请标明出处 如果对你有所帮助 别忘啦点赞支持哈

推荐阅读

评论可见,请评论后查看内容,谢谢!!!评论后请刷新页面。