简体   繁体   中英

When adding a KeyListener object for some swing component outside of this component constructor KeyAdapter doesn't work

When I try to add a KeyListener object for some swing component outside of this component constructor KeyListener doesn't answer after, why? For example:

    public class ActionViewer extends SimpleViewer
                              implements ActionListener

    {
        public ActionViewer(Dimension __wndSize, String pgnPath, 
                            PerspectiveProjection __persp)
        {
            super(__wndSize, pgnPath, __persp);

            ...
            cMng=null;
            ...
        }
        ///
        public void setCamera(double xMin, double xMax,
                              double yMin, double yMax,
                              double zMin, double zMax)
        {
            if(!isCameraInit())
            {
                if(cMng==null)
                    cMng=new CameraControl();
                this.addKeyListener((KeyListener)cMng);
                fCamInit=true;
            }
            setCameraBounds(xMin, xMax, yMin, yMax, zMin, zMax);
        }
    ...
}

When I call setCamera in main there is nothing happen.

...
ActionViewer scene = new ActionViewer(wndSize, pgnPath, persp);
scene.setCamera(xMin, xMax, yMin, yMax, zMin, zMax);
...

我认为初始化存在一些问题,你能检查一下init监听器的位置,只需try{}catch所有方法setCamera并打印所有问题,它应该可以帮助你找到问题。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM