如何使KeyListener工作?

时间:2015-10-26 18:50:57

标签: java swing keylistener

我道歉,我知道之前有人问过这个问题。其他帖子建议只使用键绑定,但我更喜欢使用这种方法,因为我希望机器人的运动停止使用keyrelease。

import java.io.BufferedReader;
import javax.swing.AbstractButton;
import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.JFrame;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.io.PrintWriter;
import java.net.Socket;
import java.net.UnknownHostException;
import java.awt.Button;
import java.awt.Component;
import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import javax.swing.BoxLayout;

public class Robot implements KeyListener {
    public static JButton a, w, s, d, space, ctrl;
    public static char input;
    public static String takeoff = "{\"op\":\"publish\",\"topic\":\"/ardrone/takeoff\",\"msg\":{}}";
    public static String landing = "{\"op\":\"publish\",\"topic\":\"/ardrone/land\",\"msg\":{}}";
    public static String stop = "{\"op\":\"publish\",\"topic\":\"/cmd_vel\",\"msg\":{\"linear\":{\"x\":0,\"y\":0,\"z\":0},\"angular\":{\"x\":0,\"y\":0,\"z\":0}}}";


    private static void createAndShowGUI() {
        //Create and set up the window.
        JFrame frame = new JFrame("Robot Controller");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        addComponentsToPane(frame.getContentPane());
        frame.pack();
        frame.setVisible(true);
        frame.setSize(800, 600);
        frame.add(a);
        frame.add(w);
        frame.add(d);
        frame.add(s);
        frame.add(space);
        frame.add(ctrl);


        Socket mysocket = null;
        PrintStream output = null;
        try {
            // opens socket
            mysocket = new Socket("lear.cs.okstate.edu", 9095);     
            } catch (UnknownHostException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
        try {
            // opens stream to write to socket
            output = new PrintStream(mysocket.getOutputStream());
            output.print(takeoff);
            } catch (IOException e){
            System.out.print(e);
            }
        }
    public void keyPressed(KeyEvent e) {
        if (e.getKeyCode() == KeyEvent.VK_A) {
            System.exit(0);
        }
        if (e.getKeyCode() == KeyEvent.VK_S) {

        }
        if (e.getKeyCode() == KeyEvent.VK_D) {

        }
        if (e.getKeyCode() == KeyEvent.VK_W) {

        }
        if (e.getKeyCode() == KeyEvent.VK_S) {

        }
        if (e.getKeyCode() == KeyEvent.VK_SPACE) {

        }
        if (e.getKeyCode() == KeyEvent.VK_CONTROL) {

        }
    }

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

    public void keyTyped(KeyEvent e) {
        // TODO Auto-generated method stub
    }

    public static void addComponentsToPane(Container pane) {
        pane.setLayout(new BoxLayout(pane, BoxLayout.Y_AXIS));
        a = new JButton("Move left: A");       
        w = new JButton("Move up: W");
        d = new JButton("Move right: D");
        s = new JButton("Move down: S");
        space = new JButton("Takeoff: spacebar");
        ctrl = new JButton("Land: control");
    }

    public static void main(String[] args) {
        javax.swing.SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                createAndShowGUI();
            }
        });
    }
}

2 个答案:

答案 0 :(得分:1)

在你createAndShowGui()的某个地方,将关键监听器添加到其中一个swing组件中。然后,当该组件具有焦点时,它将接收关键事件。如果您想要所有应用程序的密钥,那么您需要在应用程序所拥有的最大容器组件上使用密钥监听器。

http://docs.oracle.com/javase/tutorial/uiswing/events/keylistener.html

答案 1 :(得分:0)

您具有侦听器定义,但尚未将侦听器附加到任何组件。将侦听器添加到要侦听事件的组件。