在我之前的帖子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
...
}