2013-03-11 62 views
-1

我試圖移動使用KeyBinding的rectange,但我想我無法正確實施它。 我是一個java初學者,我不能找到這個程序中的錯誤 所以請幫助我。無法實現KeyBinding

Thnx提前!

import java.awt.Graphics; 
import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 
import javax.swing.AbstractAction; 
import javax.swing.JFrame; 
import javax.swing.JPanel; 
import javax.swing.KeyStroke; 
import javax.swing.SwingUtilities; 
import javax.swing.Timer; 

class Move { 

    public static void main(String args[]) { 
     SwingUtilities.invokeLater(new Runnable() { 
      @Override 
      public void run() { 
       abc(); 
      } 
     }); 
    } 

    private static void abc() { 
     JFrame frame = new JFrame("moving object"); 
     frame.setVisible(true); 
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     frame.setSize(1300, 600); 
     frame.add(new P1()); 
    } 
} 

class P1 extends JPanel { 

    int a, b; 
    int x, y; 
    Timer t = new Timer(10, new ActionListener() { 
     @Override 
     public void actionPerformed(ActionEvent e) {//the coordinates of this rectangle 
      a += x;         //are mordified in every 10 millisec 
      b += y;         //x and y changes the direction of the moving 
      repaint();        //rect. 
     } 
    }); 

    P1() { 
     this.getInputMap().put(KeyStroke.getKeyStroke("VK_UP"), "doNothing1"); 
     this.getActionMap().put("doNothing1", new AbstractAction() { 
      @Override 
      public void actionPerformed(ActionEvent e) { 
       x = 0; 
       y = -1; 
      } 
     }); 
     this.getInputMap().put(KeyStroke.getKeyStroke("VK_LEFT"), "doNothing2"); 
     this.getActionMap().put("doNothing2", new AbstractAction() { 
      @Override 
      public void actionPerformed(ActionEvent e) { 
       x = -1; 
       y = 0; 
      } 
     }); 
     this.getInputMap().put(KeyStroke.getKeyStroke("VK_DOWN"), "doNothing3"); 
     this.getActionMap().put("doNothing3", new AbstractAction() { 
      @Override 
      public void actionPerformed(ActionEvent e) { 
       x = 0; 
       y = 1; 
      } 
     }); 
     this.getInputMap().put(KeyStroke.getKeyStroke("VK_RIGHT"), "doNothing4"); 
     this.getActionMap().put("doNothing4", new AbstractAction() { 
      @Override 
      public void actionPerformed(ActionEvent e) { 
       x = -1; 
       y = 0; 
      } 
     }); 
     t.start(); 
     setFocusable(true); 
    } 

    @Override 
    protected void paintComponent(Graphics g) { 
     super.paintComponent(g); 
     g.fillRect(a, b, 10, 10); 
    } 
} 
+1

對於[示例](http://stackoverflow.com/a/5797965/230513)。 – trashgod 2013-03-11 01:46:05

+1

當您在這個(或任何網站)上提問時,您只需要付出一些努力就可以發佈格式良好的代碼。試圖理解別人的代碼已經夠難了,所以沒有必要讓它變得比我們更難。 – 2013-03-11 01:50:55

+0

再次,請修復您的代碼的縮進。 – 2013-03-11 02:43:50

回答

4

你正在使用錯誤的InputMap,這是你的問題的一個主要原因。當你沒有指定條件時獲得的默認InputMap使用WHEN_FOCUSED條件,並且由於JPanel默認情況下不允許焦點(並且實際上不應該強制獲得焦點),所以這將不起作用。你反而會想用WHEN_IN_FOCUSED_WINDOW條件。爲此,您必須明確指定。即:

InputMap inputMap = getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW); // note condition 
inputMap.put(KeyStroke.getKeyStroke("VK_UP"), "doNothing1"); 
// .... 

這甚至會工作,如果綁定組件沒有焦點,只要它是在頂層窗口,確實有重點持有。

+0

Thanx爲答案,但它仍然不工作! – user2138054 2013-03-11 12:47:58

+0

@ user2138054:請使用上面的建議顯示最近一次嘗試的代碼。再次,請只發布格式良好的代碼,並讓我們看看我們是否可以幫助您。 – 2013-03-11 17:47:28