2015-10-26 56 views
0

我很抱歉,我知道這已經被問過。其他帖子建議只使用鍵盤綁定,但我更願意使用這種方法,因爲我希望機器人的運動以keyrelease停止。我如何獲得KeyListener的工作?

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(); 
      } 
     }); 
    } 
} 
+0

'其他職位建議只使用鍵綁定' - 他們建議,由於一個原因,因爲它是更好的解決方案。 「我希望機器人的運動以keyrelease停止。」 - 這很容易完成。我多次發佈了這個解決方案。 「我知道這已經被問到了 - 」是的,它總是被問到。您只需查看本頁右側「Related」標題下的問題即可。不需要關於這個話題的另一個問題。您發佈的代碼並不反映其他問題提供的任何建議。努力。 – camickr

回答

1

某處在你createAndShowGui()將關鍵偵聽器添加到其中一個擺動組件。然後,當該組件具有焦點時,它將收到關鍵事件。如果您需要所有應用程序的密鑰,那麼您需要在應用程序所具有的最大容器組件上使用密鑰偵聽器。

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

0

你有偵聽器定義,但是你有沒有附加監聽到任何部件。將偵聽器添加到要從中偵聽事件的組件。