2012-06-11 76 views
1

在下面的程序,我試圖改變基於按下的鍵標籤的文字,但我不知道如何做到這一點。在按下鍵時要執行的語句在TimerListener InnerClass的actionPerformed()方法中定義。但我不明白我如何從那裏訪問標籤。更改標籤文本按下

package aircraftPackage; 

import java.awt.*; 
import java.awt.event.*; 
import java.awt.geom.AffineTransform; 
import java.awt.image.BufferedImage; 
import java.io.File; 
import java.io.IOException; 
import java.net.*; 
import java.util.HashMap; 
import java.util.Map; 
import javax.imageio.ImageIO; 
import javax.swing.*; 

public class Controller extends JPanel { 

    private static final long serialVersionUID = 1L; 
    public static final int STEP = 3; 
    private static final int TIMER_DELAY = STEP * 8; 
    private BufferedImage playerImage = null; 
    private Map<Direction, Boolean> directionMap = new HashMap<Direction, Boolean>(); 



    enum Direction { 

     UP(KeyEvent.VK_UP, 0, -1), DOWN(KeyEvent.VK_DOWN, 0, 1), 
     LEFT(KeyEvent.VK_LEFT, -1, 0), RIGHT(KeyEvent.VK_RIGHT, 1, 0); 
     private int keyCode; 
     private int xDirection; 
     private int yDirection; 

     private Direction(int keyCode, int xDirection, int yDirection) { 
      this.keyCode = keyCode; 
      this.xDirection = xDirection; 
      this.yDirection = yDirection; 
     } 

     public int getKeyCode() { 
      return keyCode; 
     } 

     public int getXDirection() { 
      return xDirection; 
     } 

     public int getYDirection() { 
      return yDirection; 
     } 
    } 

    public Controller() { 

     for (Direction direction : Direction.values()) { 
      directionMap.put(direction, false); 
     } 
     setKeyBindings(); 
     Timer timer = new Timer(TIMER_DELAY, new TimerListener()); 
     timer.start(); 
    } 

    private void setKeyBindings() { 
     InputMap inMap = getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW); 
     ActionMap actMap = getActionMap(); 
     for (final Direction direction : Direction.values()) { 
      KeyStroke pressed = KeyStroke.getKeyStroke(direction.getKeyCode(), 0, false); 
      KeyStroke released = KeyStroke.getKeyStroke(direction.getKeyCode(), 0, true); 
      inMap.put(pressed, direction.toString() + "pressed"); 
      inMap.put(released, direction.toString() + "released"); 
      actMap.put(direction.toString() + "pressed", new AbstractAction() { 

       private static final long serialVersionUID = 1L; 

       @Override 
       public void actionPerformed(ActionEvent e) { 
        directionMap.put(direction, true); 
       } 
      }); 
      actMap.put(direction.toString() + "released", new AbstractAction() { 

       private static final long serialVersionUID = 1L; 

       @Override 
       public void actionPerformed(ActionEvent e) { 
        directionMap.put(direction, false); 
       } 
      }); 
     } 

    } 



    private class TimerListener implements ActionListener { 

     @Override 
     public void actionPerformed(ActionEvent e) { 
      boolean moved = false; 
      for (Direction direction : Direction.values()) { 
       if (directionMap.get(direction)) { 
        if (direction.keyCode == 37) { 
         System.out.println("go LEFT"); 

        } else if (direction.getKeyCode() == 39) { 
         System.out.println("go RIGHT"); 
        } else if (direction.getKeyCode() == 38) { 
         System.out.println("go UP"); 
        } 
        else if (direction.getKeyCode()==40){ 
         System.out.println("go DOWN"); 
        } 
       } 
      } 
     } 
    } 

    public static void createAndShowUI() { 
     JFrame frame = new JFrame("MoveIcon"); 
     JPanel panel = new JPanel(); 
     JLabel jl = new JLabel(); 
     jl.setText("testing...."); 
     frame.add(jl); 
     frame.add(panel); 
     frame.getContentPane().add(new Controller()); 
     new Controller(); 
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     frame.pack(); 
     frame.setLocationRelativeTo(null); 
     frame.setVisible(true); 

    } 

    public static void main(String[] args) { 

     java.awt.EventQueue.invokeLater(new Runnable() { 

      @Override 
      @SuppressWarnings("static-access") 
      public void run() { 
       createAndShowUI(); 
      } 
     }); 
    } 
} 

