当我按下一个键时没有调用 keyPressed() 方法?

Posted

技术标签:

【中文标题】当我按下一个键时没有调用 keyPressed() 方法?【英文标题】:The keyPressed() method isn't being called when I press a key? 【发布时间】:2018-07-16 15:29:29 【问题描述】:

我的程序只是一个简单的计算器,出于某种原因,每当我按下键盘上的某个键时,都不会调用 keyPressed() 方法。我已经完成了 System.out.println() 以测试它是否被调用,但事实并非如此。请帮忙

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

public class FullCalculator extends Frame implements ActionListener,KeyListener
private Button btn0, btn1, btn2, btn3, btn4, btn5, btn6, btn7, btn8, btn9, 
btnAdd, btnSub, btnMul, btnDiv, btnEquals, btnDot, btnC, btnCE, btnRand, 
btnBack;
private Label lbl1, lbl2, black;
private float temp, temp2;
private String operator, tempS;

public FullCalculator (String s)
    super (s);
    init();
    this.addKeyListener(this);
    this.setFocusable(true);
    this.requestFocus(true);
    setLayout(null);


public void init()
    Font f = new Font ("Courier", Font.BOLD, 13);

    btn0 = new Button ("0");
    btn0.setBounds(6, 285, 70, 70);
    btn0.addActionListener(this);
    btn0.setFont (f);

    btn1 = new Button ("1");
    btn1.setBounds(6, 215, 70, 70);
    btn1.addActionListener(this);
    btn1.setFont (f);

    btn2 = new Button ("2");
    btn2.setBounds(76, 215, 70, 70);
    btn2.addActionListener(this);
    btn2.setFont (f);

    btn3 = new Button ("3");
    btn3.setBounds(146, 215, 70, 70);
    btn3.addActionListener(this);
    btn3.setFont (f);

    btn4 = new Button ("4");
    btn4.setBounds(6, 145, 70, 70);
    btn4.addActionListener(this);
    btn4.setFont (f);

    btn5 = new Button ("5");
    btn5.setBounds(76, 145, 70, 70);
    btn5.addActionListener(this);
    btn5.setFont (f);

    btn6 = new Button ("6");
    btn6.setBounds(146, 145, 70, 70);
    btn6.addActionListener(this);
    btn6.setFont (f);

    btn7 = new Button ("7");
    btn7.setBounds(6, 75, 70, 70);
    btn7.addActionListener(this);
    btn7.setFont (f);

    btn8 = new Button ("8");
    btn8.setBounds(76, 75, 70, 70);
    btn8.addActionListener(this);
    btn8.setFont (f);

    btn9 = new Button ("9");
    btn9.setBounds(146, 75, 70, 70);
    btn9.addActionListener(this);
    btn9.setFont (f);

    btnAdd = new Button ("+");
    btnAdd.setBounds(216, 75, 70, 70);
    btnAdd.addActionListener(this);
    btnAdd.setFont (f);

    btnSub = new Button ("-");
    btnSub.setBounds(216, 145, 70, 70);
    btnSub.addActionListener(this);
    btnSub.setFont (f);

    btnMul = new Button ("*");
    btnMul.setBounds(216, 215, 70, 70);
    btnMul.addActionListener(this);
    btnMul.setFont (f);

    btnDiv = new Button ("/");
    btnDiv.setBounds(146, 285, 70, 70);
    btnDiv.addActionListener(this);
    btnDiv.setFont (f);

    btnEquals = new Button ("=");
    btnEquals.setBounds(216, 285, 70, 70);
    btnEquals.addActionListener(this);
    btnEquals.setFont (f);

    btnDot = new Button (".");
    btnDot.setBounds (76, 285, 70, 70);
    btnDot.addActionListener(this);
    btnDot.setFont (f);

    btnC = new Button ("C");
    btnC.setBounds (76, 355, 70, 70);
    btnC.addActionListener(this);
    btnC.setFont (f);

    btnCE = new Button ("CE");
    btnCE.setBounds (146, 355, 70, 70);
    btnCE.addActionListener(this);
    btnCE.setFont (f);

    btnRand = new Button ("RandNum");
    btnRand.setBounds (6, 355, 70, 70);
    btnRand.addActionListener(this);
    btnRand.setFont (f);

    btnBack = new Button ("<==");
    btnBack.setBounds (216, 355, 70, 70);
    btnBack.addActionListener (this);
    btnBack.setFont (f);

    lbl1 = new Label ("", Label.RIGHT);
    lbl1.setBounds (9, 52, 274, 22);
    lbl1.setBackground (Color.WHITE);
    lbl1.setFont (new Font ("Courier", Font.BOLD, 20));

    lbl2 = new Label ("", Label.RIGHT);
    lbl2.setBounds (9, 32, 274, 20);
    lbl2.setBackground (Color.WHITE);
    lbl2.setFont (new Font ("Monospaced", Font.PLAIN, 12));

    black = new Label ("");
    black.setBounds (6, 30, 280, 50);
    black.setBackground (Color.BLACK);

    add(btn0);
    add(btn1);
    add(btn2);
    add(btn3);
    add(btn4);
    add(btn5);
    add(btn6);
    add(btn7);
    add(btn8);
    add(btn9);
    add(btnAdd);
    add(btnSub);
    add(btnMul);
    add(btnDiv);
    add(btnEquals);
    add(btnDot);
    add(btnC);
    add(btnCE);
    add(btnRand);
    add(btnBack);

    add(lbl1);
    add(lbl2);
    add(black);

    addWindowListener(new WindowAdapter()
            public void windowClosing(WindowEvent we)
                System.exit(0);
            
        );


