在我之前的帖子之后,我写了一个监听器:
@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
...
}