@wjans 的答案对于普通枚举工作正常,但不适用于带参数的枚举。为了稍微扩展他的答案,以下是在 Eclipse Juno 中为我提供最合理格式的设置:
-
Window > Preferences > Java > Code Style > Formatter
- 点击
Edit
- 选择
Line Wrapping 标签
- 选择
enum声明树节点
- 将
Line wrapping policy 设置为 Wrap all elements, every element on a new line (...),因此现在括号中显示的是 3 of 3。
- 取消选中
Force split, even if line shorter than maximum line width (...),因此现在括号中显示的是 3 of 3。
- 选择
Constants树节点
- 检查
Force split, even if line shorter than maximum line width
这会将枚举树节点的 3 个子节点设置为相同的包装策略,以及相同的强制拆分策略(Constants 树节点除外),因此您的带参数的枚举将在各自的行中格式化。参数只有在超过最大线宽时才会换行。
示例:
@wjans
enum Example {
CANCELLED,
RUNNING,
WAITING,
FINISHED
}
enum Example {
GREEN(
0,
255,
0),
RED(
255,
0,
0)
}
上述解决方案:
enum Example {
CANCELLED,
RUNNING,
WAITING,
FINISHED
}
enum Example {
GREEN(0, 255, 0),
RED(255, 0, 0)
}