简体   繁体   中英

Problem implementing ActionListener to a group of jButtons

i have a problem implementing ActionListener on a group of 10 jButtons. Each button has its text property set to a digit, from 0 - 9. So jButton1 will have its text property set to 1, JButton2 will have 2 as its text, ...., .... then jButton9 will have its text set to 9. When i click any of those buttons, i want to append the value of its text property to a JTextField.

The problem am getting is every time i click a button, the value of its text property is printed twice, some times thrice some even four times, it just happens randomly.

For example, if i click a button with text 4 once, i can get 44 printed in the JTextField, if i then click 7 only once again, i can end up with 4477 or even 447777. below is my code

 public class tCalculator extends JFrame implements ActionListener{

    public tCalculator(){
        btn1.addActionListener(this);
        btn2.addActionListener(this);
        btn3.addActionListener(this);
        btn4.addActionListener(this);
        btn5.addActionListener(this);
        btn6.addActionListener(this);
        btn7.addActionListener(this);
        btn8.addActionListener(this);
        btn9.addActionListener(this);
        btnZero.addActionListener(this);
          } 

    public void actionPerformed(ActionEvent evt) {

        String x = txtArea.getText();
        String k = evt.getActionCommand();
        String a = x + k ;
        txtArea.setText(a);

    }}

private void btn1ActionPerformed(java.awt.event.ActionEvent evt) {                                     
        ActionListener actionListener = new tCalculator();
        btn1.addActionListener(actionListener);

    }   

Your method btn1ActionPerformed adds another ActionListener . We do not see where it is called, but this would explain your problem. Whenever you click the button, you have one more Listener which is executed at the next click.

Looks like this code was generated by an IDE. Remove that action there and your code should work.


EDIT:

  1. Remove the Actions in your IDE
  2. Remove the constructor of tCalculator and put the code into the constructor of JFrame1 (below initComponents .

...
initComponents();
ActionListener actionListener = new tCalculator();
btn1.addActionListener(actionListener);
btn2.addActionListener(actionListener);
....

These steps ensure that your Listener is registered exactly once per button.

BTW:

  1. It does not make sense for tCalculator to extend JFrame . Remove that.
  2. Class names start with an upper-case letter ( TCalculator ). A better name could be ButtonActionListener or something like that.

it is because you have just assigned the action listener to your button with this and on method you'r again assigining the action listener which is the instance of tCalculator class so when you press the button boath action listener called and 2 results are shown to you, just remove the btn1ActionPerformed method and it will work fine. try below code now................................

import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JTextField;

public class Tcalculator extends JFrame implements ActionListener{
    private JButton btn1,btn2,btn3,btn4,btn5,btn6,btn7,btn8,btn9,btnZero;
    private JTextField txtArea;

    public Tcalculator(){
        btn1 = new JButton("1");
        btn2 = new JButton("2");
        btn3 = new JButton("3");
        btn4 = new JButton("4");
        btn5 = new JButton("5");
        btn6 = new JButton("6");
        btn7 = new JButton("7");
        btn8 = new JButton("8");
        btn9 = new JButton("9");
        btnZero = new JButton("0");
        txtArea = new JTextField(15);
        init();
          } 

    //performed all gui operations
    public void init(){

        getContentPane().setLayout(new FlowLayout());
        setSize(200, 200);
        add(txtArea);
        add(btn1);add(btn2);
        add(btn3);add(btn4);
        add(btn5);add(btn6);
        add(btn7);add(btn8);
        add(btn9);add(btnZero);

        btn1.addActionListener(this);
        btn2.addActionListener(this);
        btn3.addActionListener(this);
        btn4.addActionListener(this);
        btn5.addActionListener(this);
        btn6.addActionListener(this);
        btn7.addActionListener(this);
        btn8.addActionListener(this);
        btn9.addActionListener(this);
        btnZero.addActionListener(this);

        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setVisible(true);

    }

    // i am using this your made function nothing changed
    public void actionPerformed(ActionEvent evt) {

        String x = txtArea.getText();
        String k = evt.getActionCommand();
        String a = x + k ;
        txtArea.setText(a);

    }
    public static void main(String args[]){
        new  Tcalculator();
    }

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM