2013-02-13 52 views
0

我無法顯示按鍵被按下的次數並在JTextField或JButton中顯示數字。有誰能幫我一個忙嗎?在JTextField或Jbutton中顯示按鍵計數

我已經有KeyEvent.VK_NUMPAD3顯示,它只是似乎是,我有這樣的麻煩KeyEvent.VK_2:

我可以顯示,但數量不會每次關鍵是更新按下。

這裏是我的全部代碼..有點大,但你自找的.. :)

public class Display extends JFrame{ 

public static final int CANVAS_WIDTH = 1000; 
public static final int CANVAS_HEIGHT = 450; 
public static final Color LINE_COLOR = Color.BLACK; 
public static final Color GROUND_COLOR = Color.GREEN; 
public static final Color CANVAS_BACKGROUND = Color.CYAN; 
public static final int TRANS = Color.OPAQUE; 

private int x1 = CANVAS_WIDTH/2; 
private int y1 = CANVAS_HEIGHT/2; 
private int x2 = CANVAS_WIDTH /2; 
private int y2 = CANVAS_HEIGHT/2; 

private int Sx1 = CANVAS_WIDTH ; 
private int Sy1 = CANVAS_HEIGHT ; 
private int Sy2 = CANVAS_HEIGHT ; 

private int Rx1 = CANVAS_WIDTH - CANVAS_WIDTH; 
private int Ry1 = CANVAS_HEIGHT; 
private int Rx2 = CANVAS_WIDTH; 
private int Ry2 = CANVAS_HEIGHT ; 

private int Lx1 = CANVAS_WIDTH/2; 
private int Ly1 = CANVAS_HEIGHT/2; 
private int Lx2 = CANVAS_WIDTH/2; 
private int Ly2 = CANVAS_HEIGHT/2; 

private int Mx1 = CANVAS_WIDTH/2; 
private int My1 = CANVAS_HEIGHT/2; 
private int Mx2 = CANVAS_WIDTH/2; 
private int My2 = CANVAS_HEIGHT/2; 

int[] xs = {380, 460, 460, 540, 540, 620, 500, 380}; 
    int[] ys = {260, 260, 250, 250, 260, 260, 205, 260}; 

private DrawCanvas canvas; 
private JTextField Altitude; 
private JTextField TASpeed; 
private JLabel altButton; 
private int countA = 0; 
private int countS = 0; 
private int Bcount1 = 0; 
public String Ccount = Integer.toString(Bcount1); 



public class CountUpAltitude extends AbstractAction { 
     /** Constructor */ 
     public CountUpAltitude(String name, String shortDesc, Integer mnemonic) { 
     super(name); 
     putValue(SHORT_DESCRIPTION, shortDesc); 
     putValue(MNEMONIC_KEY, KeyEvent.VK_3); 
     Bcount1 += 1; 
     } 
     //RIGHT 
     @Override 
     public void actionPerformed(ActionEvent e) { 
      addKeyListener(new KeyAdapter() { 
       public void keyPressed(KeyEvent evt) { 
        switch(evt.getKeyCode()) { 
        case KeyEvent.VK_NUMPAD6: 
      //original count 
      countA += 5; 
      Altitude.setText(countA + ""); 
        } 
       } 
       }); 
     } 
    } 

    public class CountDownAltitude extends AbstractAction { 
     /** Constructor */ 
     public CountDownAltitude(String name, String shortDesc, Integer mnemonic) { 
     super(name); 
     putValue(SHORT_DESCRIPTION, shortDesc); 
     putValue(MNEMONIC_KEY, KeyEvent.VK_4); 
     } 
     //RIGHT 
     @Override 
     public void actionPerformed(ActionEvent e) { 
      addKeyListener(new KeyAdapter() { 
      public void keyPressed(KeyEvent evt) { 
       switch(evt.getKeyCode()) { 
       case KeyEvent.VK_NUMPAD3: 
      //original count 
      countA -= 5; 
      Altitude.setText(countA + ""); 
       } 
      } 
      }); 
     } 
    } 