@Override
public void actionPerformed(ActionEvent e)
    try
        if (e.getSource()==btn0)
            setLabel("0");
        
        else if (e.getSource()==btn1)
            setLabel("1");
        
        else if (e.getSource()==btn2)
            setLabel("2");
        
        else if (e.getSource()==btn3)
            setLabel("3");
        
        else if (e.getSource()==btn4)
            setLabel("4");
        
        else if (e.getSource()==btn5)
            setLabel("5");
        
        else if (e.getSource()==btn6)
            setLabel("6");
        
        else if (e.getSource()==btn7)
            setLabel("7");
        
        else if (e.getSource()==btn8)
            setLabel("8");
        
        else if (e.getSource()==btn9)
            setLabel("9");
        
        else if (e.getSource()==btnAdd)
            temp = Float.parseFloat(lbl1.getText());
            operator = "+";
            lbl2.setText(lbl1.getText()+"+");
            lbl1.setText("");
        
        else if (e.getSource()==btnSub)
            temp = Float.parseFloat(lbl1.getText());
            operator = "-";
            lbl2.setText(lbl1.getText()+"-");
            lbl1.setText("");
        
        else if (e.getSource()==btnMul)
            temp = Float.parseFloat(lbl1.getText());
            operator = "*";
            lbl2.setText(lbl1.getText()+"*");
            lbl1.setText("");
        
        else if (e.getSource()==btnDiv)
            temp = Float.parseFloat(lbl1.getText());
            operator = "/";
            lbl2.setText(lbl1.getText()+"/");
            lbl1.setText("");
        
        else if (e.getSource()==btnEquals)
            temp2 = Float.parseFloat(lbl1.getText());
            switch (operator)
                case "+": lbl1.setText("" + (temp+temp2)); break;
                case "-": lbl1.setText("" + (temp-temp2)); break;
                case "*": lbl1.setText("" + (temp*temp2)); break;
                case "/": lbl1.setText("" + (temp/temp2)); break;
            
        
        else if (e.getSource()==btnDot)
            setLabel(".");
        
        else if (e.getSource()==btnC)
            lbl1.setText("");
            lbl2.setText("");
        
        else if (e.getSource()==btnCE)
            lbl1.setText("");
        
        else if (e.getSource()==btnRand)
            lbl1.setText(""+(int)(Math.random()*100)+1);
        
        else if (e.getSource()==btnBack)
            tempS = lbl1.getText();
            lbl1.setText((lbl1.getText().substring(0, tempS.length() - 1)));
        
    catch (Exception ex)
        JOptionPane.showMessageDialog (null, "Error " + ex.getMessage(), "Error", JOptionPane.ERROR_MESSAGE);
    


