小编典典

循环内的侦听器不允许更改外人布尔标志

java

继我以前的帖子在这里,我写了一个监听器:

@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


}

目标

  1. 当我获得一秒fF(进入循环时我已经获得一个f/)F时从循环中中断。

  2. 我是否需要将第二个侦听器附加到某物上?

但是我不能将其更改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

...
}

阅读 216

收藏
2020-11-30

共1个答案

小编典典

最终变量不能更改,但是如果它们表示可变对象,则可以更改该对象的内容。

例如,如果您创建一个Settable类,并使用其实例代替boolean,则可以这样做:

class Settable {
    private boolean flag;
    public boolean get() {return flag;}
    public boolean set(boolean val) { flag = val; }
}

现在,在代码中使用的final实例Settable;使用set(...)而不是分配,而get()不是直接访问:

final Settable runForever = new Settable();
runForever.set(true);
...
while (runForever.get()) {
    ...
    if (keyOther == 'F' || keyOther == 'f') runForever.set(false);
    ...
}
2020-11-30