2014-11-05 132 views
0

我正在嘗試使用Java進行圖形編程,並且嘗試了一些練習,以便讓汽車在一幀中來回移動。然後,如果按下向上或向下箭頭鍵,我想讓它變快或變慢。但是,我似乎無法正確添加按鍵偵聽器。爲了測試我的代碼,我只是試圖向命令提示符輸出一條消息。任何幫助將不勝感激!Java:向JFrame添加密鑰監聽器

代碼編譯並按原樣運行。我得到一輛車在車架上來回走動。唯一不起作用的是關鍵聽衆。

import java.awt.*; 
import javax.swing.*; 
import java.awt.event.*; 

public class Racing extends JFrame 
{ 
    public static class Car extends JComponent implements ActionListener 
    { 
     int x=0; 
     int y=0; 
     int delta = 10; 
     Timer timer; 
     int z = 300; 

     public Car() 
     { 
      timer = new Timer(20,this); 
      timer.start(); 

      addKeyListener(new KeyAdapter() 
      { 
       @Override 
       public void keyPressed(KeyEvent e) 
       {   
        if(e.getKeyCode() == KeyEvent.VK_UP) 
         System.out.println("The up key was pressed!"); 
       } 
      }); 
     } 

     public void paint(Graphics g) 
     { 
      super.paintComponent(g); 

      y = getHeight(); 
      z = getWidth(); 

      g.setColor(Color.BLUE); 
      g.fillRect(0, 0, z, y); 

      Polygon polygon = new Polygon(); 
      polygon.addPoint(x + 10, y - 20); 
      polygon.addPoint(x + 20, y - 30); 
      polygon.addPoint(x + 30, y - 30); 
      polygon.addPoint(x + 40, y - 20); 

      g.setColor(Color.BLACK); 
      g.fillOval(x + 10, y - 11, 10, 10); 
      g.fillOval(x + 30, y - 11, 10, 10); 
      g.setColor(Color.GREEN); 
      g.fillRect(x, y - 21, 50, 10); 
      g.setColor(Color.RED); 
      g.fillPolygon(polygon); 
      g.setColor(Color.BLUE); 
     } 

    public void actionPerformed(ActionEvent e) { 
     x += delta; 
     if (x > z-40) { 
      delta *= -1; 
      x = (z-40); 
     } else if (x < 0) { 
      delta *= -1; 
      x = 0; 
     } 

     repaint(); 
    } 

    } 

    public static void main(String[] args) 
    { 
     JFrame frame = new JFrame("Racing"); 
     frame.setPreferredSize(new Dimension(600,300)); 
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     frame.pack(); 
     frame.add(new Car()); 
     frame.setVisible(true); 
     frame.setFocusable(true); 
    } 
} 

回答

3

簡短的回答是不要,涉及的問題太多。

而是使用鍵綁定API。見How to Use Key Bindings更多細節

import java.awt.Color; 
import java.awt.Dimension; 
import java.awt.EventQueue; 
import java.awt.Graphics; 
import java.awt.Polygon; 
import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 
import java.awt.event.KeyEvent; 
import javax.swing.AbstractAction; 
import javax.swing.ActionMap; 
import javax.swing.InputMap; 
import javax.swing.JComponent; 
import javax.swing.JFrame; 
import javax.swing.KeyStroke; 
import javax.swing.Timer; 
import javax.swing.UIManager; 
import javax.swing.UnsupportedLookAndFeelException; 

public class Racing extends JFrame { 

    public static class Car extends JComponent implements ActionListener { 

     int x = 0; 
     int y = 0; 
     int delta = 10; 
     Timer timer; 
     int z = 300; 

     public Car() { 
      timer = new Timer(20, this); 
      timer.start(); 

      InputMap im = getInputMap(WHEN_IN_FOCUSED_WINDOW); 
      im.put(KeyStroke.getKeyStroke(KeyEvent.VK_UP, 0), "goingUp"); 

      ActionMap am = getActionMap(); 
      am.put("goingUp", new AbstractAction() { 
       @Override 
       public void actionPerformed(ActionEvent e) { 
        System.out.println("The up key was pressed!"); 
       } 
      }); 
     } 

     @Override 
     protected void paintComponent(Graphics g) { 
      super.paintComponent(g); 

      y = getHeight(); 
      z = getWidth(); 

      g.setColor(Color.BLUE); 
      g.fillRect(0, 0, z, y); 

      Polygon polygon = new Polygon(); 
      polygon.addPoint(x + 10, y - 20); 
      polygon.addPoint(x + 20, y - 30); 
      polygon.addPoint(x + 30, y - 30); 
      polygon.addPoint(x + 40, y - 20); 

      g.setColor(Color.BLACK); 
      g.fillOval(x + 10, y - 11, 10, 10); 
      g.fillOval(x + 30, y - 11, 10, 10); 
      g.setColor(Color.GREEN); 
      g.fillRect(x, y - 21, 50, 10); 
      g.setColor(Color.RED); 
      g.fillPolygon(polygon); 
      g.setColor(Color.BLUE); 
     } 

     public void actionPerformed(ActionEvent e) { 
      x += delta; 
      if (x > z - 40) { 
       delta *= -1; 
       x = (z - 40); 
      } else if (x < 0) { 
       delta *= -1; 
       x = 0; 
      } 

      repaint(); 
     } 

    } 

    public static void main(String[] args) { 
     EventQueue.invokeLater(new Runnable() { 
      @Override 
      public void run() { 
       try { 
        UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); 
       } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) { 
        ex.printStackTrace(); 
       } 

       JFrame frame = new JFrame("Racing"); 
       frame.setPreferredSize(new Dimension(600, 300)); 
       frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
       frame.pack(); 
       frame.add(new Car()); 
       frame.setVisible(true); 
       frame.setFocusable(true); 
      } 
     }); 
    } 
} 

另外,不要從paintsuper.paintComponent,你基本上打破了整個塗料鏈。相反,覆蓋paintComponent方法本身...

+0

謝謝!那是我想要考慮的方向。我買了一本Java編程書,裏面有一些圖形信息,但它指出我在這方面有不同的方向。打破塗料鏈有什麼後果? – 2014-11-05 07:04:01

+0

你可能最終會出現奇怪的繪畫工件或根本沒有被繪的東西 – MadProgrammer 2014-11-05 08:42:53