@Override
public void keyPressed(KeyEvent e)
    if (e.getKeyCode()==(KeyEvent.VK_0|KeyEvent.VK_NUMPAD0))
        setLabel("0");
    
    else if (e.getKeyCode()==(KeyEvent.VK_1|KeyEvent.VK_NUMPAD1))
        setLabel("1");
    
    else if (e.getKeyCode()==(KeyEvent.VK_2|KeyEvent.VK_NUMPAD2))
        setLabel("2");
    
    else if (e.getKeyCode()==(KeyEvent.VK_3|KeyEvent.VK_NUMPAD3))
        setLabel("3");
    
    else if (e.getKeyCode()==(KeyEvent.VK_4|KeyEvent.VK_NUMPAD4))
        setLabel("4");
    
    else if (e.getKeyCode()==(KeyEvent.VK_5|KeyEvent.VK_NUMPAD5))
        setLabel("5");
    
    else if (e.getKeyCode()==(KeyEvent.VK_6|KeyEvent.VK_NUMPAD6))
        setLabel("6");
    
    else if (e.getKeyCode()==(KeyEvent.VK_7|KeyEvent.VK_NUMPAD7))
        setLabel("7");
    
    else if (e.getKeyCode()==(KeyEvent.VK_8|KeyEvent.VK_NUMPAD8))
        setLabel("8");
    
    else if (e.getKeyCode()==(KeyEvent.VK_9|KeyEvent.VK_NUMPAD9))
        setLabel("9");
    
    else if (e.getKeyCode()==KeyEvent.VK_PLUS)
        temp = Float.parseFloat(lbl1.getText());
        operator = "+";
        lbl2.setText(lbl1.getText()+"+");
        lbl1.setText("");
    
    else if (e.getKeyCode()==KeyEvent.VK_MINUS)
        temp = Float.parseFloat(lbl1.getText());
        operator = "-";
        lbl2.setText(lbl1.getText()+"-");
        lbl1.setText("");
    
    else if (e.getKeyCode()==KeyEvent.VK_ASTERISK)
        temp = Float.parseFloat(lbl1.getText());
        operator = "*";
        lbl2.setText(lbl1.getText()+"*");
        lbl1.setText("");
    
    else if (e.getKeyCode()==KeyEvent.VK_SLASH)
        temp = Float.parseFloat(lbl1.getText());
        operator = "/";
        lbl2.setText(lbl1.getText()+"/");
        lbl1.setText("");
    
    else if (e.getKeyCode()==KeyEvent.VK_PERIOD)
        setLabel(".");
    


@Override
public void keyReleased(KeyEvent e)



@Override
public void keyTyped(KeyEvent e)



public void setLabel(String key)
    lbl1.setText(lbl1.getText()+key);

我也试过不使用 setFocus(true) 和 requestFocus(true)

【问题讨论】:

为什么你有一个也是 KeyListener 的 Frame?您应该避免创建“一体化”类。 Pipe (|) operator in Java的可能重复 请阅读欺骗问题以了解此e.getKeyCode()==(KeyEvent.VK_6|KeyEvent.VK_NUMPAD6) 并不意味着“VK_6 或 VK_NUMPAD0”。 请将代码格式化为代码,并仅提供最小示例。 (MCVE) 另外,您的 KeyListener 被分配给 Frame,而不是 Labels。 【参考方案1】:

您遇到的第一个问题是Unresponsive KeyListener for JFrame.

考虑将 keylistener 注册到相关组件。

然后,正如@Tom 也指出的那样,例如KeyEvent.VK_0|KeyEvent.VK_NUMPAD0整数KeyEvent.VK_0KeyEvent.VK_NUMPAD0 之间执行按位OR 运算。

还有

 if (e.getKeyCode()==(KeyEvent.VK_0|KeyEvent.VK_NUMPAD0))

变成

if (e.getKeyCode()==<result of the above bitwise OR operation>)

用例如替换你的条件

if ((e.getKeyCode()==KeyEvent.VK_0) || (e.getKeyCode()==KeyEvent.VK_NUMPAD0))

获得预期的行为。

还要注意双管道||,这样可以防止在左侧部分为真时评估右侧部分。

参照。 Pipe (|) operator in Java

【讨论】:

请注意,在这种情况下,VK_0=48VK_NUMPAD0=9648|96=112VK_F1=112。因此,在尝试检查 0 时,它实际上与 F1 匹配。 虽然有趣,但不要在代码中使用数字,使用 KeyEvent.VK_ 常量 我很感谢您的回复,但使用 ||它没有工作,出现以下错误; 二元运算符“||”的操作数类型错误第一种:int;第二种:int; 当我完全删除 OR 时,它确实有效,所以无论如何 ;) 现在,由于某种原因,出现了另一个问题......每当我输入任何数字时,它都会将计算器显示设置为“00”而不是“0”......请帮助;/跨度>

以上是关于当我按下一个键时没有调用 keyPressed() 方法?的主要内容,如果未能解决你的问题,请参考以下文章

觅求高手.java键盘事件.

我需要能够暂停整个游戏,但是当我按下键盘上的 p 键时,只有我的汽车精灵暂停

当我按下键盘键时,Windows 可以播放声音吗?

当我按 Enter 键时,选项卡控件的选项卡页会关闭

当我在OpenGL中按下一个键时无法绘制形状

我想要一个简单的程序来在我按下一个键时冻结/解冻输入