【发布时间】:2015-05-30 10:41:03
【问题描述】:
感谢JComboBox,我想编写一个程序,让您可以在其中遇到场景(JPanel)。
我用了ActionListener,但是不行。
在构造函数的开头,我将 panel 定义为 final,但没有帮助。
scene.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String choice = String.valueOf(scene.getSelectedItem());
if(choice=="Sceneria"||choice=="Scene"){
slider.setEnabled(false);
panel = new JPanel();// problem here
}
}
});
错误
The final local variable panel cannot be assigned, since it is defined in an enclosing type
【问题讨论】:
-
将
==更改为equals(String) -
我的如果没问题。我在其他课上也做过类似的事情,而且效果很好。问题在这里:panel = new JPanel();
-
1) 为了尽快获得更好的帮助,请发布MCVE(最小完整可验证示例)或SSCCE(简短、自包含、正确示例)。 2) 但改用
CardLayout,如this answer所示。
标签: java swing compiler-errors actionlistener jcombobox