ActionListener添加到JButton但不起作用

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了ActionListener添加到JButton但不起作用相关的知识,希望对你有一定的参考价值。

用户必须在文本字段中输入数字。当用户按下Enter键时,应该从文本字段中删除该数字并将其添加到ArrayList。当用户单击“计算”按钮时,程序必须计算结果并将其投影到第二个文本字段。当用户单击“重置”时按钮,必须删除文本字段上的文本,并且必须清除ArrayList。我将ActionListener添加到btnCalc,但是当我单击它们时,TextField上没有任何变化

  import java.awt.event.ActionEvent;
  import java.awt.event.ActionListener;
  import java.awt.event.KeyEvent;
  import java.awt.event.KeyListener;
  import java.util.ArrayList;

  import javax.swing.ButtonGroup;
  import javax.swing.JButton;
  import javax.swing.JFrame;
  import javax.swing.JTextField;
  import javax.swing.JLabel;
  import javax.swing.JPanel;
  import javax.swing.JRadioButton;

  public class AddMult extends JFrame implements KeyListener,     ActionListener{

private JLabel lblEnterNum;
private JTextField txtEnterNum;
private JRadioButton radBtnAdd;
private JRadioButton radBtnMul;
private JButton btnCalc;
private JButton btnReset;
private JLabel lblResult;
private JTextField txtResult;







public AddMult() {
    setSize(300, 300);



    lblEnterNum=new JLabel();
    lblEnterNum.setText("Enter a number:");

    lblEnterNum.setSize(100,20);
    lblEnterNum.setLocation(100, 20);
    add(lblEnterNum);
    lblEnterNum.setVisible(true);


    txtEnterNum = new JTextField();
    txtEnterNum.setSize(90,20);
    txtEnterNum.setLocation(105, 60);
    add(txtEnterNum);
    txtEnterNum.setVisible(true);
    txtEnterNum.addKeyListener(this);


    JRadioButton radBtnAdd = new JRadioButton();
    radBtnAdd.setText("Add");
    radBtnAdd.setSize(70,50);
    radBtnAdd.setLocation(80, 80);
    radBtnAdd.setSelected(true);
    add(radBtnAdd);


    JRadioButton radBtnMul = new JRadioButton();
    radBtnMul.setText("Multiply");
    radBtnMul.setSize(100,50);
    radBtnMul.setLocation(160, 80);
    add(radBtnMul);

    ButtonGroup group = new ButtonGroup();
    group.add(radBtnAdd);
    group.add(radBtnMul);

    btnCalc= new JButton();
    btnCalc.setText("Calculate");
    btnCalc.setLocation(70, 140);
    btnCalc.setSize(80, 40);
    add(btnCalc);


    btnReset=new JButton();
    btnReset.setText("Reset");
    btnReset.setLocation(180, 140);
    btnReset.setSize(60, 40);
    add(btnReset);


    JLabel lblResult =new JLabel();
    lblResult.setText("Result:");
    lblResult.setLocation(95, 200);
    lblResult.setSize(70, 40);
    add(lblResult);


    JTextField txtResult = new JTextField();
    txtResult.setLocation(150, 205);
    txtResult.setSize(70,30);
    txtResult.setEditable(false);
    add(txtResult);

    btnCalc.addActionListener(this);
    btnReset.addActionListener(this);

    setLayout(null);
    setVisible(true);
    setResizable(false);



    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);


}

public static void main(String[] args) {
    // TODO Auto-generated method stub
AddMult test = new AddMult();
}
ArrayList<Integer> list = new ArrayList<Integer>();
public void addList() {

    int num = Integer.parseInt(txtEnterNum.getText().trim());
    list.add(num);
    txtEnterNum.setText("");


}

public String sum() {
    int total=0;
    for (int i = 0; i < list.size(); i++) {
        total=total+list.get(i);
    }
    return total + "";
}

public String mult() {
    int mult=1;
    for (int i = 0; i < list.size(); i++) {
        mult=mult*list.get(i);
    }
    return mult+"";
}

@Override
public void actionPerformed(ActionEvent a) {

    // TODO Auto-generated method stub
    String sum =sum();
    String mult=mult();

    if(btnCalc.equals(a.getSource())) {
    if (radBtnAdd.isSelected()) {

        txtResult.setText(sum);
    }
    else if (radBtnMul.isSelected()) {

        txtResult.setText(mult);
    }


    }
    else if (btnReset.equals(a.getSource())) {
        txtEnterNum.setText("");
        txtResult.setText("");
        list.clear();
    }

}

@Override
public void keyPressed(KeyEvent e) {
    // TODO Auto-generated method stub
    if(e.getKeyCode()==e.VK_ENTER) {
        addList();
    }
    else if(e.getKeyCode()==e.VK_ESCAPE)
        System.exit(1);
}

@Override
public void keyReleased(KeyEvent arg0) {
    // TODO Auto-generated method stub

}

@Override
public void keyTyped(KeyEvent arg0) {
    // TODO Auto-generated method stub

}
}
答案

初始化btnCalc和btnReset时,请不要将它们重新声明为:

JButton button;

void foo(){
    JButton button = new JButton();
 }

但是这样:

JButton button;

void foo(){
    button = new JButton();
 }

此外,在您的动作侦听器中,不要将该按钮的名称作为字符串进行测试,而是将该按钮测试为等于该对象:

actionEvent.getSource().equals(btnReset);
另一答案

因为你创建了两次btnCalc。请重新扫描此代码:

JButton btnCalc = new JButton();

和写:

private JButton btnCalc = new JButton();

之后:

btnCalc.equals(a.getSource())

将返回真实。

以上是关于ActionListener添加到JButton但不起作用的主要内容,如果未能解决你的问题,请参考以下文章

从 JButton 中删除一个 ActionListener

组合 Swing 组件:如何添加添加 ActionListener 的功能?

当JButton按下时isShiftDown?

如何在单击JButton后刷新JTable

JavaGUI程序给按钮添加快捷键

需要有关 JTable 的帮助吗?