I want to write program in which you can chance scene ( JPanel
) thanks to JComboBox
. I used ActionListener
, but it doesn't work.
At the beginning of constructor I defined panel as final, but it didn't help.
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
I suggest you to make panel
an attribute of your class. Then call panel
such as 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();
}
}
});
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.