Eclipse Java代码格式化 [英] Eclipse Java code formatting

查看:259
本文介绍了Eclipse Java代码格式化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在写一个GUI程序。



它有几个JPanels控制其他组件。如果我的代码看起来像这样,这将是非常好的:

 父组件代码
子组件代码
孙子组件代码

而不是这样:

 父组件代码
子组件代码
孙子组件代码

它会使代码更加可读。是(是)有一些字符,我可以放入我的代码,Java将忽略,但Eclipse会检测到它作为代码的一部分必须有额外的缩进?



我知道我可以手动完成所有操作,但我经常自动设置缩进(ctrl + a,ctrl + i)。这样我就不用担心直接缩进了,我可以让Eclipse为我管理它。然而,这也使我自己做出的任何自定义更改都是无效的。



添加{}执行伎俩,但也会影响java读取代码的方式,没有真正的工作。



也许有一些插件可用于此?
可以检测线条的东西,例如

  // {
/ pre>

  //} 

然后包含在其中的缩进代码?

解决方案

选择正确的开启和关闭大括号的文本块,然后按 Ctrl + Shift + F 将格式化所选的文本块。



Morevoer在eclipse中可以转到窗口 - > 首选项 - > Java - > 代码样式 - > 格式化程序。在这里,您可以创建一个新的格式化配置文件并分配很多自定义格式规则。



易于参考的屏幕截图




I'm writting a GUI program.

It has several JPanels that conatin other components. It would be really nice if my code would look like this:

parent component code
    child component code
        grandchild component code

instead of this:

parent component code
child component code
grandchild component code

It would make the code far more readable. Is(Are) there some character(s) I can put in my code that Java will ignore, but Eclipse would detect it as a part of code that has to have additional indentation?

I know I can do all this manually, but I often set indentation automatically (ctrl+a, ctrl+i). That way I don't have to worry about indentation directly, I can make Eclipse manage it for me. However, that also nullifies any "custom" changes I made myself.

Adding {} does the trick, but it also influences on the way java reads the code, so that doesn't really work.

Maybe there are some plug-ins available for this? Something that would detect lines like e.g.

//{

and

//}

and then indent code that is enclosed in them?

解决方案

Select a block of text with the proper opening and closing braces, and then pressing Ctrl+Shift+F will format the selected block of text.

Morevoer, in eclipse you can go to Window -> Preferences -> Java -> Code Style -> Formatter. Here you can create a new formatter profile and assign a lot of your custom formatting rules.

Screenshot for easy reference

这篇关于Eclipse Java代码格式化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