gpt4 book ai didi

java - 缩短一段代码会产生错误;我该如何解决这个问题?

转载 作者:行者123 更新时间:2023-11-30 03:23:17 25 4
gpt4 key购买 nike

我在一个方法中有这段代码:

switches[0].addMouseListener(new MouseAdapter(){
public void mouseClicked(MouseEvent e)
{
if(switchstate[0] == false)
{
if((e.getX() >= OFFBUTTONLEFT && e.getX() <= OFFBUTTONRIGHT) && (e.getY() >= OFFBUTTONTOP && e.getY() <= OFFBUTTONDOWN))
switchstate[0] = true;
}else
{
if((e.getX() >= ONBUTTONLEFT && e.getX() <= ONBUTTONRIGHT) && (e.getY() >= ONBUTTONTOP && e.getY() <= ONBUTTONDOWN))
switchstate[0] = false;
}

paintStuff();
}
});

switches[1].addMouseListener(new MouseAdapter(){
public void mouseClicked(MouseEvent e)
{
if(switchstate[1] == false)
{
if((e.getX() >= OFFBUTTONLEFT && e.getX() <= OFFBUTTONRIGHT) && (e.getY() >= OFFBUTTONTOP && e.getY() <= OFFBUTTONDOWN))
switchstate[1] = true;
}else
{
if((e.getX() >= ONBUTTONLEFT && e.getX() <= ONBUTTONRIGHT) && (e.getY() >= ONBUTTONTOP && e.getY() <= ONBUTTONDOWN))
switchstate[1] = false;
}

paintStuff();
}
});

switches[2].addMouseListener(new MouseAdapter(){
public void mouseClicked(MouseEvent e)
{
if(switchstate[2] == false)
{
if((e.getX() >= OFFBUTTONLEFT && e.getX() <= OFFBUTTONRIGHT) && (e.getY() >= OFFBUTTONTOP && e.getY() <= OFFBUTTONDOWN))
switchstate[2] = true;
}else
{
if((e.getX() >= ONBUTTONLEFT && e.getX() <= ONBUTTONRIGHT) && (e.getY() >= ONBUTTONTOP && e.getY() <= ONBUTTONDOWN))
switchstate[2] = false;
}

paintStuff();
}
});

变量(类的字段)在哪里

JLabel[] switches = new JLabel[3];      //I've initialized each index
boolean[] switchstate = new boolean[3]; //Indices Initialized to 'false'

final static int OFFBUTTONTOP   = 75;
final static int OFFBUTTONLEFT = 30;
final static int OFFBUTTONRIGHT = 65;
final static int OFFBUTTONDOWN = 115;

final static int ONBUTTONTOP = 35;
final static int ONBUTTONLEFT = 25;
final static int ONBUTTONRIGHT = 60;
final static int ONBUTTONDOWN = 75;

我想缩短那段代码,所以我做到了

for(final int i=0; i<switchstate.length; i++)
switches[i].addMouseListener(new MouseAdapter(){
public void mouseClicked(MouseEvent e)
{
if(switchstate[i])
{
if(ON_RECTANGLE.contains(e.getX(), e.getY()))
switchstate[i] = false;
}
else
{
if(OFF_RECTANGLE.contains(e.getX(), e.getY()))
switchstate[i] = true;
}
}
});

两个新变量是

final static Rectangle OFF_RECTANGLE = new Rectangle(30, 75, 35, 40); 
final static Rectangle ON_RECTANGLE = new Rectangle(25, 35, 35, 30);

但是缩短的代码给了我一个错误:

error: local variable i is accessed from within inner class; needs to be declared final

但是如果我声明 ifinal ,我无法使用i++正如它给出的那样

error: cannot assign a value to final variable i

如何避免这个问题?

最佳答案

使用另一个变量 -

for(int i = 0; i < switchstate.length; i++) {
final int j = i;
switches[i].addMouseListener(new MouseAdapter(){
public void mouseClicked(MouseEvent e)
{
if(switchstate[j])
{
if(ON_RECTANGLE.contains(e.getX(), e.getY()))
switchstate[j] = false;
}
else
{
if(OFF_RECTANGLE.contains(e.getX(), e.getY()))
switchstate[j] = true;
}

paintStuff();
}
});
}

通过这种方式,您可以在 mouseClicked 方法中捕获 i 的值,而不是捕获变量本身(不幸的是 Java 还不允许)。

关于java - 缩短一段代码会产生错误;我该如何解决这个问题?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30815822/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com