我的程序只是一个简单的计算器,出于某种原因,每当我按下键盘上的键时,都不会调用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)
答案 0 :(得分:0)
您遇到的第一个问题是Unresponsive KeyListener for JFrame.
考虑将keylistener注册到相关组件。
然后,正如@Tom所指出的那样,KeyEvent.VK_0|KeyEvent.VK_NUMPAD0
例如,在整数值KeyEvent.VK_0
和{之间执行按位 OR 操作{1}}。
并且
KeyEvent.VK_NUMPAD0
变为
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))
,如果左侧部分为真,则会阻止评估右手部分。