KeyListener作为自己的类添加不起作用

时间:2014-11-15 13:20:36

标签: java swing jframe jpanel keylistener

目前我正在尝试构建一个使用IP摄像头的小型WebCam客户端,该摄像头可以通过http命令控制器。不幸的是,我添加了自己的类的KeyListener没有响应任何击键。 我想我以错误的方式添加了KeyListener。

import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;

public class MainFrame extends JFrame{

    private static final long serialVersionUID = 1L;

    InterfaceVideo activeVideo;
    InterfaceMoving activeCamera;
    Image image;

    public MainFrame(){

        initComponents();
    }

    public void initComponents(){

        Container container = this.getContentPane();

        final CameraPanel cameraPanel = new CameraPanel();
        final EventHandlerKeyListener keyListener = new EventHandlerKeyListener();

            // create menubar
            JMenuBar menuBar = new JMenuBar();

            // create menuitem
            JMenu Camera_Menu = new JMenu("Cameras");
            JMenu Options_Menu = new JMenu("Options");

            // create submenuitems
            JMenuItem AllnetCamera_MenuItem = new JMenuItem("Allnet Camera");
            JMenuItem FosCamera_MenuItem = new JMenuItem("FosCam camera");
            JMenuItem SevenLinkCamera_MenuItem = new JMenuItem("SevenLink Camera");

            JMenuItem AddCamera_MenuItem = new JMenuItem("Add Camera...");

            // add menuitems
            menuBar.add(Camera_Menu);
            menuBar.add(Options_Menu);

            // add submenuitems
            Camera_Menu.add(AllnetCamera_MenuItem);
            Camera_Menu.add(FosCamera_MenuItem);
            Camera_Menu.add(SevenLinkCamera_MenuItem);
            Options_Menu.add(AddCamera_MenuItem);

        add(menuBar, BorderLayout.NORTH);

        container.add(cameraPanel);

        setSize(1280, 720);
        setTitle("Webcam Client");

        cameraPanel.setFocusable(true);
        cameraPanel.requestFocusInWindow();
        cameraPanel.addKeyListener(keyListener);

        cameraPanel.setVisible(true);
        setVisible(true);


        AllnetCamera_MenuItem.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent arg0) {

                    activeVideo = new AllnetCamera("ip");
                    activeCamera = new AllnetCamera("ip");

                    cameraPanel.setInterfaces(activeCamera, activeVideo);

                    keyListener.setInterface(activeCamera);

                }
        });
    }


    //MAIN METHOD
    public static void main(String[] args){
        new MainFrame();
    }

}


package webcamClient;

import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

public class EventHandlerKeyListener implements KeyListener{

    InterfaceMoving activeCamera;

    public EventHandlerKeyListener(){

    }

    public void setInterface(InterfaceMoving i){
        activeCamera = i;
    }

    @Override
    public void keyTyped(KeyEvent event) {
        if (event.getKeyCode() == KeyEvent.VK_DOWN){
            System.out.println("it works");
            activeCamera.move("down");
        }
        if (event.getKeyCode() == KeyEvent.VK_UP){
            activeCamera.move("up");
        }
        if (event.getKeyCode() == KeyEvent.VK_LEFT){
            activeCamera.move("left");
        }
        if (event.getKeyCode() == KeyEvent.VK_RIGHT){
            activeCamera.move("right");
        }
        if (event.getKeyCode() == KeyEvent.VK_R){
            activeCamera.move("reset");
        }
        if (event.getKeyCode() == KeyEvent.VK_S){
            activeCamera.move("stop");
        }

    }

    @Override
    public void keyPressed(KeyEvent e) {
        // TODO Auto-generated method stub

    }

    @Override
    public void keyReleased(KeyEvent e) {
        // TODO Auto-generated method stub

    }

}

我看不出自己的错误,

提前谢谢。

0 个答案:

没有答案