2013-11-04 35 views
1

我正試圖製作一個程序,使用箭頭鍵在java swing窗口中移動一個圓圈。鍵綁定工作正常,但顯示圓圈總是有問題。這裏是代碼:在Java swing窗口中控制形狀運動的問題

public class ShapesMove extends JFrame{ 

    public static int x = 40; 
    public static int y = 40; 

    public static void main(String[] args){ 

     final JFrame frame = new JFrame("Movement of 2d Shapes"); 
     frame.setSize(400,400); 
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     JPanel content = (JPanel) frame.getContentPane(); 
     frame.setLocationRelativeTo(null); 
     frame.setVisible(true); 

     Action actionRight = new AbstractAction(){ 
      public void actionPerformed(ActionEvent actionRightEvent){ 
       x++; 
      } 
     }; 

     Action actionLeft = new AbstractAction(){ 
      public void actionPerformed(ActionEvent actionLeftEvent){ 
       x--; 
      } 
     }; 

     Action actionUp = new AbstractAction(){ 
      public void actionPerformed(ActionEvent actionUpEvent){ 
       y++; 
      } 
     }; 

     Action actionDown = new AbstractAction(){ 
      public void actionPerformed(ActionEvent actionDownEvent){ 
       y--; 
      } 
     }; 

     KeyStroke right = KeyStroke.getKeyStroke("RIGHT"); 
     KeyStroke left = KeyStroke.getKeyStroke("LEFT"); 
     KeyStroke up = KeyStroke.getKeyStroke("UP"); 
     KeyStroke down = KeyStroke.getKeyStroke("DOWN"); 

     InputMap inputMap = content.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW); 
     inputMap.put(right, "RIGHT"); 
     inputMap.put(left, "LEFT"); 
     inputMap.put(up, "UP"); 
     inputMap.put(down, "DOWN"); 
     content.getActionMap().put("RIGHT", actionRight); 
     content.getActionMap().put("LEFT", actionLeft); 
     content.getActionMap().put("UP", actionUp); 
     content.getActionMap().put("DOWN", actionDown); 

    } 
    public void draw(Graphics g){ 
     g.drawOval(x, y, 60, 60); 
    } 
} 

我沒有包括導入行,因爲我知道我有所有正確的模塊。編譯總是很順利,但是當我運行它時,該圓形不顯示。我在它自己的獨立遊戲中嘗試了相同的代碼,當我運行它時出現了這個圈子,那麼我在這裏做錯了什麼?

+1

哪裏畫的叫? – Dodd10x

回答

1

更改了你的代碼,它工作正常。重寫paintComponent方法並在其中調用您的draw方法。並將JFrame更改爲JPanel

public class ShapesMove extends JPanel{ 

    public static int x = 40; 
    public static int y = 40; 

    public static void main(String[] args){ 

     final JFrame frame = new JFrame("Movement of 2d Shapes"); 
     frame.setSize(400,400); 
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     final ShapesMove m = new ShapesMove(); 
     frame.getContentPane().add(m); 
     frame.setLocationRelativeTo(null); 
     frame.setVisible(true); 

     Action actionRight = new AbstractAction(){ 
      public void actionPerformed(ActionEvent actionRightEvent){ 
       x++; 
       m.repaint(); 
      } 
     }; 

     Action actionLeft = new AbstractAction(){ 
      public void actionPerformed(ActionEvent actionLeftEvent){ 
       x--; 
       m.repaint(); 
      } 
     }; 

     Action actionUp = new AbstractAction(){ 
      public void actionPerformed(ActionEvent actionUpEvent){ 
       y++; 
       m.repaint(); 
      } 
     }; 

     Action actionDown = new AbstractAction(){ 
      public void actionPerformed(ActionEvent actionDownEvent){ 
       y--; 
       m.repaint(); 
      } 
     }; 

     KeyStroke right = KeyStroke.getKeyStroke("RIGHT"); 
     KeyStroke left = KeyStroke.getKeyStroke("LEFT"); 
     KeyStroke up = KeyStroke.getKeyStroke("UP"); 
     KeyStroke down = KeyStroke.getKeyStroke("DOWN"); 

     InputMap inputMap = m.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW); 
     inputMap.put(right, "RIGHT"); 
     inputMap.put(left, "LEFT"); 
     inputMap.put(up, "UP"); 
     inputMap.put(down, "DOWN"); 
     m.getActionMap().put("RIGHT", actionRight); 
     m.getActionMap().put("LEFT", actionLeft); 
     m.getActionMap().put("UP", actionUp); 
     m.getActionMap().put("DOWN", actionDown); 
    } 

    @Override 
    protected void paintComponent(Graphics g) { 
     super.paintComponent(g); 
     g.drawOval(x, y, 60, 60); 
    } 

    } 
+0

謝謝,儘管您在編輯之前發佈的內容實際上效果稍好一些,但沒有冒犯性。編輯後,我的paintComponent方法的覆蓋錯誤,我不斷收到錯誤。 – dakatk

2

你需要重寫paintComponent來做你的拖曳。在添加到JFrame而不是框架本身上的組件上執行此操作,因爲JFrame是一個容器,它具有一個contentPane,這會使事情變得更加複雜,並且對於進一步的修改而言變得更加靈活。