简体   繁体   English

如何在不使用“在范围内定义的局部变量x必须是最终的…”的情况下使用数组索引

[英]How to use array index without “Local variable x defined in an enclosing scope must be final…”

I'm writing a simple program, and there is one problem I cannot solve. 我正在编写一个简单的程序,但是有一个我无法解决的问题。

I am creating textFields with such loop: 我正在使用这样的循环创建textFields:

 testText = new JTextField[9][9];
    for(int x = 0; x < 9; x++)
        for(int y = 0; y < 9; y++)
        {
            testText[x][y] = new JTextField();
            testText[x][y].setPreferredSize(new Dimension(30, 30));
            testText[x][y].setHorizontalAlignment(SwingConstants.CENTER);
            testText[x][y].setFont(new Font("Tahoma", Font.BOLD, 18));  
            testText[x][y].setBackground(Color.WHITE);
            testText[x][y].setEditable(false);
            testText[x][y].addMouseListener(new MouseAdapter() {
                @Override
                public void mouseClicked(MouseEvent e) {
                    if( //blablabla )                       
                    testText[x][y].setText(value + "");
                }
            });
            panelMain.add(testText[x][y]);
        }

I want to use the x, and y to get the location of this "clicked" field, but the mysterious error apperas: "Local variable x defined in an enclosing scope must be final or effectively final" (same for "y") 我想使用x和y来获取“单击”字段的位置,但神秘的错误说法是:“在封闭范围内定义的局部变量x必须是最终的或有效地是最终的”(与“ y”相同)

In my project there would be checking function and it would be great if I could use those x and y as arguments like : 在我的项目中,将有检查功能,如果我可以将x和y用作参数,那将是一件很棒的事情:

         checkIfPossibel(x,y,value); // "value" is global

Keep in mind that I am not a Java God and I would like to keep this work on understandable level(for me) if this is possible. 请记住,我不是Java的上帝,如果可能的话,我希望将这项工作保持在(对我来说)可理解的水平上。

The best fix here is to simplify your code - remove all that duplication of testTest[x][y] by introducing a local variable which can be final (and thus allow you to use it within the anonymous inner class): 最好的解决方法是简化您的代码-通过引入可以为final的局部变量(从而允许您在匿名内部类中使用它)来消除所有testTest[x][y]的重复:

testText = new JTextField[9][9];
for (int x = 0; x < 9; x++) {
    for (int y = 0; y < 9; y++)
    {
        final JTextField field = new JTextField();
        testText[x][y] = field;
        field.setPreferredSize(new Dimension(30, 30));
        field.setHorizontalAlignment(SwingConstants.CENTER);
        field.setFont(new Font("Tahoma", Font.BOLD, 18));  
        field.setBackground(Color.WHITE);
        field.setEditable(false);
        field.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                if (//blablabla) {                       
                    field.setText(value + "");
                }
            }
        });
        panelMain.add(field);
    }
}

It's not clear whether you even need testText at this point, but I'll assume for the moment that you are referring to it somewhere else. 目前尚不清楚您是否甚至需要testText ,但目前我假设您其他地方引用它。

First, you need to understand why the compiler tells you that it cannot access non-final x and y . 首先,您需要了解为什么编译器告诉您它无法访问非最终xy When you create new MouseAdapter() , variables x and y need to be "captured" for the constructor of the anonymous class. 创建new MouseAdapter() ,需要为匿名类的构造函数“捕获”变量xy The value of non-final x and y is subject to change after the object has been created, which may lead to confusion, because the values of x and y that you observe inside and outside the mouseClicked method could be different. 创建对象 ,非最终xy值可能会发生变化,这可能会引起混淆,因为您在mouseClicked方法的内部和外部观察到的xy的值可能不同。 That is why Java language designers required that only final local variables could be used inside anonymous method implementations. 这就是Java语言设计人员要求匿名方法实现中只能使用final局部变量的原因。

Now that you understand what is going on, coming up with the fix is simple: make final copies of x and y , and use them instead: 现在您了解了所发生的情况,解决此问题很简单:制作xy final副本,并改用它们:

final int tmpX = x;
final int tmpY = x;
testText[x][y].addMouseListener(new MouseAdapter() {
    @Override
    public void mouseClicked(MouseEvent e) {
        if( //blablabla )                       
        testText[tmpX][tmpY].setText(value + "");
    }
});

Create a temporary final varable like this: 创建一个临时的最终变量,如下所示:

        testText[x][y].addMouseListener(new MouseAdapter() {
        final int x1 = x;
        final int y1 = y;
            @Override
            public void mouseClicked(MouseEvent e) {
                if( //blablabla )                       
                testText[x1][y1].setText(value + "");
            }
        });

Guys it was my first post here, and I'm stunned. 伙计们,这是我在这里的第一篇文章,我很震惊。 You are ULTRA fast. 您超快。 BUT it does not work as it suppuose to. 但是它不起作用,因为它支持。

To find out I've made something like that: 为了找出答案,我做了类似的事情:

           final int tmpX = x;
            final int tmpY = x;
            Liczba[x][y].addMouseListener(new MouseAdapter() {
                @Override
                public void mouseClicked(MouseEvent e) {
                    pole =javax.swing.JTextField)e.getSource());
                    pole.setText(tmpX + " " + tmpY);                        

                }
            });

And it sets the number of row for example: for 0, its "0 0", for 6 "6 6" and so on. 并设置行数,例如:对于0,其“ 0 0”,对于6,“ 6 6”,依此类推。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在封闭作用域中定义的局部变量必须是最终的或有效的最终变量 - Local variable defined in an enclosing scope must be final or effectively final 在封闭 scope 中定义的局部变量迭代必须是最终的或有效的最终 - local variable iteration defined in an enclosing scope must be final or effectively final 在封闭 scope 中定义的局部变量 ObjList 必须是最终的或有效的最终 - Local variable ObjList defined in an enclosing scope must be final or effectively final 在封闭范围内定义的局部变量 log 必须是 final 或有效 final - Local variable log defined in an enclosing scope must be final or effectively final 线程:封闭范围中定义的局部变量必须是final或有效的final - Threads: Local variable defined in an enclosing scope must be final or effectively final 我在封闭作用域中定义的局部变量必须是最终的或有效的最终变量 - Local variable i defined in an enclosing scope must be final or effectively final 获取在封闭 scope 中定义的局部变量必须是最终的或有效的最终 - Getting local variable defined in the enclosing scope must be final or effective final 在封闭的 scope 中定义的局部变量 collect 必须是最终的或有效的最终的 - Local variable collect defined in an enclosing scope must be final or effectively final 封闭范围中定义的局部变量计数必须是最终错误 - Local variable count defined in an enclosing scope must be final error 处理封闭 scope 中定义的局部变量的流问题必须是最终的 - working with streams problem of Local variable defined in an enclosing scope must be final
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM