Java - How to set String as static int

后端 未结 4 585
予麋鹿
予麋鹿 2021-01-28 08:24

I have a method that accepts only a String.

public void setVerticalAlignment(String align) {
            ...
    gd.verticalAlignment = align;   // accepts only          


        
4条回答
  •  灰色年华
    2021-01-28 08:53

    If you use Java 7 you can always use switch on Strings:

    switch (align) {
        case "SWT.TOP":
            gd.verticalAlignment = SWT.TOP;
        /* etc */
    }
    

    Being honest I would avoid using strings like "STW.TOP". If I really had to store alignment state in the other way than just int I would use enums which might be used in switch in older versions of Java.

提交回复
热议问题