繁体   English   中英

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设为班级的一个属性。 然后调用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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM