簡體   English   中英

在一組jButton上實現ActionListener時遇到問題

[英]Problem implementing ActionListener to a group of jButtons

我在一組10個jButton上實現ActionListener時遇到問題。 每個按鈕的text屬性設置為0到9之間的一個數字。因此jButton1的text屬性設置為1,JButton2的text設置為2,....,....然后jButton9的文本設置為1。設置為9。當我單擊這些按鈕中的任何一個時,我想將其text屬性的值附加到JTextField。

問題是,每次我單擊按鈕時,其text屬性的值都會打印兩次,有時甚至是三次,甚至是三次,它只是隨機發生。

例如,如果我單擊一次帶有文本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);

    }   

您的方法btn1ActionPerformed添加了另一個ActionListener 我們看不到它的名稱,但這可以解釋您的問題。 每當您單擊該按鈕時,您就會再有一個Listener ,該Listener在下次單擊時執行。

看起來這段代碼是由IDE生成的。 在那里刪除該操作,您的代碼應該可以工作。


編輯:

  1. 在您的IDE中刪除操作
  2. 刪除tCalculator的構造tCalculator ,並將代碼放入JFrame1的構造函數中( initComponents下方)。

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

這些步驟可確保每個按鈕僅一次注冊您的監聽器。

順便說一句:

  1. tCalculator擴展JFrame沒有任何意義。 刪除那個。
  2. 類名以大寫字母( TCalculator )開頭。 更好的名稱可以是ButtonActionListener或類似的名稱。

這是因為您剛剛使用this和on方法將動作偵聽器分配給了按鈕,所以您再次輔助動作偵聽器,它是tCalculator類的實例,因此,當您按下按鈕時,就會調用船boat動作偵聽器,並顯示2個結果,只需刪除btn1ActionPerformed方法,它將可以正常工作。 現在嘗試下面的代碼................................

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