【问题标题】:KeyListener doesn't triggerKeyListener 不会触发
【发布时间】:2014-09-17 21:22:48
【问题描述】:

这是我正在制作的计算器的字母代码,我希望它是当我按下“1”时,计算器在我的数组中输入 1,但它甚至没有触发 keyPressed 等等。

package ws1;
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
import java.util.ArrayList;

public class ws41 extends Applet implements ActionListener, KeyListener{
    Button equals,zero,one,two,three,four,five,six,seven,eight,nine,divide,times,plus,minus,u2s,s2u,period;
    Label show;
    double total;
    double dot;
    double left=0;
    double right=0;
    String tots = "";
    String sign = "";
    ArrayList<Integer> Calc = new ArrayList<Integer>();

    public void init(){
        setSize(30,200);
        show = new Label("");
        period = new Button(" . ");
        u2s = new Button(" Usd to Sek");
        s2u = new Button("Sek to Usd");
        zero = new Button(" 0 ");
        one = new Button(" 1 ");
        two = new Button(" 2 ");
        three = new Button(" 3 ");
        four = new Button(" 4 ");
        five = new Button(" 5 ");
        six = new Button(" 6 ");
        seven = new Button(" 7 ");
        eight = new Button(" 8 ");
        nine = new Button(" 9 ");
        plus = new Button(" + ");
        minus = new Button(" - ");
        times = new Button(" * ");
        divide = new Button(" / ");
        equals = new Button(" = ");
        this.addKeyListener(this);
        zero.addActionListener (this);
        one.addActionListener (this);
        two.addActionListener (this);
        three.addActionListener (this);
        four.addActionListener (this);
        five.addActionListener (this);
        six.addActionListener (this);
        seven.addActionListener (this);
        eight.addActionListener (this);
        nine.addActionListener (this);
        plus.addActionListener (this);
        times.addActionListener (this);
        minus.addActionListener (this);
        times.addActionListener (this);
        divide.addActionListener (this);
        equals.addActionListener (this);
        u2s.addActionListener (this);
        s2u.addActionListener (this);
        period.addActionListener(this);
        //add(show);
        add(one);
        add(two);
        add(three);
        add(four);
        add(five);
        add(six);
        add(seven);
        add(eight);
        add(nine);
        add(zero);
        add(period);
        add(plus);
        add(minus);
        add(times);
        add(divide);
        add(equals);
        add(u2s);
        add(s2u);
    }
    public void keyPressed(KeyEvent ke) {
        Calc.add(ke.getKeyCode());
    }


    public void keyReleased(KeyEvent ke){
    }

    public void keyTyped(KeyEvent ke) {
    }

【问题讨论】:

  • 考虑发布MCVE。你为一个很小的问题发布了很多代码。
  • "当我按“1”时计算器输入 1" 按键盘上的 1 或计算器上的 1?
  • 键盘上的一个对不起,缩短了
  • 您不应该在按钮上使用 KeyListener,而应该使用 ActionListener。实际上,您可以通过使用Action APIkey bindings 大大简化此操作,因为Action 将在按钮和键绑定之间共享
  • 我会检查一下谢谢。

标签: java keylistener


【解决方案1】:

KeyListener 是焦点上下文。也就是只有注册到的组件有焦点(并且是可聚焦的)时才会响应key事件

AWT 已过时 15 年以上,已被 Swing 取代。考虑改用基于 Swing 的组件

您不应该在按钮上使用KeyListener,而应该使用ActionListener

请参阅How to use buttonsHow to write ActionListeners 了解更多详情

事实上,您可以通过使用Action APIkey bindings 大大简化此操作,因为Action 将在按钮和键绑定之间共享

【讨论】:

  • 但他不会使用按钮吧? "One on the keyboard Sorry, shortened it – Chris"
  • @zach 他们是,他们期望当他们按下键盘上的相应键时,关联的按钮会被触发,他们正在向按钮添加 KeyListensers
  • @Takendarkk 他们可能是一个团队...?
  • 哦,好吧,我明白你的意思了
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-07-01
  • 2013-06-04
  • 2011-07-09
  • 2019-09-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多