JComboBox的ActionListener并初始化JPanel
ActionListener of JComboBox and initialize JPanel
感谢JComboBox
,我想编写一个可以在其中遇到场景(JPanel
)的程序。
我用了ActionListener
,但是没用
在构造函数的开头我将面板定义为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
我建议您将 panel
设为 class 的属性。
然后调用panel
如YourClass.this.panel
.
public class YourClass {
private JPanel panel;
public YourClass() {
// ...
scene.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String choice = String.valueOf(scene.getSelectedItem());
if(choice.equals("Sceneria") || choice.equals("Scene")) {
slider.setEnabled(false);
YourClass.this.panel = new JPanel();
YourClass.this.panel.revalidate();
YourClass.this.panel.repaint();
}
}
});
}
}
感谢JComboBox
,我想编写一个可以在其中遇到场景(JPanel
)的程序。
我用了ActionListener
,但是没用
在构造函数的开头我将面板定义为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
我建议您将 panel
设为 class 的属性。
然后调用panel
如YourClass.this.panel
.
public class YourClass {
private JPanel panel;
public YourClass() {
// ...
scene.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String choice = String.valueOf(scene.getSelectedItem());
if(choice.equals("Sceneria") || choice.equals("Scene")) {
slider.setEnabled(false);
YourClass.this.panel = new JPanel();
YourClass.this.panel.revalidate();
YourClass.this.panel.repaint();
}
}
});
}
}