无法将JButton值分配给变量

时间:2019-05-10 19:55:59

标签: java jbutton return-value addeventlistener

我试图将JButton的文本值存储在变量中,但是引发了"local variable referenced from an inner class must be final or effectively final"异常。

public String NumberList()
    {
        String VAL= new String();
        int HalnBut=5;
        JFrame Fram=new JFrame();
        JLayeredPane lyp=new JLayeredPane();
        JButton[] options=new JButton[10];
        Fram.setBounds(400,100,555,100);
        Fram.add(lyp);
        lyp.setBounds(400,100,700,200);
        Fram.setVisible(true);
                for(int i=1;i<=9;i++)
        {
                options[i]=new JButton(String.valueOf(i));
                lyp.add(options[i]);
                options[i].setVisible(true);
                options[i].setBounds(HalnBut, 0, 50, 50);
                options[i].addActionListener(new ActionListener()
                {
                    @Override
                    public void actionPerformed(ActionEvent e)
                    {
                        String value;
                        value=options[i].getText();/*EXception of final variable as shown above
                    }
                });
                HalnBut=HalnBut+60;
        }

    }

我正在尝试将JButton文本值存储到VAL中并将其返回到另一个事件侦听器中

0 个答案:

没有答案