2012-12-20 26 views
0

的状态我有设置在我的GUI的方法

然后,我有这样的一些其他类的对象使用EnumStates

CallmeClass obj; 

,当我尝试设置状态与JComboBox中的这样

obj.setState(state.getSelectedItem()); 

我得到的编译错误

结果的枚举

1.所需的状态,但找到的对象

所以我的问题是is there a way to make the setState take as argument state.getSelectedItem() withouth changing the return type of the method setState() or re declaring the enums in the gui。谢谢。

+0

尝试铸造state.getSelectedItem()来所需的状态对象 – MadProgrammer

+0

向我们显示您的代码和确切的编译器错误消息。你所展示的三行代码已经没有多大意义了。 –

回答

2

我猜你的声明setState是seomthing这样的:

public void setState(State state){ 
    ... 
} 

的问题是,JComboBox可无类型(至少它是这样,直到Java7)。因此getSelectedItem()总是返回需要转换为您的类型的对象。所以,你可以做,当你得到该项目的投:

obj.setState((State)state.getSelectedItem()); 

或者你可以改变你的方法声明为对象,做你的施法有:

public void setState(Object state){ 
    if(state instanceof State){ 
     State realState = (State)state; 
     ... 
    } 
} 
相关问题