How to find a button source in AWT (calculator homework)

前端 未结 2 2034
夕颜
夕颜 2020-11-21 22:15

We were asked to make a simple GUI calculator, I used getSource() to detect the buttons, cause that\'s what they taught us. It works by typing the 1st and 2nd value then cho

2条回答
  •  庸人自扰
    2020-11-21 22:37

    You can check if your source is a Button (not actually nessesary since you should know it is only called from a button) and then type cast to a button and use .getText()

    String sourceText;
    if (ae.getSource() instanceof Button) {
    sourceText = ((Button)ae.getSource()).getText();
    }
    

提交回复
热议问题