其實,我想要做的是,創造一個很好的形式,將包含在Netbeans的(它更容易做到這一點那裏),並使用上面的代碼只知道被按下哪個鍵和各種標籤相應地更改每個標籤的值。 我試過了,但它沒有奏效。 請幫助我。 謝謝。

PS你會在某些問題或某些網站上發現上述代碼的部分,因爲我沒有自己寫過。我沒有任何keybindings/keyListeners等經驗。這只是我正在做的一個項目的一部分。

+1

代碼中沒有'ad'和'jLabel1'定義。他們在哪?另外,爲什麼你要創建兩個'Controller'(一個被添加到JFrame中)? –

+1

該代碼看起來非常眼熟...... –

+1

是啊,類似我以前的StackOverflow的答案[這裏](http://stackoverflow.com/a/8961998/522444)。 –

回答

3

我試圖刪除你不馬上需要的一切,並且加入了JLabel,顯示這樣的指示:

go go UP go UP DOWN LEFT RIGHT


這應該讓你開始。

import java.awt.event.*; 
import java.util.HashMap; 
import java.util.Map; 
import javax.swing.*; 

public class Controller extends JPanel { 

    private static final long serialVersionUID = 1L; 
    private static final int STEP = 3; 
    private static final int TIMER_DELAY = STEP * 8; 
    private Map<Direction, Boolean> directionMap = new HashMap<Direction, Boolean>(); 

    private JLabel lblDirection = new JLabel(); 

    enum Direction { 

     UP(KeyEvent.VK_UP), DOWN(KeyEvent.VK_DOWN), 
     LEFT(KeyEvent.VK_LEFT), RIGHT(KeyEvent.VK_RIGHT); 

     private int keyCode; 

     private Direction(int keyCode) { 
      this.keyCode = keyCode; 
     } 

     public int getKeyCode() { 
      return keyCode; 
     } 
    } 

    public Controller() { 

     add(lblDirection); 

     for (Direction direction : Direction.values()) { 
      directionMap.put(direction, false); 
     } 
     setKeyBindings(); 
     Timer timer = new Timer(TIMER_DELAY, new TimerListener()); 
     timer.start(); 
    } 

    private void setKeyBindings() { 
     InputMap inMap = getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW); 
     ActionMap actMap = getActionMap(); 
     for (final Direction direction : Direction.values()) { 
      KeyStroke pressed = KeyStroke.getKeyStroke(direction.getKeyCode(), 0, false); 
      KeyStroke released = KeyStroke.getKeyStroke(direction.getKeyCode(), 0, true); 
      inMap.put(pressed, direction.toString() + "pressed"); 
      inMap.put(released, direction.toString() + "released"); 
      actMap.put(direction.toString() + "pressed", new AbstractAction() { 

       private static final long serialVersionUID = 1L; 

       @Override 
       public void actionPerformed(ActionEvent e) { 
        directionMap.put(direction, true); 
       } 
      }); 
      actMap.put(direction.toString() + "released", new AbstractAction() { 

       private static final long serialVersionUID = 1L; 

       @Override 
       public void actionPerformed(ActionEvent e) { 
        directionMap.put(direction, false); 
       } 
      }); 
     } 
    } 

    private class TimerListener implements ActionListener { 

     @Override 
     public void actionPerformed(ActionEvent e) { 
      StringBuilder s = new StringBuilder("go "); 
      for (Direction direction : Direction.values()) { 
       if (directionMap.get(direction)) { 
        s.append(direction.name() + " "); 
       } 
      } 
      lblDirection.setText(s.toString()); 
     } 
    } 

    public static void createAndShowUI() { 
     JFrame frame = new JFrame("KeyMapping"); 
     frame.getContentPane().add(new Controller()); 
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     frame.setSize(400, 80); 
     frame.setLocationRelativeTo(null); 
     frame.setVisible(true); 
    } 

    public static void main(String[] args) { 
     java.awt.EventQueue.invokeLater(new Runnable() { 
      @Override 
      public void run() { 
       createAndShowUI(); 
      } 
     }); 
    } 
}