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来获取这个“单击”字段的位置,但是神秘的错误apperas:“在封闭范围中定义的局部变量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的神,如果可能的话,我希望把这项工作保持在可以理解的水平上。
4 个解决方案
#1
3
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):
这里最好的解决方法是简化您的代码——通过引入一个局部变量来删除testTest[x][y]的所有副本,该变量可以是final的(因此允许您在匿名内部类中使用它):
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.
目前还不清楚您是否需要测试文本,但是我将假设您在其他地方引用它。
#2
1
First, you need to understand why the compiler tells you that it cannot access non-final x
and y
. When you create new MouseAdapter()
, variables x
and y
need to be "captured" for the constructor of the anonymous class. 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. That is why Java language designers required that only final
local variables could be used inside anonymous method implementations.
首先,您需要理解为什么编译器告诉您它不能访问非final的x和y,当您创建新的MouseAdapter()时,需要为匿名类的构造函数“捕获”变量x和y。在创建对象之后,非final x和y的值可能会发生变化,这可能会导致混淆,因为您在mouseClicked方法中观察到的x和y的值可能会有所不同。这就是为什么Java语言设计者要求在匿名方法实现中只能使用最终的本地变量。
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:
既然您已经了解了目前的情况,那么提出修复方案就很简单了:制作x和y的最终副本,然后使用它们:
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 + "");
}
});
#3
0
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 + "");
}
});
#4
0
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”等等。
#1
3
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):
这里最好的解决方法是简化您的代码——通过引入一个局部变量来删除testTest[x][y]的所有副本,该变量可以是final的(因此允许您在匿名内部类中使用它):
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.
目前还不清楚您是否需要测试文本,但是我将假设您在其他地方引用它。
#2
1
First, you need to understand why the compiler tells you that it cannot access non-final x
and y
. When you create new MouseAdapter()
, variables x
and y
need to be "captured" for the constructor of the anonymous class. 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. That is why Java language designers required that only final
local variables could be used inside anonymous method implementations.
首先,您需要理解为什么编译器告诉您它不能访问非final的x和y,当您创建新的MouseAdapter()时,需要为匿名类的构造函数“捕获”变量x和y。在创建对象之后,非final x和y的值可能会发生变化,这可能会导致混淆,因为您在mouseClicked方法中观察到的x和y的值可能会有所不同。这就是为什么Java语言设计者要求在匿名方法实现中只能使用最终的本地变量。
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:
既然您已经了解了目前的情况,那么提出修复方案就很简单了:制作x和y的最终副本,然后使用它们:
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 + "");
}
});
#3
0
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 + "");
}
});
#4
0
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”等等。