getting the right text from a button in netbeans
public class tCalculator implements ActionListener{
//private JTextField resultText;
public void Calculator(){
btn1.addActionListener(this);
btn2.addActionListener(this);
btn3.addActio开发者_开发百科nListener(this);
}
public void actionPerformed(ActionEvent evt) {
Object sourc = evt.getSource();
String source = evt.getActionCommand();
System.out.println(source);
jTextArea1.append(source);
}}
hello
this is a part of my program through which i am trying to make a calculator by using buttons in netbeans. what i am trying to do is to get the text within the button and print it in a jtextfield but when i press the button. on running the program the text is not coming in the right number. if i press "1" it doesn't display the first time. in the second time it comes once and then maybe int he third time it comes four times but the display is random and it is not known how many time it would display.help would be appreciated.
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
}
}}
精彩评论