如何从不同方法中定义的内部类中检索变量?

时间:2021-11-23 23:29:04

So right now I have the following:

所以现在我有以下内容:

public class Panel extends JPanel {

int size;

public Panel()
{
    JButton newBut = new JButton();

    newBut.addActionListener(new ActionListener()
    {
        @Override
        public void actionPerformed(ActionEvent e)
        {
            boolean validInput = false;
            while (!validInput)
            {
                String input = JOptionPane.showInputDialog("Please specify a number:");
                try{
                    size = Integer.parseInt(input);
                    validInput = true;
                } catch (NumberFormatException ex){
                    JOptionPane.showMessageDialog(new JFrame(), "Invalid Input!", "ERROR",JOptionPane.ERROR_MESSAGE);
                }

                if (input == null)
                {
                    validInput = true;
                }
            }
        }
    });
}

Now as you can see from the code, I am trying to parse the input from the user as an integer, and then store that value inside my global variable "size". However, when I try to do this I get the error:

现在您可以从代码中看到,我试图将用户的输入解析为整数,然后将该值存储在我的全局变量“size”中。但是,当我尝试这样做时,我得到错误:

Cannot refer to a non-final variable inside an inner class defined in a different method

Setting size to final is not an option for me, since I need size to be able to change each time the user inputs a new value. So I honestly have no idea how I'm supposed to retrieve the size variable from the inner method. Any help would be mightily appreciated.

将大小设置为final对我来说不是一个选项,因为我需要大小才能在每次用户输入新值时进行更改。所以老实说,我不知道我应该如何从内部方法中检索size变量。任何帮助都会受到极大的赞赏。

2 个解决方案

#1


0  

Yes @EJP is right....I have also tried in Java 8 using netbeans...for me it is working perfectly...but if still you get problem then implement ActionListener to your class and then make a separate method for this...here is the code....

是的@EJP是对的......我也尝试过在Java 8中使用netbeans ...对我来说它工作得很好......但如果你仍然遇到问题,那么将ActionListener实现到你的类,然后为此创建一个单独的方法......这是代码....

public class Panel extends JPanel implements ActionListener{

    int size;

    public Panel()
    {
        JButton newBut = new JButton("Button");  //give some name just to check..
        newBut.addActionListener(this);
        this.add(newBut);  //you have not added your button to the Panel..so I did it
    }

    @Override
    public void actionPerformed(ActionEvent e) {  //same method just outside the inner class
        boolean validInput = false;   //all the functionality is same as your nothing is changed
        while (!validInput)
        {
            String input = JOptionPane.showInputDialog("Please specify a number:");
            try{
                size = Integer.parseInt(input);
                validInput = true;
            } catch (NumberFormatException ex){
                JOptionPane.showMessageDialog(new JFrame(), "Invalid Input!", "ERROR",JOptionPane.ERROR_MESSAGE);
            }

            if (input == null)
            {
                validInput = true;
            }
        }
    }
    }

I think it may help you...

我想它可以帮到你......

#2


-1  

You could cheat and use an AtomicInteger or similar, and call its set(int) method. But I would instead reconsider your design -- why are you trying to stash this value in a global anyways?

您可以欺骗并使用AtomicInteger或类似方法,并调用其set(int)方法。但我会重新考虑你的设计 - 为什么你试图在全球范围内保留这个价值呢?

Also, what's with the floating

还有,浮动是什么

if (input == null)
{
    validInput = true;
}

block?

#1


0  

Yes @EJP is right....I have also tried in Java 8 using netbeans...for me it is working perfectly...but if still you get problem then implement ActionListener to your class and then make a separate method for this...here is the code....

是的@EJP是对的......我也尝试过在Java 8中使用netbeans ...对我来说它工作得很好......但如果你仍然遇到问题,那么将ActionListener实现到你的类,然后为此创建一个单独的方法......这是代码....

public class Panel extends JPanel implements ActionListener{

    int size;

    public Panel()
    {
        JButton newBut = new JButton("Button");  //give some name just to check..
        newBut.addActionListener(this);
        this.add(newBut);  //you have not added your button to the Panel..so I did it
    }

    @Override
    public void actionPerformed(ActionEvent e) {  //same method just outside the inner class
        boolean validInput = false;   //all the functionality is same as your nothing is changed
        while (!validInput)
        {
            String input = JOptionPane.showInputDialog("Please specify a number:");
            try{
                size = Integer.parseInt(input);
                validInput = true;
            } catch (NumberFormatException ex){
                JOptionPane.showMessageDialog(new JFrame(), "Invalid Input!", "ERROR",JOptionPane.ERROR_MESSAGE);
            }

            if (input == null)
            {
                validInput = true;
            }
        }
    }
    }

I think it may help you...

我想它可以帮到你......

#2


-1  

You could cheat and use an AtomicInteger or similar, and call its set(int) method. But I would instead reconsider your design -- why are you trying to stash this value in a global anyways?

您可以欺骗并使用AtomicInteger或类似方法,并调用其set(int)方法。但我会重新考虑你的设计 - 为什么你试图在全球范围内保留这个价值呢?

Also, what's with the floating

还有,浮动是什么

if (input == null)
{
    validInput = true;
}

block?