java 监听 循环,java-循环内的侦听器不允许更改外人布尔标...

在我之前的帖子here之后,我写了一个侦听器:

@Override

public void keyTyped(KeyEvent keyEvent)

{

PolygonFiller polyFiller = new PolygonFiller();

char key = keyEvent.getKeyChar();

final boolean runForever = true;

switch(key)

{

/**

* Fill the polygons

*/

case FILL_POLYGON:

{

if (greenLightForFilling == true)

{

while (runForever)

{

fillPolygon(polyFiller);

KeyListener listener = new KeyListener()

{

public void keyPressed(KeyEvent keyEvent)

{

char keyOther = keyEvent.getKeyChar();

if (keyOther == 'F' || keyOther == 'f')

// can't use break;

runForever = false;

}

@Override

public void keyReleased(KeyEvent arg0) {}

@Override

public void keyTyped(KeyEvent arg0) {}

};

}

}

break;

} // end FILL_POLYGON

case FILL_POLYGON_LOWERCASE:

{

if (greenLightForFilling == true)

{

fillPolygon(polyFiller);

}

break;

}

/**

* save all polygons in a .scn file

*/

case SAVE_POLYGONS :

{

if (greenLightForFilling == true)

{

saveWorkspace();

}

break;

} // end SAVE_POLYGONS

case SAVE_POLYGONS_LOWERCASE:

{

if (greenLightForFilling == true)

{

saveWorkspace();

}

break;

}

/**

* Delete everything & load all polygons from .scn file

*/

case LOAD_POLYGONS:

{

loadWorkspace();

break;

}

case LOAD_POLYGONS_LOWERCASE:

{

loadWorkspace();

break;

}

default: break;

} // end switch

}

目标 :

>从循环中断,当我得到第二个f或F时(进入循环时我已经得到一个f / F).

>是否需要将第二个听众附加到某物上?

但是我无法将runForever更改为false,因为它是在外部编写的.

即使我将其更改为final,也不能将其值更改为false.

可以解决吗?

注意:我已经有一个用于输入开关盒的按键监听器!!

编辑:

// Hot-keys hit by the user - used for keyboard listening

private static final char FILL_POLYGON = 'F';

private static final char SAVE_POLYGONS = 'S';

private static final char LOAD_POLYGONS = 'L';

private static final char FILL_POLYGON_LOWERCASE = 'f';

private static final char SAVE_POLYGONS_LOWERCASE = 's';

private static final char LOAD_POLYGONS_LOWERCASE = 'l';

@Override

public void keyTyped(KeyEvent keyEvent)

{

PolygonFiller polyFiller = new PolygonFiller();

char key = keyEvent.getKeyChar();

final Settable runForever = new Settable();

switch(key)

{

/**

* Fill the polygons

*/

case FILL_POLYGON:

{

if (greenLightForFilling == true)

{

KeyListener listener = new KeyListener()

{

public void keyPressed(KeyEvent keyEvent)

{

char keyOther = keyEvent.getKeyChar();

if (keyOther == 'F' || keyOther == 'f')

runForever.set(false);

}

@Override

public void keyReleased(KeyEvent arg0) {}

@Override

public void keyTyped(KeyEvent arg0) {}

};

this.addKeyListener(listener);

while (runForever.get())

{

fillPolygon(polyFiller);

}

}

break;

} // end FILL_POLYGON

...

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值
>