[英]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
設為班級的一個屬性。 然后調用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();
}
}
});
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.