我正在使用 java 枚举来定义如何呈现带有按钮的模态窗口(Vaadin 处理呈现)。我的问题是,当我运行 gui 时,我的按钮每次都以随机顺序出现。所以我的问题是,因为我使用枚举集来固定我的按钮,那会是无序的吗?使其成为有序列表的最佳方法是什么?
我的设置枚举
public enum MODAL_SETTINGS {
NEW_MODAL_WINDOW("menu.context.new", "400", MODAL_BUTTON.SAVE, MODAL_BUTTON.CANCEL),
EDIT_MODAL_WINDOW("menu.context.modify","400", MODAL_BUTTON.UPDATE, MODAL_BUTTON.CANCEL),
DELETE_MODAL_WINDOW("menu.context.delete", "250", false, MODAL_BUTTON.DELETE, MODAL_BUTTON.CANCEL);
private EnumSet<MODAL_BUTTON> buttons;
private String caption;
private String width;
private boolean isResizable = true;
private MODAL_SETTINGS(String caption, String width, MODAL_BUTTON... buttons){
this.setCaption(caption);
this.setWidth(width);
this.buttons = EnumSet.copyOf(Arrays.asList(buttons));
}
private MODAL_SETTINGS(String caption, String width, boolean isResizable, MODAL_BUTTON... buttons){
this.setCaption(caption);
this.setWidth(width);
this.isResizable = isResizable;
this.buttons = EnumSet.copyOf(Arrays.asList(buttons));
}
public EnumSet<MODAL_BUTTON> getButtons(){
return buttons;
}
@Override
public String toString(){
String s = super.toString();
s=s.replaceAll("_", ".");
return s;
}
public void setCaption(String caption) {
this.caption = caption;
}
public String getCaption() {
return caption;
}
public void setWidth(String width) {
this.width = width;
}
public String getWidth() {
return width;
}
public boolean isResizable() {
return isResizable;
}
}
我的按钮枚举
public enum MODAL_BUTTON {
SAVE, UPDATE, CANCEL, DELETE;
}
最佳答案
使用 Enum.values()
而不是 EnumSet
:
Note that each enum type has a static values method that returns an array containing all of the values of the enum type in the order they are declared. This method is commonly used in combination with the for-each loop to iterate over the values of an enumerated type.
关于java枚举排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6069550/