在JTextField或Jbutton中显示按键计数

时间:2013-02-13 11:49:28

标签: java swing jbutton jtextfield keyevent

我无法显示按键的次数并在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 个答案:

答案 0 :(得分: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");
    }
}