从netbeans中的按钮获取正确的文本

public class tCalculator implements ActionListener{ //private JTextField resultText; public void Calculator(){ btn1.addActionListener(this); btn2.addActionListener(this); btn3.addActionListener(this); } public void actionPerformed(ActionEvent evt) { Object sourc = evt.getSource(); String source = evt.getActionCommand(); System.out.println(source); jTextArea1.append(source); }} 

你好

这是我的程序的一部分,我试图通过使用netbeans中的按钮来制作计算器。 我想要做的是获取按钮内的文本并将其打印在jtextfield中但是当我按下按钮时。 在运行程序时,文本不是正确的数字。 如果我按“1”它不会第一次显示。 在第二次它来一次然后可能在第三次来它四次,但显示是随机的,不知道它将显示多少时间。帮助将不胜感激。

 public void actionPerformed(ActionEvent evt) { Object source = evt.getSource(); if (source instanceof JButton){ JButton but = (JButton) source; String text = but.getText(); //do what you want with the text } }}