    public class CountUpTASpeed extends AbstractAction { 
      /** Constructor */ 
      public CountUpTASpeed(String name, String shortDesc, Integer  mnemonic) { 
      super(name); 
      putValue(SHORT_DESCRIPTION, shortDesc); 
      putValue(MNEMONIC_KEY, KeyEvent.VK_1); 

      } 
      //LEFT 
      @Override 
      public void actionPerformed(ActionEvent e) { 
       addKeyListener(new KeyAdapter() { 
        public void keyPressed(KeyEvent evt) { 
         switch(evt.getKeyCode()) { 
         case KeyEvent.VK_NUMPAD4: 
       //original count 
       countS += 5; 
       TASpeed.setText(countS + ""); 
         } 
        } 
        }); 
      } 
     } 

    public class CountDownTASpeed extends AbstractAction { 
      /** Constructor */ 
      public CountDownTASpeed(String name, String shortDesc, Integer  mnemonic) { 
      super(name); 
      putValue(SHORT_DESCRIPTION, shortDesc); 
      putValue(MNEMONIC_KEY, KeyEvent.VK_2); 
      } 
      //LEFT 
      @Override 
      public void actionPerformed(ActionEvent e) { 
       addKeyListener(new KeyAdapter() { 
       public void keyPressed(KeyEvent evt) { 
        switch(evt.getKeyCode()) { 
        case KeyEvent.VK_NUMPAD1: 
       //original count 
       countS -= 5; 
       TASpeed.setText(countS + ""); 
        } 
       } 
       }); 
      } 
     } 



public Display() { 
    canvas = new DrawCanvas(); 
    canvas.setPreferredSize(new Dimension(CANVAS_WIDTH, CANVAS_HEIGHT)); 
    canvas.setLayout(new BorderLayout()); 
    Container cp = getContentPane(); 
    cp.setLayout(new BorderLayout()); 


    // Create the Actions shared by the button and keys 
    Action countUpAltitude = new CountUpAltitude("Count Up", 
      "", new Integer(KeyEvent.VK_ENTER)); 
    Action countDownAltitude = new CountDownAltitude("Count Down", 
      "", new Integer(KeyEvent.VK_D)); 

    Action countUpTASpeed = new CountUpTASpeed("Count Up", 
      "", new Integer(KeyEvent.VK_ENTER)); 
    Action countDownTASpeed = new CountDownTASpeed("Count Down", 
      "", new Integer(KeyEvent.VK_D)); 






     Altitude = new JTextField("0", 5); 
     Altitude.setHorizontalAlignment(JTextField.CENTER); 
     Altitude.setEditable(false); 
     Altitude.setOpaque(false); 
     Altitude.setFont(Altitude.getFont().deriveFont(25f)); 

     TASpeed = new JTextField("0", 5); 
     TASpeed.setHorizontalAlignment(JTextField.CENTER); 
     TASpeed.setEditable(false); 
     TASpeed.setOpaque(false); 
     TASpeed.setFont(Altitude.getFont().deriveFont(25f)); 

     altButton = new JLabel(); 
     altButton.setText(Ccount); 

     canvas.add(altButton, BorderLayout.SOUTH); 


     canvas.add(Altitude, BorderLayout.LINE_END); 
     canvas.add(TASpeed, BorderLayout.LINE_START); 


     //Invisible 
     JButton btnCountUp = new JButton(); 
     btnCountUp.setFocusable(false); 
     btnCountUp.setHideActionText(true); 
//  btnCountUp.setContentAreaFilled(false); 
//  btnCountUp.setBorderPainted(false); 
     canvas.add(btnCountUp, BorderLayout.NORTH); 
     //Invisible 
     JButton btnCountDown = new JButton(); 
     btnCountDown.setFocusable(false); 
     btnCountDown.setHideActionText(true); 
    //  btnCountDown.setContentAreaFilled(false); 
    //  btnCountDown.setBorderPainted(false); 
     canvas.add(btnCountDown, BorderLayout.NORTH); 
     // Set actions for buttons 
     btnCountUp.setAction(countUpAltitude); 
     btnCountDown.setAction(countDownAltitude); 


     JButton btnCountUpS = new JButton(); 
     btnCountUpS.setFocusable(false); 
     btnCountUpS.setHideActionText(true); 
//  btnCountUpS.setContentAreaFilled(false); 
//  btnCountUpS.setBorderPainted(false); 
     canvas.add(btnCountUpS, BorderLayout.SOUTH); 
     //Invisible 
     JButton btnCountDownS = new JButton(); 
     btnCountDownS.setFocusable(false); 
     btnCountDownS.setHideActionText(true); 
//  btnCountDownS.setContentAreaFilled(false); 
//  btnCountDownS.setBorderPainted(false); 
     canvas.add(btnCountDownS, BorderLayout.SOUTH); 
     // Set actions for buttons 
     btnCountUpS.setAction(countUpTASpeed); 
     btnCountDownS.setAction(countDownTASpeed); 


    addKeyListener(new KeyAdapter() { 
     public void keyPressed(KeyEvent evt) { 
      switch(evt.getKeyCode()) { 
      case KeyEvent.VK_LEFT: 
       Rx1 -= 10;  Ry1 += 10; 
       Rx2 += 10;  Ry2 -= 10; 
       x1 -=10;  x2 +=10; 
       Mx1 += 10;  Mx2 -= 10; 
       repaint(); 
       break; 
      case KeyEvent.VK_RIGHT: 
       Rx1 -= 10;  Ry1 -= 10; 
       Rx2 += 10;  Ry2 += 10; 
       x1 += 10;  x2 += 10; 
       Mx1 -= 10;  Mx2 += 10; 
       repaint(); 
       break; 
      case KeyEvent.VK_DOWN: 
       y1 -= 10;  y2 -= 10; 
       Ly1 -= 10;  Ly2 -= 10; 
       Sy1 += 10;  Sy2 -= 10; 
       Ry1 +=10;  Ry2 += 10; 
       repaint(); 
       break; 
      case KeyEvent.VK_UP: 
       y1 += 10;  y2 += 10; 
       Ly1 += 10;  Ly2 += 10; 
       Sy1 -= 10;  Sy2 += 10; 
       Ry1 -= 10;  Ry2 -= 10; 
       repaint(); 
       break; 
      case KeyEvent.VK_M: 
       System.exit(0); 
      } 
     } 
    }); 
    cp.add(canvas, BorderLayout.LINE_START); 
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    setTitle("FLIGHT DISPLAY"); 
    pack(); 
    setVisible(true); 
    requestFocus(); 
} 

class DrawCanvas extends JPanel { 
    public void paintComponent(Graphics g) { 
     super.paintComponent(g); 
     setBackground(CANVAS_BACKGROUND); 

     g.setColor(GROUND_COLOR); 
     //Draw ground Color 
     g.drawRect(Sx1 - Sx1,Sy1 /2, CANVAS_WIDTH, Sy2 /2); 
     g.fillRect(Sx1 - Sx1, Sy1 /2, CANVAS_WIDTH, Sy2 /2); 
     g.setColor(LINE_COLOR); 
     //Draw line centre horizontal 
     g.drawLine(Rx1, Ry1 /2, Rx2, Ry2 /2); 
     g.drawOval(x1 -15, y1 -15, 30, 30); 
     g.fillOval(x1 - 5, y1 -5, 10, 10); 
     //Draw line centre vertical 

     //Draw line dim 
     g.setColor(Color.YELLOW); 
     g.fillArc(300, 0, 400, 140, 0, 180); 
     g.setColor(LINE_COLOR); 
     g.drawLine(Lx1 -25, Ly1 +20, Lx2 +25, Ly2 +20); 
     g.drawLine(Lx1 -50, Ly1 +40, Lx2 +50, Ly2 +40); 
     g.drawLine(Lx1 -25, Ly1 +60, Lx2 +25, Ly2 +60); 
     g.drawLine(Lx1 -75, Ly1 +80, Lx2 +75, Ly2 +80); 
     g.drawLine(Lx1 -25, Ly1 +100, Lx2 +25, Ly2 +100); 
     //Draw line dim 
     g.drawLine(Lx1 -25, Ly1 -20, Lx2 +25, Ly2 -20); 
     g.drawLine(Lx1 -50, Ly1 -40, Lx2 +50, Ly2 -40); 
     g.drawLine(Lx1 -25, Ly1 -60, Lx2 +25, Ly2 -60); 
     g.drawLine(Lx1 -75, Ly1 -80, Lx2 +75, Ly2 -80); 
     g.drawLine(Lx1 -25, Ly1 -100, Lx2 +25, Ly2 -100); 
     //Draw polyline centre plane 
     g.drawPolyline(xs, ys, 8); 

     g.setColor(Color.BLACK); 
     g.drawArc(300, 0, 400, 140, 0, 180); 
     g.drawLine(Mx1+30, My1 + My1, Mx2, My2 - My2); 
     g.drawLine(Mx1-30, My1 + My1, Mx2, My2 - My2); 
     g.drawLine(Mx1, My1 + My1, Mx2, My2 - My2); 

    } 
} 

public static void main(String[] args) { 
    SwingUtilities.invokeLater(new Runnable() { 
     public void run() { 
      new Display(); 
     } 
    }); 
} 
} 
+1

檢查[this] [1],你能告訴你哪個鍵有問題嗎? [1]:http://stackoverflow.com/questions/11923033/java-awt-robot-keypress-throws-illegalargumentexception-when-when-pressing-quota – aksappy 2013-02-13 12:08:15

+0

nopes這並不真正幫助作爲即時通訊不使用機器人和im試圖顯示按鈕或文本字段上的按下量而不是printStackTrace。和有問題的關鍵是putValue(MNEMONIC_KEY,KeyEvent.VK_2);我上面的代碼行..即將顯示多少次這個鍵被按下! – Delish 2013-02-13 12:48:00

+1

爲什麼要將KeyListener添加到AbstractAction中?這讓我感到困惑不已。如果alt-2被按下,你的目標是什麼?最下面的按鈕被壓低了,但是沒有動作會發生,因爲actionPerformed中的所有代碼都包含在一個奇怪的KeyListener中。在附註中,爲什麼要使用KeyListeners而不是Key綁定?它們更適合Swing應用程序。 – 2013-02-14 02:26:37

回答

2

KeyEvent.VK_2不過與2號在非數字鍵盤區的美國標準的關鍵鍵盤。

public static final int VK_2    = 0x32; 

下面的代碼工作正常,我,我不指望你還沒有加入這個鍵開關的情況下,這樣你就可以做一個小測試,打印所需的鍵的ASCII值,並找出它在KeyEvent類中實際上對應於什麼。

public void keyPressed(KeyEvent e) { 
    switch (e.getKeyCode()) { 
     case KeyEvent.VK_2: 
      System.out.println("Non Numpad 2"); 
     case KeyEvent.VK_NUMPAD3: 
      System.out.println("NumPad 3"); 
    } 
} 
+0

以及我其實有一個開關的各種按鈕和所有工作正常,除了非數字鍵盤2我想顯示多少次它被按下不是它被按下..此刻mu numpad 3倒計數和數字non-numpad 2必須按b來初始化numpad3 ..並且我想要顯示非numpad 2已被按下多少次,因此顯示numpad 3已經初始化了多少次.. – Delish 2013-02-13 15:41:55

+1

BenJi,聲明一個類變量按下VK_2鍵時按下按鍵的次數會有所幫助。我不瞭解初始化部分。你的意思是你想動態初始化Numpad3 VK_2鍵被按下的次數。你會發布你試圖達到這個目標的程序嗎?我的電子郵件ID在我的個人資料 – aksappy 2013-02-13 17:15:46

+0

我將如何向您發送完整的代碼? – Delish 2013-02-13 17:50:42