简体   繁体   English

在一组jButton上实现ActionListener时遇到问题

[英]Problem implementing ActionListener to a group of jButtons

i have a problem implementing ActionListener on a group of 10 jButtons. 我在一组10个jButton上实现ActionListener时遇到问题。 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. 每个按钮的text属性设置为0到9之间的一个数字。因此jButton1的text属性设置为1,JButton2的text设置为2,....,....然后jButton9的文本设置为1。设置为9。当我单击这些按钮中的任何一个时,我想将其text属性的值附加到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. 问题是,每次我单击按钮时,其text属性的值都会打印两次,有时甚至是三次,甚至是三次,它只是随机发生。

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 例如,如果我单击一次带有文本4的按钮,则可以在JTextField中打印44,如果再单击一次7,则最终可以得到4477甚至447777。下面是我的代码

 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 . 您的方法btn1ActionPerformed添加了另一个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. 每当您单击该按钮时,您就会再有一个Listener ,该Listener在下次单击时执行。

Looks like this code was generated by an IDE. 看起来这段代码是由IDE生成的。 Remove that action there and your code should work. 在那里删除该操作,您的代码应该可以工作。


EDIT: 编辑:

  1. Remove the Actions in your IDE 在您的IDE中删除操作
  2. Remove the constructor of tCalculator and put the code into the constructor of JFrame1 (below initComponents . 删除tCalculator的构造tCalculator ,并将代码放入JFrame1的构造函数中( 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 . tCalculator扩展JFrame没有任何意义。 Remove that. 删除那个。
  2. Class names start with an upper-case letter ( TCalculator ). 类名以大写字母( TCalculator )开头。 A better name could be ButtonActionListener or something like that. 更好的名称可以是ButtonActionListener或类似的名称。

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. 这是因为您刚刚使用this和on方法将动作侦听器分配给了按钮,所以您再次辅助动作侦听器,它是tCalculator类的实例,因此,当您按下按钮时,就会调用船boat动作侦听器,并显示2个结果,只需删除btn1ActionPerformed方法,它将可以正常工作。 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();
    }

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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