简体   繁体   English

Java:JComboBox将不接受enum.values()

[英]Java: JComboBox won't accept enum.values()

So I'm working on a practice program where I'm trying to use an Enum in a jcombobox. 所以我正在一个练习程序中尝试在jcombobox中使用枚举。 I believe this is a good decision for my program because the options in the combo box are constants, and they also have alternate values assigned to them (a string name in addition to the constant value), so I felt that enum was the best way to go. 我认为这对我的程序是个不错的决定,因为组合框中的选项是常量,并且它们还具有为其分配的备用值(除了常量值外,还包含字符串名称),因此我觉得枚举是最好的方法去。

Unfortunately, I can't get the combo box to accept the full list of values from my enum constant. 不幸的是,我无法使组合框接受我的枚举常量的值的完整列表。 I'm pasting my full code below. 我在下面粘贴我的完整代码。 What am I doing wrong here? 我在这里做错了什么?

public enum CurrencyTypes{

    USD ("US Dollars"),
    BPS ("British Pound Sterling"),
    E ("European Euros"),
    RR ("Russian Rubles"),
    JY ("Japanese Yen"),
    CY ("Chinese Yuan"),
    IR ("Indian Rupees"),
    NIS ("New Israeli Shekels");

    private String typeName;


    private CurrencyTypes(String typeName){
        this.typeName = typeName;
    }

    public String getTypeName(){
        return typeName;
    }

}

In the Driver class below, the program is failing when I try and initialize currencyBox with the list of values from CurrencyTypes.values(). 在下面的Driver类中,当我尝试使用CurrencyTypes.values()中的值列表初始化currencyBox时,程序失败。 It compiles fine, but when I run the program I get java.lang.ExceptionInInitializerError and it crashes. 它可以正常编译,但是当我运行该程序时,我得到java.lang.ExceptionInInitializerError并崩溃。

public class AccountDriver{

    private String[] stringArray = new String[] { "", "test1", "test2", "test3" };
    private JComboBox<String> stringBox;
    private JComboBox<CurrencyTypes> currencyBox;
    private JLabel stringSelection;
    private String stringResult;
    private JLabel etSelection;
    private CurrencyTypes[] currencyArray;
    private ArrayList<CurrencyTypes> currencyArrayL;


    public AccountDriver(){


        JFrame testFrame = new JFrame();
        testFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);


        stringBox = new JComboBox<String>(stringArray);
        stringBox.addItemListener(new StringBoxListener());


        stringSelection = new JLabel(stringResult);


        currencyBox = new JComboBox<CurrencyTypes>(CurrencyTypes.values());


        testFrame.setLayout(new GridLayout(2, 2));
        testFrame.add(stringBox);
        testFrame.add(stringSelection);
        testFrame.add(currencyBox);

        testFrame.setVisible(true);
        testFrame.pack();
    }

    public static void main(String[] args){
        new AccountDriver();
    }

    private class StringBoxListener implements ItemListener{
        public void itemStateChanged(ItemEvent e){
            stringResult = String.valueOf(stringBox.getSelectedItem());
            System.out.println(stringResult);
            stringSelection.setText(stringResult);
        }
    }

    private class CurrencyBoxListener implements ItemListener{
        public void itemStateChanged(ItemEvent e){

        }
    }

}

I don't know anything about ENUMs but... A combo box render requires a toString() implementation to display the values in the combo box. 我对ENUM一无所知,但是...组合框渲染需要toString()实现才能在组合框中显示值。 Maybe this needs to be implemented in the enum? 也许这需要在枚举中实现?

and they also have alternate values assigned to them (a string name in addition to the constant value), 并且还为它们分配了替代值(除了常量值外还有一个字符串名称),

You can also create a custom POJO with the two values to dislay in the combo box. 您还可以使用两个值创建一个自定义POJO,以将其放置在组合框中。 See Combo Box With Custom Renderer for a simple example. 有关简单示例,请参见带有自定义渲染器的组合框

尝试:

new JComboBox(CurrencyTypes.values());

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM